Skip to content

Commit

Permalink
Simplify code by using min/max/clamp from Ord trait instead of core::cmp
Browse files Browse the repository at this point in the history
  • Loading branch information
rafalh committed Apr 21, 2024
1 parent 5138b8a commit 37562d6
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 20 deletions.
6 changes: 1 addition & 5 deletions src/boot_sector.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use core::cmp;
use core::slice;
use core::u16;
use core::u8;
Expand Down Expand Up @@ -514,10 +513,7 @@ fn determine_bytes_per_cluster(total_bytes: u64, bytes_per_sector: u16, fat_type
}
}
};
let bytes_per_cluster_clamped = cmp::min(
cmp::max(bytes_per_cluster, u32::from(bytes_per_sector)),
MAX_CLUSTER_SIZE,
);
let bytes_per_cluster_clamped = bytes_per_cluster.clamp(bytes_per_sector.into(), MAX_CLUSTER_SIZE);
debug_assert!(bytes_per_cluster_clamped.is_power_of_two());
bytes_per_cluster_clamped
}
Expand Down
9 changes: 4 additions & 5 deletions src/dir.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#[cfg(all(not(feature = "std"), feature = "alloc", feature = "lfn"))]
use alloc::vec::Vec;
use core::char;
use core::cmp;
use core::num;
use core::str;
#[cfg(feature = "lfn")]
Expand Down Expand Up @@ -1131,7 +1130,7 @@ impl ShortNameGenerator {

fn check_for_long_prefix_collision(&mut self, short_name: &[u8; SFN_SIZE]) {
// check for long prefix form collision (TEXTFI~1.TXT)
let long_prefix_len = cmp::min(self.basename_len, 6);
let long_prefix_len = 6.min(self.basename_len);
if short_name[long_prefix_len] != b'~' {
return;
}
Expand All @@ -1146,7 +1145,7 @@ impl ShortNameGenerator {

fn check_for_short_prefix_collision(&mut self, short_name: &[u8; SFN_SIZE]) {
// check for short prefix + checksum form collision (TE021F~1.TXT)
let short_prefix_len = cmp::min(self.basename_len, 2);
let short_prefix_len = 2.min(self.basename_len);
if short_name[short_prefix_len + 4] != b'~' {
return;
}
Expand Down Expand Up @@ -1205,12 +1204,12 @@ impl ShortNameGenerator {
fn build_prefixed_name(&self, num: u32, with_chksum: bool) -> [u8; SFN_SIZE] {
let mut buf = [SFN_PADDING; SFN_SIZE];
let prefix_len = if with_chksum {
let prefix_len = cmp::min(self.basename_len, 2);
let prefix_len = 2.min(self.basename_len);
buf[..prefix_len].copy_from_slice(&self.short_name[..prefix_len]);
buf[prefix_len..prefix_len + 4].copy_from_slice(&Self::u16_to_hex(self.chksum));
prefix_len + 4
} else {
let prefix_len = cmp::min(self.basename_len, 6);
let prefix_len = 6.min(self.basename_len);
buf[..prefix_len].copy_from_slice(&self.short_name[..prefix_len]);
prefix_len
};
Expand Down
6 changes: 2 additions & 4 deletions src/file.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use core::cmp;
use core::convert::TryFrom;

use crate::dir_entry::DirEntryEditor;
Expand Down Expand Up @@ -281,7 +280,7 @@ impl<IO: ReadWriteSeek, TP: TimeProvider, OCC> Read for File<'_, IO, TP, OCC> {
let offset_in_cluster = self.offset % cluster_size;
let bytes_left_in_cluster = (cluster_size - offset_in_cluster) as usize;
let bytes_left_in_file = self.bytes_left_in_file().unwrap_or(bytes_left_in_cluster);
let read_size = cmp::min(cmp::min(buf.len(), bytes_left_in_cluster), bytes_left_in_file);
let read_size = buf.len().min(bytes_left_in_cluster).min(bytes_left_in_file);
if read_size == 0 {
return Ok(0);
}
Expand Down Expand Up @@ -325,8 +324,7 @@ impl<IO: ReadWriteSeek, TP: TimeProvider, OCC> Write for File<'_, IO, TP, OCC> {
let offset_in_cluster = self.offset % cluster_size;
let bytes_left_in_cluster = (cluster_size - offset_in_cluster) as usize;
let bytes_left_until_max_file_size = (MAX_FILE_SIZE - self.offset) as usize;
let write_size = cmp::min(buf.len(), bytes_left_in_cluster);
let write_size = cmp::min(write_size, bytes_left_until_max_file_size);
let write_size = buf.len().min(bytes_left_in_cluster).min(bytes_left_until_max_file_size);
// Exit early if we are going to write no data
if write_size == 0 {
return Ok(0);
Expand Down
7 changes: 3 additions & 4 deletions src/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ use alloc::string::String;
use core::borrow::BorrowMut;
use core::cell::{Cell, RefCell};
use core::char;
use core::cmp;
use core::convert::TryFrom;
use core::fmt::Debug;
use core::marker::PhantomData;
Expand Down Expand Up @@ -805,7 +804,7 @@ impl<B, S: IoBase> IoBase for DiskSlice<B, S> {
impl<B: BorrowMut<S>, S: Read + Seek> Read for DiskSlice<B, S> {
fn read(&mut self, buf: &mut [u8]) -> Result<usize, Self::Error> {
let offset = self.begin + self.offset;
let read_size = cmp::min(self.size - self.offset, buf.len() as u64) as usize;
let read_size = (buf.len() as u64).min(self.size - self.offset) as usize;
self.inner.borrow_mut().seek(SeekFrom::Start(offset))?;
let size = self.inner.borrow_mut().read(&mut buf[..read_size])?;
self.offset += size as u64;
Expand All @@ -816,7 +815,7 @@ impl<B: BorrowMut<S>, S: Read + Seek> Read for DiskSlice<B, S> {
impl<B: BorrowMut<S>, S: Write + Seek> Write for DiskSlice<B, S> {
fn write(&mut self, buf: &[u8]) -> Result<usize, Self::Error> {
let offset = self.begin + self.offset;
let write_size = cmp::min(self.size - self.offset, buf.len() as u64) as usize;
let write_size = (buf.len() as u64).min(self.size - self.offset) as usize;
if write_size == 0 {
return Ok(0);
}
Expand Down Expand Up @@ -905,7 +904,7 @@ impl OemCpConverter for LossyOemCpConverter {
pub(crate) fn write_zeros<IO: ReadWriteSeek>(disk: &mut IO, mut len: u64) -> Result<(), IO::Error> {
const ZEROS: [u8; 512] = [0_u8; 512];
while len > 0 {
let write_size = cmp::min(len, ZEROS.len() as u64) as usize;
let write_size = len.min(ZEROS.len() as u64) as usize;
disk.write_all(&ZEROS[..write_size])?;
len -= write_size as u64;
}
Expand Down
3 changes: 1 addition & 2 deletions src/table.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use core::borrow::BorrowMut;
use core::cmp;
use core::marker::PhantomData;

use crate::error::{Error, IoError};
Expand Down Expand Up @@ -227,7 +226,7 @@ where
}
// mark special entries 0x0FFFFFF0 - 0x0FFFFFFF as BAD if they exists on FAT32 volume
if end_cluster > 0x0FFF_FFF0 {
let end_bad_cluster = cmp::min(0x0FFF_FFFF + 1, end_cluster);
let end_bad_cluster = (0x0FFF_FFFF + 1).min(end_cluster);
for cluster in 0x0FFF_FFF0..end_bad_cluster {
write_fat(fat, fat_type, cluster, FatValue::Bad)?;
}
Expand Down

0 comments on commit 37562d6

Please sign in to comment.