Simplify code by using while and if let constructions

This commit is contained in:
Rafał Harabień 2018-05-27 18:37:33 +02:00
parent a760b4b6d0
commit 380add131d
4 changed files with 50 additions and 91 deletions

View File

@ -493,10 +493,7 @@ impl LongNameBuilder {
fn truncate(&mut self) { fn truncate(&mut self) {
// Truncate 0 and 0xFFFF characters from LFN buffer // Truncate 0 and 0xFFFF characters from LFN buffer
let mut lfn_len = self.buf.len(); let mut lfn_len = self.buf.len();
loop { while lfn_len > 0 {
if lfn_len == 0 {
break;
}
match self.buf[lfn_len-1] { match self.buf[lfn_len-1] {
0xFFFF | 0 => lfn_len -= 1, 0xFFFF | 0 => lfn_len -= 1,
_ => break, _ => break,

View File

@ -32,27 +32,21 @@ impl <'a, 'b> File<'a, 'b> {
fn update_size(&mut self) { fn update_size(&mut self) {
let offset = self.offset; let offset = self.offset;
match self.entry { if let Some(ref mut e) = self.entry {
Some(ref mut e) => { e.reset_modified();
e.reset_modified(); if e.inner().size().map_or(false, |s| offset > s) {
if e.inner().size().map_or(false, |s| offset > s) { e.set_size(offset);
e.set_size(offset); }
}
},
_ => {},
} }
} }
/// Truncate file in current position. /// Truncate file in current position.
pub fn truncate(&mut self) -> io::Result<()> { pub fn truncate(&mut self) -> io::Result<()> {
match self.entry { if let Some(ref mut e) = self.entry {
Some(ref mut e) => { e.set_size(self.offset);
e.set_size(self.offset); if self.offset == 0 {
if self.offset == 0 { e.set_first_cluster(None, self.fs.fat_type());
e.set_first_cluster(None, self.fs.fat_type()); }
}
},
_ => {},
} }
if self.offset > 0 { if self.offset > 0 {
debug_assert!(self.current_cluster.is_some()); debug_assert!(self.current_cluster.is_some());
@ -60,11 +54,10 @@ impl <'a, 'b> File<'a, 'b> {
self.fs.cluster_iter(self.current_cluster.unwrap()).truncate() // SAFE self.fs.cluster_iter(self.current_cluster.unwrap()).truncate() // SAFE
} else { } else {
debug_assert!(self.current_cluster.is_none()); debug_assert!(self.current_cluster.is_none());
match self.first_cluster { if let Some(n) = self.first_cluster {
Some(n) => self.fs.cluster_iter(n).free()?, self.fs.cluster_iter(n).free()?;
_ => {}, self.first_cluster = None;
} }
self.first_cluster = None;
Ok(()) Ok(())
} }
} }
@ -84,9 +77,8 @@ impl <'a, 'b> File<'a, 'b> {
} }
pub(crate) fn flush_dir_entry(&mut self) -> io::Result<()> { pub(crate) fn flush_dir_entry(&mut self) -> io::Result<()> {
match self.entry { if let Some(ref mut e) = self.entry {
Some(ref mut e) => e.flush(self.fs)?, e.flush(self.fs)?;
_ => {},
} }
Ok(()) Ok(())
} }
@ -95,9 +87,8 @@ impl <'a, 'b> File<'a, 'b> {
/// ///
/// Note: if chrono feature is enabled (default) library automatically updates all timestamps /// Note: if chrono feature is enabled (default) library automatically updates all timestamps
pub fn set_created(&mut self, date_time: DateTime) { pub fn set_created(&mut self, date_time: DateTime) {
match self.entry { if let Some(ref mut e) = self.entry {
Some(ref mut e) => e.set_created(date_time), e.set_created(date_time);
_ => {},
} }
} }
@ -105,9 +96,8 @@ impl <'a, 'b> File<'a, 'b> {
/// ///
/// Note: if chrono feature is enabled (default) library automatically updates all timestamps /// Note: if chrono feature is enabled (default) library automatically updates all timestamps
pub fn set_accessed(&mut self, date: Date) { pub fn set_accessed(&mut self, date: Date) {
match self.entry { if let Some(ref mut e) = self.entry {
Some(ref mut e) => e.set_accessed(date), e.set_accessed(date);
_ => {},
} }
} }
@ -115,9 +105,8 @@ impl <'a, 'b> File<'a, 'b> {
/// ///
/// Note: if chrono feature is enabled (default) library automatically updates all timestamps /// Note: if chrono feature is enabled (default) library automatically updates all timestamps
pub fn set_modified(&mut self, date_time: DateTime) { pub fn set_modified(&mut self, date_time: DateTime) {
match self.entry { if let Some(ref mut e) = self.entry {
Some(ref mut e) => e.set_modified(date_time), e.set_modified(date_time);
_ => {},
} }
} }
@ -130,9 +119,8 @@ impl <'a, 'b> File<'a, 'b> {
fn set_first_cluster(&mut self, cluster: u32) { fn set_first_cluster(&mut self, cluster: u32) {
self.first_cluster = Some(cluster); self.first_cluster = Some(cluster);
match self.entry { if let Some(ref mut e) = self.entry {
Some(ref mut e) => e.set_first_cluster(self.first_cluster, self.fs.fat_type()), e.set_first_cluster(self.first_cluster, self.fs.fat_type());
None => {},
} }
} }
@ -143,9 +131,8 @@ impl <'a, 'b> File<'a, 'b> {
impl<'a, 'b> Drop for File<'a, 'b> { impl<'a, 'b> Drop for File<'a, 'b> {
fn drop(&mut self) { fn drop(&mut self) {
match self.flush() { if let Err(err) = self.flush() {
Err(err) => error!("flush failed {}", err), error!("flush failed {}", err);
_ => {},
} }
} }
} }

View File

@ -68,17 +68,16 @@ pub(crate) fn alloc_cluster(fat: &mut DiskSlice, fat_type: FatType, prev_cluster
let end_cluster = total_clusters + RESERVED_FAT_ENTRIES; let end_cluster = total_clusters + RESERVED_FAT_ENTRIES;
let start_cluster = match hint { let start_cluster = match hint {
Some(n) if n < end_cluster => n, Some(n) if n < end_cluster => n,
_ => 2, _ => RESERVED_FAT_ENTRIES,
}; };
let new_cluster = match find_free_cluster(fat, fat_type, start_cluster, end_cluster) { let new_cluster = match find_free_cluster(fat, fat_type, start_cluster, end_cluster) {
Ok(n) => n, Ok(n) => n,
Err(_) if start_cluster > 2 => find_free_cluster(fat, fat_type, 2, start_cluster)?, Err(_) if start_cluster > RESERVED_FAT_ENTRIES => find_free_cluster(fat, fat_type, RESERVED_FAT_ENTRIES, start_cluster)?,
Err(e) => return Err(e), Err(e) => return Err(e),
}; };
write_fat(fat, fat_type, new_cluster, FatValue::EndOfChain)?; write_fat(fat, fat_type, new_cluster, FatValue::EndOfChain)?;
match prev_cluster { if let Some(n) = prev_cluster {
Some(n) => write_fat(fat, fat_type, n, FatValue::Data(new_cluster))?, write_fat(fat, fat_type, n, FatValue::Data(new_cluster))?;
None => {},
} }
trace!("allocated cluster {}", new_cluster); trace!("allocated cluster {}", new_cluster);
Ok(new_cluster) Ok(new_cluster)
@ -187,7 +186,7 @@ impl FatTrait for Fat12 {
let mut cluster = RESERVED_FAT_ENTRIES; let mut cluster = RESERVED_FAT_ENTRIES;
fat.seek(io::SeekFrom::Start((cluster*3/2) as u64))?; fat.seek(io::SeekFrom::Start((cluster*3/2) as u64))?;
let mut prev_packed_val = 0u16; let mut prev_packed_val = 0u16;
loop { while cluster < end_cluster {
let res = match cluster & 1 { let res = match cluster & 1 {
0 => fat.read_u16::<LittleEndian>(), 0 => fat.read_u16::<LittleEndian>(),
_ => fat.read_u8().map(|n| n as u16), _ => fat.read_u8().map(|n| n as u16),
@ -206,9 +205,6 @@ impl FatTrait for Fat12 {
count += 1; count += 1;
} }
cluster += 1; cluster += 1;
if cluster >= end_cluster {
break;
}
} }
Ok(count) Ok(count)
} }
@ -245,23 +241,21 @@ impl FatTrait for Fat16 {
fn find_free(fat: &mut ReadSeek, start_cluster: u32, end_cluster: u32) -> io::Result<u32> { fn find_free(fat: &mut ReadSeek, start_cluster: u32, end_cluster: u32) -> io::Result<u32> {
let mut cluster = start_cluster; let mut cluster = start_cluster;
fat.seek(io::SeekFrom::Start((cluster*2) as u64))?; fat.seek(io::SeekFrom::Start((cluster*2) as u64))?;
loop { while cluster < end_cluster {
let val = fat.read_u16::<LittleEndian>()?; let val = fat.read_u16::<LittleEndian>()?;
if val == 0 { if val == 0 {
return Ok(cluster); return Ok(cluster);
} }
cluster += 1; cluster += 1;
if cluster == end_cluster {
return Err(io::Error::new(io::ErrorKind::Other, "end of FAT reached"));
}
} }
Err(io::Error::new(io::ErrorKind::Other, "end of FAT reached"))
} }
fn count_free(fat: &mut ReadSeek, end_cluster: u32) -> io::Result<u32> { fn count_free(fat: &mut ReadSeek, end_cluster: u32) -> io::Result<u32> {
let mut count = 0; let mut count = 0;
let mut cluster = RESERVED_FAT_ENTRIES; let mut cluster = RESERVED_FAT_ENTRIES;
fat.seek(io::SeekFrom::Start((cluster*2) as u64))?; fat.seek(io::SeekFrom::Start((cluster*2) as u64))?;
loop { while cluster < end_cluster {
match fat.read_u16::<LittleEndian>() { match fat.read_u16::<LittleEndian>() {
Err(ref err) if err.kind() == io::ErrorKind::UnexpectedEof => break, Err(ref err) if err.kind() == io::ErrorKind::UnexpectedEof => break,
Err(err) => return Err(err), Err(err) => return Err(err),
@ -269,9 +263,6 @@ impl FatTrait for Fat16 {
_ => {}, _ => {},
} }
cluster += 1; cluster += 1;
if cluster >= end_cluster {
break;
}
} }
Ok(count) Ok(count)
} }
@ -308,23 +299,21 @@ impl FatTrait for Fat32 {
fn find_free(fat: &mut ReadSeek, start_cluster: u32, end_cluster: u32) -> io::Result<u32> { fn find_free(fat: &mut ReadSeek, start_cluster: u32, end_cluster: u32) -> io::Result<u32> {
let mut cluster = start_cluster; let mut cluster = start_cluster;
fat.seek(io::SeekFrom::Start((cluster*4) as u64))?; fat.seek(io::SeekFrom::Start((cluster*4) as u64))?;
loop { while cluster < end_cluster {
let val = fat.read_u32::<LittleEndian>()? & 0x0FFFFFFF; let val = fat.read_u32::<LittleEndian>()? & 0x0FFFFFFF;
if val == 0 { if val == 0 {
return Ok(cluster); return Ok(cluster);
} }
cluster += 1; cluster += 1;
if cluster == end_cluster {
return Err(io::Error::new(io::ErrorKind::Other, "end of FAT reached"));
}
} }
Err(io::Error::new(io::ErrorKind::Other, "end of FAT reached"))
} }
fn count_free(fat: &mut ReadSeek, end_cluster: u32) -> io::Result<u32> { fn count_free(fat: &mut ReadSeek, end_cluster: u32) -> io::Result<u32> {
let mut count = 0; let mut count = 0;
let mut cluster = RESERVED_FAT_ENTRIES; let mut cluster = RESERVED_FAT_ENTRIES;
fat.seek(io::SeekFrom::Start((cluster*4) as u64))?; fat.seek(io::SeekFrom::Start((cluster*4) as u64))?;
loop { while cluster < end_cluster {
match fat.read_u32::<LittleEndian>() { match fat.read_u32::<LittleEndian>() {
Err(ref err) if err.kind() == io::ErrorKind::UnexpectedEof => break, Err(ref err) if err.kind() == io::ErrorKind::UnexpectedEof => break,
Err(err) => return Err(err), Err(err) => return Err(err),
@ -332,9 +321,6 @@ impl FatTrait for Fat32 {
_ => {}, _ => {},
} }
cluster += 1; cluster += 1;
if cluster >= end_cluster {
break;
}
} }
Ok(count) Ok(count)
} }
@ -373,13 +359,9 @@ impl <'a, 'b> ClusterIterator<'a, 'b> {
} }
pub(crate) fn free(&mut self) -> io::Result<()> { pub(crate) fn free(&mut self) -> io::Result<()> {
loop { while let Some(n) = self.cluster {
let prev = self.cluster;
self.next(); self.next();
match prev { write_fat(&mut self.fat, self.fat_type, n, FatValue::Free)?;
Some(n) => write_fat(&mut self.fat, self.fat_type, n, FatValue::Free)?,
None => break,
};
} }
Ok(()) Ok(())
} }
@ -392,21 +374,15 @@ impl <'a, 'b> Iterator for ClusterIterator<'a, 'b> {
if self.err { if self.err {
return None; return None;
} }
match self.cluster { if let Some(current_cluster) = self.cluster {
Some(current_cluster) => { self.cluster = match get_next_cluster(&mut self.fat, self.fat_type, current_cluster) {
self.cluster = match get_next_cluster(&mut self.fat, self.fat_type, current_cluster) { Ok(next_cluster) => next_cluster,
Ok(next_cluster) => next_cluster, Err(err) => {
Err(err) => { self.err = true;
self.err = true; return Some(Err(err));
return Some(Err(err)); },
}, }
}
},
None => {},
};
match self.cluster {
Some(n) => Some(Ok(n)),
None => None,
} }
self.cluster.map(|n| Ok(n))
} }
} }

View File

@ -130,9 +130,8 @@ impl<T: Read+Write+Seek> Seek for BufStream<T> {
impl<T: Read+Write+Seek> Drop for BufStream<T> { impl<T: Read+Write+Seek> Drop for BufStream<T> {
fn drop(&mut self) { fn drop(&mut self) {
match self.flush() { if let Err(err) = self.flush() {
Err(err) => error!("flush failed {}", err), error!("flush failed {}", err);
_ => {},
} }
} }
} }