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: 2 additions & 0 deletions wincode/src/io/cursor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,8 @@ impl<'a, T> Reader<'a> for Cursor<T>
where
T: AsRef<[u8]>,
{
const BORROW_KINDS: u8 = BorrowKind::CallSite.mask();

#[inline]
fn copy_into_slice(&mut self, dst: &mut [MaybeUninit<u8>]) -> ReadResult<()> {
let src = self.advance_slice_checked(dst.len())?;
Expand Down
41 changes: 40 additions & 1 deletion wincode/src/io/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ use {
thiserror::Error,
};

#[derive(Debug)]
#[derive(Debug, Clone, Copy)]
#[repr(u8)]
pub enum BorrowKind {
/// Borrowed from the call site, not extending past it.
CallSite,
Expand All @@ -18,6 +19,13 @@ pub enum BorrowKind {
BackingMut,
}

impl BorrowKind {
#[inline]
pub const fn mask(self) -> u8 {
1u8 << (self as u8)
}
}

impl core::fmt::Display for BorrowKind {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
match self {
Expand Down Expand Up @@ -62,6 +70,35 @@ pub(super) const fn transpose<const N: usize, T>(
/// compatible with readers that don't support borrowing, if possible.
/// - Returns [`ReadError::UnsupportedBorrow`] for readers that do not support borrowing.
pub trait Reader<'a> {
/// Borrow capabilities of this reader.
///
/// A bitmask of [`BorrowKind`] values indicating which kinds of borrows are supported.
///
/// Users of [`Reader`] can call [`Reader::supports_borrow`] to check if a borrow kind
/// is supported.
const BORROW_KINDS: u8 = 0;

/// Checks if this reader supports the given borrow kind.
///
/// # Examples
/// ```
/// # use wincode::io::{Reader, BorrowKind, Cursor};
/// #
/// let reader = [1, 2, 3, 4, 5];
/// assert!(reader.as_slice().supports_borrow(BorrowKind::Backing));
///
/// let mut reader = [1, 2, 3, 4, 5];
/// assert!(reader.as_mut_slice().supports_borrow(BorrowKind::BackingMut));
///
/// let reader = Cursor::new([1, 2, 3, 4, 5]);
/// assert!(reader.supports_borrow(BorrowKind::CallSite));
/// assert!(!reader.supports_borrow(BorrowKind::Backing));
/// ```
#[inline]
fn supports_borrow(&self, kind: BorrowKind) -> bool {
Copy link
Contributor

Choose a reason for hiding this comment

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

can we make it const?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Unfortunately not -- const functions in traits isn't yet supported. But, I looked at the assembly in a few examples, and the compiler reliably constant-folds conditions out since BORROW_CAP and kind.mask is const

Self::BORROW_KINDS & kind.mask() != 0
}

/// Return exactly `N` bytes as `&[u8; N]` without advancing.
///
/// Errors if fewer than `N` bytes are available.
Expand Down Expand Up @@ -270,6 +307,8 @@ pub trait Reader<'a> {
}

impl<'a, R: Reader<'a> + ?Sized> Reader<'a> for &mut R {
const BORROW_KINDS: u8 = R::BORROW_KINDS;

#[inline(always)]
fn by_ref(&mut self) -> impl Reader<'a> {
&mut **self
Expand Down
12 changes: 12 additions & 0 deletions wincode/src/io/slice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ impl<'a, T> SliceUnchecked<'a, T> {
}

impl<'a> Reader<'a> for SliceUnchecked<'a, u8> {
const BORROW_KINDS: u8 = BorrowKind::Backing.mask() | BorrowKind::CallSite.mask();

#[inline]
fn copy_into_slice(&mut self, buf: &mut [MaybeUninit<u8>]) -> ReadResult<()> {
// SAFETY: by constructing `SliceUnchecked`, caller guarantees
Expand Down Expand Up @@ -171,6 +173,9 @@ impl<'a, T> SliceMutUnchecked<'a, T> {
}

impl<'a> Reader<'a> for SliceMutUnchecked<'a, u8> {
const BORROW_KINDS: u8 =
BorrowKind::Backing.mask() | BorrowKind::BackingMut.mask() | BorrowKind::CallSite.mask();

#[inline]
fn copy_into_slice(&mut self, buf: &mut [MaybeUninit<u8>]) -> ReadResult<()> {
// SAFETY: by constructing `SliceMutUnchecked`, caller guarantees
Expand Down Expand Up @@ -260,6 +265,8 @@ impl<'b, T> SliceScopedUnchecked<'_, 'b, T> {
}

impl<'a> Reader<'a> for SliceScopedUnchecked<'a, '_, u8> {
const BORROW_KINDS: u8 = BorrowKind::CallSite.mask();

#[inline(always)]
fn copy_into_slice(&mut self, buf: &mut [MaybeUninit<u8>]) -> ReadResult<()> {
self.inner.copy_into_slice(buf)
Expand Down Expand Up @@ -292,6 +299,8 @@ impl<'a> Reader<'a> for SliceScopedUnchecked<'a, '_, u8> {
}

impl<'a> Reader<'a> for &'a [u8] {
const BORROW_KINDS: u8 = BorrowKind::Backing.mask() | BorrowKind::CallSite.mask();

#[inline]
fn take_borrowed(&mut self, len: usize) -> ReadResult<&'a [u8]> {
let Some(src) = advance_slice_checked(self, len) else {
Expand Down Expand Up @@ -357,6 +366,9 @@ impl<'a> Reader<'a> for &'a [u8] {
}

impl<'a> Reader<'a> for &'a mut [u8] {
const BORROW_KINDS: u8 =
BorrowKind::Backing.mask() | BorrowKind::BackingMut.mask() | BorrowKind::CallSite.mask();

#[inline(always)]
unsafe fn as_trusted_for(&mut self, n_bytes: usize) -> ReadResult<impl Reader<'a>> {
let Some(window) = advance_slice_mut_checked(self, n_bytes) else {
Expand Down