Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion wincode/src/io/cursor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ where
self.pos = unsafe { self.pos.unchecked_add(amt) };
}

#[expect(deprecated)]
#[inline]
fn consume(&mut self, amt: usize) -> ReadResult<()> {
if self.cur_len() < amt {
return Err(read_size_limit(amt));
Expand Down
44 changes: 32 additions & 12 deletions wincode/src/io/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ pub trait Reader<'a> {
/// Return exactly `N` bytes as `&[u8; N]` without advancing.
///
/// Errors if fewer than `N` bytes are available.
#[deprecated(since = "0.4.6", note = "use `take_array`.")]
#[deprecated(since = "0.4.6", note = "use `peek_array`.")]
#[expect(deprecated)]
fn fill_array<const N: usize>(&mut self) -> ReadResult<&[u8; N]> {
let src = self.fill_exact(N)?;
Expand All @@ -99,6 +99,18 @@ pub trait Reader<'a> {
Ok(unsafe { &*src.as_ptr().cast::<[u8; N]>() })
}

/// Return exactly `N` bytes as `&[u8; N]` without advancing.
///
/// Errors if fewer than `N` bytes are available.
#[inline]
#[expect(deprecated)]
fn peek_array<const N: usize>(&mut self) -> ReadResult<&[u8; N]> {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So it's only a rename.
I'm thinking, maybe we should make both peek_byte and peek_array return value instead of borrowing?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For peek_byte, definitely makes sense to return by value since a byte is smaller than a pointer. But, for peek_array I think it's reasonable to let the caller decide whether to dereference based on N

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, sounds good.

let src = self.fill_exact(N)?;
// SAFETY:
// - `fill_exact` ensures we read N bytes.
Ok(unsafe { &*src.as_ptr().cast::<[u8; N]>() })
}

/// Return exactly `N` bytes as `[u8; N]` and advance by `N`.
///
/// Errors if fewer than `N` bytes are available.
Expand Down Expand Up @@ -202,17 +214,9 @@ pub trait Reader<'a> {
/// # Safety
///
/// - `amt` must be less than or equal to the number of bytes remaining in the reader.
#[deprecated(
since = "0.4.6",
note = "use one of the `take_*` methods, which automatically advance the reader."
)]
unsafe fn consume_unchecked(&mut self, amt: usize);

/// Advance the reader exactly `amt` bytes, returning an error if the source does not have enough bytes.
#[deprecated(
since = "0.4.6",
note = "use one of the `take_*` methods, which automatically advance the reader."
)]
fn consume(&mut self, amt: usize) -> ReadResult<()>;

/// Advance the parent by `n_bytes` and return a [`Reader`] that can elide bounds checks within
Expand Down Expand Up @@ -249,12 +253,20 @@ pub trait Reader<'a> {
///
/// May buffer more bytes if necessary. Errors if no bytes remain.
#[inline]
#[deprecated(since = "0.4.6", note = "use `take_byte`.")]
#[deprecated(since = "0.4.6", note = "use `peek_byte`.")]
#[expect(deprecated)]
fn peek(&mut self) -> ReadResult<&u8> {
self.fill_buf(1)?.first().ok_or_else(|| read_size_limit(1))
}

/// Get the next byte without advancing.
///
/// Errors if no bytes remain.
#[inline]
fn peek_byte(&mut self) -> ReadResult<u8> {
Ok(self.peek_array::<1>()?[0])
}

/// Get a mutable reference to the [`Reader`].
///
/// Useful in situations where one only has an `impl Reader<'de>` that
Expand Down Expand Up @@ -386,6 +398,11 @@ impl<'a, R: Reader<'a> + ?Sized> Reader<'a> for &mut R {
(*self).fill_array()
}

#[inline(always)]
fn peek_array<const N: usize>(&mut self) -> ReadResult<&[u8; N]> {
(*self).peek_array()
}

#[inline(always)]
fn take_array<const N: usize>(&mut self) -> ReadResult<[u8; N]> {
(*self).take_array()
Expand Down Expand Up @@ -424,13 +441,11 @@ impl<'a, R: Reader<'a> + ?Sized> Reader<'a> for &mut R {
}

#[inline(always)]
#[expect(deprecated)]
unsafe fn consume_unchecked(&mut self, amt: usize) {
(*self).consume_unchecked(amt)
}

#[inline(always)]
#[expect(deprecated)]
fn consume(&mut self, amt: usize) -> ReadResult<()> {
(*self).consume(amt)
}
Expand All @@ -446,6 +461,11 @@ impl<'a, R: Reader<'a> + ?Sized> Reader<'a> for &mut R {
(*self).peek()
}

#[inline(always)]
fn peek_byte(&mut self) -> ReadResult<u8> {
(*self).peek_byte()
}

#[inline(always)]
fn copy_into_slice(&mut self, dst: &mut [MaybeUninit<u8>]) -> ReadResult<()> {
(*self).copy_into_slice(dst)
Expand Down
3 changes: 0 additions & 3 deletions wincode/src/io/slice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,6 @@ impl<'a> Reader<'a> for TrustedSliceReaderZeroCopyMut<'a> {
}

#[inline]
#[expect(deprecated)]
fn consume(&mut self, amt: usize) -> ReadResult<()> {
unsafe { Self::consume_unchecked(self, amt) };
Ok(())
Expand Down Expand Up @@ -308,7 +307,6 @@ impl<'a> Reader<'a> for &'a [u8] {
}

#[inline]
#[expect(deprecated)]
fn consume(&mut self, amt: usize) -> ReadResult<()> {
if self.len() < amt {
return Err(read_size_limit(amt));
Expand Down Expand Up @@ -376,7 +374,6 @@ impl<'a> Reader<'a> for &'a mut [u8] {
}

#[inline]
#[expect(deprecated)]
fn consume(&mut self, amt: usize) -> ReadResult<()> {
if self.len() < amt {
return Err(read_size_limit(amt));
Expand Down
Loading