pub trait Reader<'a> {
type Trusted<'b>: Reader<'a>
where Self: 'b;
Show 14 methods
// Required methods
fn fill_buf(&mut self, n_bytes: usize) -> ReadResult<&[u8]>;
unsafe fn consume_unchecked(&mut self, amt: usize);
fn consume(&mut self, amt: usize) -> ReadResult<()>;
unsafe fn as_trusted_for(
&mut self,
n_bytes: usize,
) -> ReadResult<Self::Trusted<'_>>;
// Provided methods
fn fill_exact(&mut self, n_bytes: usize) -> ReadResult<&[u8]> { ... }
fn fill_array<const N: usize>(&mut self) -> ReadResult<&[u8; N]> { ... }
fn borrow_exact(&mut self, len: usize) -> ReadResult<&'a [u8]> { ... }
fn borrow_exact_mut(&mut self, len: usize) -> ReadResult<&'a mut [u8]> { ... }
fn peek(&mut self) -> ReadResult<&u8> { ... }
fn by_ref(&mut self) -> impl Reader<'a> { ... }
fn copy_into_slice(&mut self, dst: &mut [MaybeUninit<u8>]) -> ReadResult<()> { ... }
fn copy_into_array<const N: usize>(
&mut self,
dst: &mut MaybeUninit<[u8; N]>,
) -> ReadResult<()> { ... }
unsafe fn copy_into_t<T>(
&mut self,
dst: &mut MaybeUninit<T>,
) -> ReadResult<()> { ... }
unsafe fn copy_into_slice_t<T>(
&mut self,
dst: &mut [MaybeUninit<T>],
) -> ReadResult<()> { ... }
}Expand description
Trait for structured reading of bytes from a source into potentially uninitialized memory.
§Advancement semantics
fill_*methods never advance.copy_into_*andborrow_*methods advance by the number of bytes read.Reader::as_trusted_foradvances the parent by the number of bytes requested.
§Zero-copy semantics
Only implement Reader::borrow_exact for sources where stable borrows into the backing storage are possible.
Callers should prefer Reader::fill_exact to remain compatible with readers that don’t support zero-copy.
Returns ReadError::UnsupportedZeroCopy for readers that do not support zero-copy.
Required Associated Types§
Sourcetype Trusted<'b>: Reader<'a>
where
Self: 'b
type Trusted<'b>: Reader<'a> where Self: 'b
A variant of the Reader that can elide bounds checking within a given window.
Trusted variants of the Reader should generally not be constructed directly,
but rather by calling Reader::as_trusted_for on a trusted Reader.
This will ensure that the safety invariants are upheld.
Required Methods§
Sourcefn fill_buf(&mut self, n_bytes: usize) -> ReadResult<&[u8]>
fn fill_buf(&mut self, n_bytes: usize) -> ReadResult<&[u8]>
Return up to n_bytes from the internal buffer without advancing. Implementations may
read more data internally to satisfy future requests. Returns fewer than n_bytes at EOF.
This is not required to return exactly n_bytes, it is required to return up to n_bytes.
Use Reader::fill_exact if you need exactly n_bytes.
Sourceunsafe fn consume_unchecked(&mut self, amt: usize)
unsafe fn consume_unchecked(&mut self, amt: usize)
Advance by exactly amt bytes without bounds checks.
May panic if fewer than amt bytes remain.
§Safety
amtmust be less than or equal to the number of bytes remaining in the reader.
Sourcefn consume(&mut self, amt: usize) -> ReadResult<()>
fn consume(&mut self, amt: usize) -> ReadResult<()>
Advance the reader exactly amt bytes, returning an error if the source does not have enough bytes.
Sourceunsafe fn as_trusted_for(
&mut self,
n_bytes: usize,
) -> ReadResult<Self::Trusted<'_>>
unsafe fn as_trusted_for( &mut self, n_bytes: usize, ) -> ReadResult<Self::Trusted<'_>>
Advance the parent by n_bytes and return a Reader that can elide bounds checks within
that n_bytes window.
Implementors must:
- Ensure that either at least
n_bytesbytes are available backing the returned reader, or return an error. - Arrange that the returned
Trustedreader’s methods operate within thatn_byteswindow (it may buffer or prefetch arbitrarily).
Note:
as_trusted_foris intended for callers that know they will operate within a fixed-size window and want to avoid intermediate bounds checks.- If you simply want to advance the parent by
n_byteswithout using a trusted window, preferconsume(n_bytes)instead.
§Safety
The caller must ensure that, through the returned reader, they do not
cause more than n_bytes bytes to be logically read or consumed
without performing additional bounds checks.
Concretely:
- The total number of bytes accessed/consumed via the
Trustedreader (fill_*,copy_into_*,consume, etc.) must be <=n_bytes.
Violating this is undefined behavior, because Trusted readers are
permitted to elide bounds checks within the n_bytes window; reading past the
n_bytes window may read past the end of the underlying buffer.
Provided Methods§
Sourcefn fill_exact(&mut self, n_bytes: usize) -> ReadResult<&[u8]>
fn fill_exact(&mut self, n_bytes: usize) -> ReadResult<&[u8]>
Return exactly n_bytes without advancing.
Errors if the source cannot provide enough bytes.
Sourcefn fill_array<const N: usize>(&mut self) -> ReadResult<&[u8; N]>
fn fill_array<const N: usize>(&mut self) -> ReadResult<&[u8; N]>
Return exactly N bytes as &[u8; N] without advancing.
Errors if fewer than N bytes are available.
Sourcefn borrow_exact(&mut self, len: usize) -> ReadResult<&'a [u8]>
fn borrow_exact(&mut self, len: usize) -> ReadResult<&'a [u8]>
Zero-copy: return a borrowed slice of exactly len bytes and advance by len.
The returned slice is tied to 'a. Prefer Reader::fill_exact unless you truly need zero-copy.
Errors for readers that don’t support zero-copy.
Sourcefn borrow_exact_mut(&mut self, len: usize) -> ReadResult<&'a mut [u8]>
fn borrow_exact_mut(&mut self, len: usize) -> ReadResult<&'a mut [u8]>
Zero-copy: return a borrowed mutable slice of exactly len bytes and advance by len.
Errors for readers that don’t support zero-copy.
Sourcefn peek(&mut self) -> ReadResult<&u8>
fn peek(&mut self) -> ReadResult<&u8>
Return a reference to the next byte without advancing.
May buffer more bytes if necessary. Errors if no bytes remain.
Sourcefn by_ref(&mut self) -> impl Reader<'a>
fn by_ref(&mut self) -> impl Reader<'a>
Get a mutable reference to the Reader.
Useful in situations where one only has an impl Reader<'de> that
needs to be passed to mulitple functions requiring impl Reader<'de>.
Always prefer this over &mut reader to avoid recursive borrows.
struct FooBar {
foo: u32,
bar: u32,
}
unsafe impl<'de, C: Config> SchemaRead<'de, C> for FooBar {
type Dst = Self;
fn read(mut reader: impl Reader<'de>, dst: &mut MaybeUninit<Self>) -> ReadResult<()> {
// `reader.by_ref()`; Good ✅
let foo = <u32 as SchemaRead<'de, C>>::get(reader.by_ref())?;
let bar = <u32 as SchemaRead<'de, C>>::get(reader)?;
dst.write(FooBar { foo, bar });
Ok(())
}
}Sourcefn copy_into_slice(&mut self, dst: &mut [MaybeUninit<u8>]) -> ReadResult<()>
fn copy_into_slice(&mut self, dst: &mut [MaybeUninit<u8>]) -> ReadResult<()>
Sourcefn copy_into_array<const N: usize>(
&mut self,
dst: &mut MaybeUninit<[u8; N]>,
) -> ReadResult<()>
fn copy_into_array<const N: usize>( &mut self, dst: &mut MaybeUninit<[u8; N]>, ) -> ReadResult<()>
Sourceunsafe fn copy_into_t<T>(&mut self, dst: &mut MaybeUninit<T>) -> ReadResult<()>
unsafe fn copy_into_t<T>(&mut self, dst: &mut MaybeUninit<T>) -> ReadResult<()>
Sourceunsafe fn copy_into_slice_t<T>(
&mut self,
dst: &mut [MaybeUninit<T>],
) -> ReadResult<()>
unsafe fn copy_into_slice_t<T>( &mut self, dst: &mut [MaybeUninit<T>], ) -> ReadResult<()>
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.