From 380add131d6c4082c8530a32b65dbeae1a4dfea7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafa=C5=82=20Harabie=C5=84?= Date: Sun, 27 May 2018 18:37:33 +0200 Subject: [PATCH] Simplify code by using while and if let constructions --- src/dir.rs | 5 +--- src/file.rs | 63 +++++++++++++++++++----------------------------- src/table.rs | 68 +++++++++++++++++----------------------------------- src/utils.rs | 5 ++-- 4 files changed, 50 insertions(+), 91 deletions(-) diff --git a/src/dir.rs b/src/dir.rs index 66b4e78..d00c525 100644 --- a/src/dir.rs +++ b/src/dir.rs @@ -493,10 +493,7 @@ impl LongNameBuilder { fn truncate(&mut self) { // Truncate 0 and 0xFFFF characters from LFN buffer let mut lfn_len = self.buf.len(); - loop { - if lfn_len == 0 { - break; - } + while lfn_len > 0 { match self.buf[lfn_len-1] { 0xFFFF | 0 => lfn_len -= 1, _ => break, diff --git a/src/file.rs b/src/file.rs index 0176512..9ab94d9 100644 --- a/src/file.rs +++ b/src/file.rs @@ -32,27 +32,21 @@ impl <'a, 'b> File<'a, 'b> { fn update_size(&mut self) { let offset = self.offset; - match self.entry { - Some(ref mut e) => { - e.reset_modified(); - if e.inner().size().map_or(false, |s| offset > s) { - e.set_size(offset); - } - }, - _ => {}, + if let Some(ref mut e) = self.entry { + e.reset_modified(); + if e.inner().size().map_or(false, |s| offset > s) { + e.set_size(offset); + } } } /// Truncate file in current position. pub fn truncate(&mut self) -> io::Result<()> { - match self.entry { - Some(ref mut e) => { - e.set_size(self.offset); - if self.offset == 0 { - e.set_first_cluster(None, self.fs.fat_type()); - } - }, - _ => {}, + if let Some(ref mut e) = self.entry { + e.set_size(self.offset); + if self.offset == 0 { + e.set_first_cluster(None, self.fs.fat_type()); + } } if self.offset > 0 { 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 } else { debug_assert!(self.current_cluster.is_none()); - match self.first_cluster { - Some(n) => self.fs.cluster_iter(n).free()?, - _ => {}, + if let Some(n) = self.first_cluster { + self.fs.cluster_iter(n).free()?; + self.first_cluster = None; } - self.first_cluster = None; Ok(()) } } @@ -84,9 +77,8 @@ impl <'a, 'b> File<'a, 'b> { } pub(crate) fn flush_dir_entry(&mut self) -> io::Result<()> { - match self.entry { - Some(ref mut e) => e.flush(self.fs)?, - _ => {}, + if let Some(ref mut e) = self.entry { + e.flush(self.fs)?; } Ok(()) } @@ -95,9 +87,8 @@ impl <'a, 'b> File<'a, 'b> { /// /// Note: if chrono feature is enabled (default) library automatically updates all timestamps pub fn set_created(&mut self, date_time: DateTime) { - match self.entry { - Some(ref mut e) => e.set_created(date_time), - _ => {}, + if let Some(ref mut e) = self.entry { + 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 pub fn set_accessed(&mut self, date: Date) { - match self.entry { - Some(ref mut e) => e.set_accessed(date), - _ => {}, + if let Some(ref mut e) = self.entry { + 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 pub fn set_modified(&mut self, date_time: DateTime) { - match self.entry { - Some(ref mut e) => e.set_modified(date_time), - _ => {}, + if let Some(ref mut e) = self.entry { + e.set_modified(date_time); } } @@ -130,9 +119,8 @@ impl <'a, 'b> File<'a, 'b> { fn set_first_cluster(&mut self, cluster: u32) { self.first_cluster = Some(cluster); - match self.entry { - Some(ref mut e) => e.set_first_cluster(self.first_cluster, self.fs.fat_type()), - None => {}, + if let Some(ref mut e) = self.entry { + e.set_first_cluster(self.first_cluster, self.fs.fat_type()); } } @@ -143,9 +131,8 @@ impl <'a, 'b> File<'a, 'b> { impl<'a, 'b> Drop for File<'a, 'b> { fn drop(&mut self) { - match self.flush() { - Err(err) => error!("flush failed {}", err), - _ => {}, + if let Err(err) = self.flush() { + error!("flush failed {}", err); } } } diff --git a/src/table.rs b/src/table.rs index a3c856d..cdfa311 100644 --- a/src/table.rs +++ b/src/table.rs @@ -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 start_cluster = match hint { 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) { 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), }; write_fat(fat, fat_type, new_cluster, FatValue::EndOfChain)?; - match prev_cluster { - Some(n) => write_fat(fat, fat_type, n, FatValue::Data(new_cluster))?, - None => {}, + if let Some(n) = prev_cluster { + write_fat(fat, fat_type, n, FatValue::Data(new_cluster))?; } trace!("allocated cluster {}", new_cluster); Ok(new_cluster) @@ -187,7 +186,7 @@ impl FatTrait for Fat12 { let mut cluster = RESERVED_FAT_ENTRIES; fat.seek(io::SeekFrom::Start((cluster*3/2) as u64))?; let mut prev_packed_val = 0u16; - loop { + while cluster < end_cluster { let res = match cluster & 1 { 0 => fat.read_u16::(), _ => fat.read_u8().map(|n| n as u16), @@ -206,9 +205,6 @@ impl FatTrait for Fat12 { count += 1; } cluster += 1; - if cluster >= end_cluster { - break; - } } Ok(count) } @@ -245,23 +241,21 @@ impl FatTrait for Fat16 { fn find_free(fat: &mut ReadSeek, start_cluster: u32, end_cluster: u32) -> io::Result { let mut cluster = start_cluster; fat.seek(io::SeekFrom::Start((cluster*2) as u64))?; - loop { + while cluster < end_cluster { let val = fat.read_u16::()?; if val == 0 { return Ok(cluster); } 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 { let mut count = 0; let mut cluster = RESERVED_FAT_ENTRIES; fat.seek(io::SeekFrom::Start((cluster*2) as u64))?; - loop { + while cluster < end_cluster { match fat.read_u16::() { Err(ref err) if err.kind() == io::ErrorKind::UnexpectedEof => break, Err(err) => return Err(err), @@ -269,9 +263,6 @@ impl FatTrait for Fat16 { _ => {}, } cluster += 1; - if cluster >= end_cluster { - break; - } } Ok(count) } @@ -308,23 +299,21 @@ impl FatTrait for Fat32 { fn find_free(fat: &mut ReadSeek, start_cluster: u32, end_cluster: u32) -> io::Result { let mut cluster = start_cluster; fat.seek(io::SeekFrom::Start((cluster*4) as u64))?; - loop { + while cluster < end_cluster { let val = fat.read_u32::()? & 0x0FFFFFFF; if val == 0 { return Ok(cluster); } 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 { let mut count = 0; let mut cluster = RESERVED_FAT_ENTRIES; fat.seek(io::SeekFrom::Start((cluster*4) as u64))?; - loop { + while cluster < end_cluster { match fat.read_u32::() { Err(ref err) if err.kind() == io::ErrorKind::UnexpectedEof => break, Err(err) => return Err(err), @@ -332,9 +321,6 @@ impl FatTrait for Fat32 { _ => {}, } cluster += 1; - if cluster >= end_cluster { - break; - } } Ok(count) } @@ -373,13 +359,9 @@ impl <'a, 'b> ClusterIterator<'a, 'b> { } pub(crate) fn free(&mut self) -> io::Result<()> { - loop { - let prev = self.cluster; + while let Some(n) = self.cluster { self.next(); - match prev { - Some(n) => write_fat(&mut self.fat, self.fat_type, n, FatValue::Free)?, - None => break, - }; + write_fat(&mut self.fat, self.fat_type, n, FatValue::Free)?; } Ok(()) } @@ -392,21 +374,15 @@ impl <'a, 'b> Iterator for ClusterIterator<'a, 'b> { if self.err { return None; } - match self.cluster { - Some(current_cluster) => { - self.cluster = match get_next_cluster(&mut self.fat, self.fat_type, current_cluster) { - Ok(next_cluster) => next_cluster, - Err(err) => { - self.err = true; - return Some(Err(err)); - }, - } - }, - None => {}, - }; - match self.cluster { - Some(n) => Some(Ok(n)), - None => None, + if let Some(current_cluster) = self.cluster { + self.cluster = match get_next_cluster(&mut self.fat, self.fat_type, current_cluster) { + Ok(next_cluster) => next_cluster, + Err(err) => { + self.err = true; + return Some(Err(err)); + }, + } } + self.cluster.map(|n| Ok(n)) } } diff --git a/src/utils.rs b/src/utils.rs index c384844..dddb384 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -130,9 +130,8 @@ impl Seek for BufStream { impl Drop for BufStream { fn drop(&mut self) { - match self.flush() { - Err(err) => error!("flush failed {}", err), - _ => {}, + if let Err(err) = self.flush() { + error!("flush failed {}", err); } } }