Skip to content

Commit 77b45be

Browse files
committed
Simplify some casts
Avoid try_from ... unwrap. We can just use 'as', which is easier to understand.
1 parent a340b4d commit 77b45be

File tree

2 files changed

+8
-14
lines changed

2 files changed

+8
-14
lines changed

src/fat/volume.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -587,8 +587,8 @@ impl FatVolume {
587587
// Can quit early
588588
return Ok(());
589589
} else if dir_entry.is_valid() && !dir_entry.is_lfn() {
590-
// Safe, since Block::LEN always fits on a u32
591-
let start = u32::try_from(start).unwrap();
590+
// Block::LEN always fits on a u32
591+
let start = start as u32;
592592
let entry = dir_entry.get_entry(FatType::Fat16, block_idx, start);
593593
func(&entry);
594594
}
@@ -642,8 +642,8 @@ impl FatVolume {
642642
// Can quit early
643643
return Ok(());
644644
} else if dir_entry.is_valid() && !dir_entry.is_lfn() {
645-
// Safe, since Block::LEN always fits on a u32
646-
let start = u32::try_from(start).unwrap();
645+
// Block::LEN always fits on a u32
646+
let start = start as u32;
647647
let entry = dir_entry.get_entry(FatType::Fat32, block, start);
648648
func(&entry);
649649
}
@@ -769,8 +769,8 @@ impl FatVolume {
769769
break;
770770
} else if dir_entry.matches(match_name) {
771771
// Found it
772-
// Safe, since Block::LEN always fits on a u32
773-
let start = u32::try_from(start).unwrap();
772+
// Block::LEN always fits on a u32
773+
let start = start as u32;
774774
return Ok(dir_entry.get_entry(fat_type, block, start));
775775
}
776776
}

src/filesystem/directory.rs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
use core::convert::TryFrom;
2-
31
use crate::blockdevice::BlockIdx;
42
use crate::fat::{FatType, OnDiskDirEntry};
53
use crate::filesystem::{Attributes, ClusterId, Handle, ShortFileName, Timestamp};
@@ -262,16 +260,12 @@ impl DirEntry {
262260
[0u8; 2]
263261
} else {
264262
// Safe due to the AND operation
265-
u16::try_from((cluster_number >> 16) & 0x0000_FFFF)
266-
.unwrap()
267-
.to_le_bytes()
263+
(((cluster_number >> 16) & 0x0000_FFFF) as u16).to_le_bytes()
268264
};
269265
data[20..22].copy_from_slice(&cluster_hi[..]);
270266
data[22..26].copy_from_slice(&self.mtime.serialize_to_fat()[..]);
271267
// Safe due to the AND operation
272-
let cluster_lo = u16::try_from(cluster_number & 0x0000_FFFF)
273-
.unwrap()
274-
.to_le_bytes();
268+
let cluster_lo = ((cluster_number & 0x0000_FFFF) as u16).to_le_bytes();
275269
data[26..28].copy_from_slice(&cluster_lo[..]);
276270
data[28..32].copy_from_slice(&self.size.to_le_bytes()[..]);
277271
data

0 commit comments

Comments
 (0)