Skip to content

Commit 008ceeb

Browse files
wip
1 parent d3d3c37 commit 008ceeb

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
@@ -254,7 +254,7 @@ impl Ext4 {
254254
/// Read bytes into `dst`, starting at `start_byte`.
255255
fn read_bytes(
256256
&self,
257-
start_byte: u64,
257+
mut start_byte: u64,
258258
mut dst: &mut [u8],
259259
) -> Result<(), Ext4Error> {
260260
// The first 1024 bytes are reserved for non-filesystem
@@ -271,7 +271,7 @@ impl Ext4 {
271271
let block_index = self.0.journal.map_block_index(block_index);
272272
let offset_within_block = start_byte % block_size.to_u64();
273273

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

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

286-
let start_byte =
286+
let partial_start_byte =
287287
block_index * block_size.to_u64() + offset_within_block;
288288
self.0
289289
.reader
290290
.borrow_mut()
291-
.read(start_byte, partial_dst)
291+
.read(partial_start_byte, partial_dst)
292292
.map_err(Ext4Error::Io)?;
293293

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

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)