Skip to content

Commit ab20d98

Browse files
committed
Move cast_read_offset_usize to the input module
I don't know if it's _that_ much different. It's still a tiny function in the middle of much more complex stuff. But even the chunker's use is technically input-related, so... let's keep it here for now.
1 parent 995606f commit ab20d98

3 files changed

Lines changed: 24 additions & 22 deletions

File tree

src/input.rs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,7 @@ where
252252
/// Returns the number of bytes remaining to read from the captured prefix before consuming
253253
/// more from the source.
254254
fn captured_unread_size(&self) -> usize {
255-
let offset = crate::cast_read_offset_usize(self.prefix.position());
255+
let offset = cast_read_offset_usize(self.prefix.position());
256256
self.prefix.get_ref().len() - offset
257257
}
258258

@@ -335,6 +335,25 @@ where
335335
}
336336
}
337337

338+
/// Cast the offset of a memory-based [`io::Read`] to a [`usize`].
339+
///
340+
/// While `Read` APIs present offsets as `u64`s, any offset into a reader over an in-memory slice
341+
/// must naturally be representable in a width that covers every possible memory address.
342+
///
343+
/// # Panics
344+
///
345+
/// When debug assertions are enabled and `n` doesn't fit in a usize.
346+
/// This is a tradeoff between the relative efficiency and potential dangers of plain `as` casts.
347+
#[inline(always)]
348+
#[allow(clippy::cast_possible_truncation)]
349+
pub(crate) fn cast_read_offset_usize(n: u64) -> usize {
350+
if cfg!(debug_assertions) {
351+
usize::try_from(n).expect("reader offset should fit in a usize")
352+
} else {
353+
n as usize
354+
}
355+
}
356+
338357
#[cfg(test)]
339358
mod tests {
340359
use super::{CaptureReader, Handle, Input, Ref};

src/lib.rs

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -301,22 +301,3 @@ impl Format {
301301
Ok(None)
302302
}
303303
}
304-
305-
/// Cast the offset of a memory-based [`io::Read`] to a [`usize`].
306-
///
307-
/// While `Read` APIs present offsets as `u64`s, any offset into a reader over an in-memory slice
308-
/// must naturally be representable in a width that covers every possible memory address.
309-
///
310-
/// # Panics
311-
///
312-
/// When debug assertions are enabled and `n` doesn't fit in a usize.
313-
/// This is a tradeoff between the relative efficiency and potential dangers of plain `as` casts.
314-
#[inline(always)]
315-
#[allow(clippy::cast_possible_truncation)]
316-
fn cast_read_offset_usize(n: u64) -> usize {
317-
if cfg!(debug_assertions) {
318-
usize::try_from(n).expect("reader offset should fit in a usize")
319-
} else {
320-
n as usize
321-
}
322-
}

src/yaml/chunker.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ use std::mem;
1414

1515
mod parser;
1616

17+
use crate::input::cast_read_offset_usize;
18+
1719
use self::parser::{
1820
Parser, YAML_DOCUMENT_END_EVENT, YAML_DOCUMENT_START_EVENT, YAML_MAPPING_START_EVENT,
1921
YAML_SCALAR_EVENT, YAML_SEQUENCE_START_EVENT, YAML_STREAM_END_EVENT,
@@ -156,15 +158,15 @@ where
156158
/// Trims from the start of the capture buffer so the next chunk will begin at the specified
157159
/// reader offset.
158160
fn trim_to_offset(&mut self, offset: u64) {
159-
let trim_len = crate::cast_read_offset_usize(offset - self.captured_start_offset);
161+
let trim_len = cast_read_offset_usize(offset - self.captured_start_offset);
160162
self.captured_start_offset = offset;
161163
self.captured.drain(..trim_len);
162164
}
163165

164166
/// Takes the chunk from the start of the capture buffer up to the specified reader offset,
165167
/// leaving bytes beyond the offset in the capture buffer.
166168
fn take_to_offset(&mut self, offset: u64) -> Vec<u8> {
167-
let take_len = crate::cast_read_offset_usize(offset - self.captured_start_offset);
169+
let take_len = cast_read_offset_usize(offset - self.captured_start_offset);
168170
let tail = self.captured.split_off(take_len);
169171
self.captured_start_offset = offset;
170172
mem::replace(&mut self.captured, tail)

0 commit comments

Comments
 (0)