Skip to content

Commit 3db9cf5

Browse files
wip
1 parent e7cfcd0 commit 3db9cf5

File tree

2 files changed

+25
-4
lines changed

2 files changed

+25
-4
lines changed

src/lib.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@ impl Ext4 {
251251
/// Read bytes into `dst`, starting at `start_byte`.
252252
fn read_bytes(
253253
&self,
254-
start_byte: u64,
254+
mut start_byte: u64,
255255
mut dst: &mut [u8],
256256
) -> Result<(), Ext4Error> {
257257
// The first 1024 bytes are reserved for non-filesystem
@@ -268,7 +268,7 @@ impl Ext4 {
268268
let block_index = self.0.journal.map_block_index(block_index);
269269
let offset_within_block = start_byte % block_size.to_u64();
270270

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

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

283-
let start_byte =
283+
let partial_start_byte =
284284
block_index * block_size.to_u64() + offset_within_block;
285285
self.0
286286
.reader
287287
.borrow_mut()
288-
.read(start_byte, partial_dst)
288+
.read(partial_start_byte, partial_dst)
289289
.map_err(Ext4Error::Io)?;
290290

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

src/util.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,26 @@ pub(crate) const fn usize_from_u32(val: u32) -> usize {
2828
}
2929
}
3030

31+
/// Convert a `usize` to a `u64`.
32+
///
33+
/// TODO, but on platforms
34+
/// supported by this crate, this conversion is infallible.
35+
///
36+
/// # Panics
37+
///
38+
/// Panics if `val` does not fit in this platform's `u64`.
39+
#[inline]
40+
#[must_use]
41+
pub(crate) const fn u64_from_usize(val: usize) -> u64 {
42+
assert!(size_of::<u64>() >= size_of::<usize>());
43+
44+
// Cannot use `usize::try_from` in a `const fn`.
45+
#[expect(clippy::as_conversions)]
46+
{
47+
val as u64
48+
}
49+
}
50+
3151
/// Create a `u64` from two `u32` values.
3252
#[inline]
3353
#[must_use]

0 commit comments

Comments
 (0)