Skip to content

Commit

Permalink
Simplify some casts
Browse files Browse the repository at this point in the history
Avoid try_from ... unwrap. We can just use 'as', which is easier to understand.
  • Loading branch information
thejpster committed Oct 6, 2024
1 parent f3bdb80 commit 959993b
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 14 deletions.
12 changes: 6 additions & 6 deletions src/fat/volume.rs
Original file line number Diff line number Diff line change
Expand Up @@ -587,8 +587,8 @@ impl FatVolume {
// Can quit early
return Ok(());
} else if dir_entry.is_valid() && !dir_entry.is_lfn() {
// Safe, since Block::LEN always fits on a u32
let start = u32::try_from(start).unwrap();
// Block::LEN always fits on a u32
let start = start as u32;
let entry = dir_entry.get_entry(FatType::Fat16, block_idx, start);
func(&entry);
}
Expand Down Expand Up @@ -642,8 +642,8 @@ impl FatVolume {
// Can quit early
return Ok(());
} else if dir_entry.is_valid() && !dir_entry.is_lfn() {
// Safe, since Block::LEN always fits on a u32
let start = u32::try_from(start).unwrap();
// Block::LEN always fits on a u32
let start = start as u32;
let entry = dir_entry.get_entry(FatType::Fat32, block, start);
func(&entry);
}
Expand Down Expand Up @@ -769,8 +769,8 @@ impl FatVolume {
break;
} else if dir_entry.matches(match_name) {
// Found it
// Safe, since Block::LEN always fits on a u32
let start = u32::try_from(start).unwrap();
// Block::LEN always fits on a u32
let start = start as u32;
return Ok(dir_entry.get_entry(fat_type, block, start));
}
}
Expand Down
10 changes: 2 additions & 8 deletions src/filesystem/directory.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use core::convert::TryFrom;

use crate::blockdevice::BlockIdx;
use crate::fat::{FatType, OnDiskDirEntry};
use crate::filesystem::{Attributes, ClusterId, SearchId, ShortFileName, Timestamp};
Expand Down Expand Up @@ -262,16 +260,12 @@ impl DirEntry {
[0u8; 2]
} else {
// Safe due to the AND operation
u16::try_from((cluster_number >> 16) & 0x0000_FFFF)
.unwrap()
.to_le_bytes()
(((cluster_number >> 16) & 0x0000_FFFF) as u16).to_le_bytes()
};
data[20..22].copy_from_slice(&cluster_hi[..]);
data[22..26].copy_from_slice(&self.mtime.serialize_to_fat()[..]);
// Safe due to the AND operation
let cluster_lo = u16::try_from(cluster_number & 0x0000_FFFF)
.unwrap()
.to_le_bytes();
let cluster_lo = ((cluster_number & 0x0000_FFFF) as u16).to_le_bytes();
data[26..28].copy_from_slice(&cluster_lo[..]);
data[28..32].copy_from_slice(&self.size.to_le_bytes()[..]);
data
Expand Down

0 comments on commit 959993b

Please sign in to comment.