Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
nicholasbishop committed Jan 6, 2025
1 parent e7cfcd0 commit 3db9cf5
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 4 deletions.
9 changes: 5 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ impl Ext4 {
/// Read bytes into `dst`, starting at `start_byte`.
fn read_bytes(
&self,
start_byte: u64,
mut start_byte: u64,
mut dst: &mut [u8],
) -> Result<(), Ext4Error> {
// The first 1024 bytes are reserved for non-filesystem
Expand All @@ -268,7 +268,7 @@ impl Ext4 {
let block_index = self.0.journal.map_block_index(block_index);
let offset_within_block = start_byte % block_size.to_u64();

let read_len = {
let read_len: usize = {
// OK to unwrap: `offset_within_block` is always less
// than the block size, and the block size always fits
// in a `u32`. We assume a `usize` is always at least as
Expand All @@ -280,14 +280,15 @@ impl Ext4 {

let partial_dst = &mut dst[..read_len];

let start_byte =
let partial_start_byte =
block_index * block_size.to_u64() + offset_within_block;
self.0
.reader
.borrow_mut()
.read(start_byte, partial_dst)
.read(partial_start_byte, partial_dst)
.map_err(Ext4Error::Io)?;

start_byte += util::u64_from_usize(read_len);
dst = &mut dst[read_len..];
}

Expand Down
20 changes: 20 additions & 0 deletions src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,26 @@ pub(crate) const fn usize_from_u32(val: u32) -> usize {
}
}

/// Convert a `usize` to a `u64`.
///
/// TODO, but on platforms
/// supported by this crate, this conversion is infallible.
///
/// # Panics
///
/// Panics if `val` does not fit in this platform's `u64`.
#[inline]
#[must_use]
pub(crate) const fn u64_from_usize(val: usize) -> u64 {
assert!(size_of::<u64>() >= size_of::<usize>());

// Cannot use `usize::try_from` in a `const fn`.
#[expect(clippy::as_conversions)]
{
val as u64
}
}

/// Create a `u64` from two `u32` values.
#[inline]
#[must_use]
Expand Down

0 comments on commit 3db9cf5

Please sign in to comment.