From 77b45be92c13c749f14a9cb9bd046d5dfd81d27a Mon Sep 17 00:00:00 2001 From: Jonathan 'theJPster' Pallant Date: Sun, 6 Oct 2024 20:33:10 +0100 Subject: [PATCH] Simplify some casts Avoid try_from ... unwrap. We can just use 'as', which is easier to understand. --- src/fat/volume.rs | 12 ++++++------ src/filesystem/directory.rs | 10 ++-------- 2 files changed, 8 insertions(+), 14 deletions(-) diff --git a/src/fat/volume.rs b/src/fat/volume.rs index ebab5b7..c453adb 100644 --- a/src/fat/volume.rs +++ b/src/fat/volume.rs @@ -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); } @@ -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); } @@ -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)); } } diff --git a/src/filesystem/directory.rs b/src/filesystem/directory.rs index c7ff0c9..efd6e5e 100644 --- a/src/filesystem/directory.rs +++ b/src/filesystem/directory.rs @@ -1,5 +1,3 @@ -use core::convert::TryFrom; - use crate::blockdevice::BlockIdx; use crate::fat::{FatType, OnDiskDirEntry}; use crate::filesystem::{Attributes, ClusterId, Handle, ShortFileName, Timestamp}; @@ -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