Trait musli_utils::reader::Reader

source ·
pub trait Reader<'de> {
    type Mut<'this>: Reader<'de>
       where Self: 'this;

    // Required methods
    fn borrow_mut(&mut self) -> Self::Mut<'_>;
    fn skip<C>(&mut self, cx: &C, n: usize) -> Result<(), C::Error>
       where C: ?Sized + Context;
    fn peek<C>(&mut self, cx: &C) -> Result<Option<u8>, C::Error>
       where C: ?Sized + Context;
    fn read_bytes<C, V>(
        &mut self,
        cx: &C,
        n: usize,
        visitor: V
    ) -> Result<V::Ok, C::Error>
       where C: ?Sized + Context,
             V: ValueVisitor<'de, C, [u8]>;

    // Provided methods
    fn read<C>(&mut self, cx: &C, buf: &mut [u8]) -> Result<(), C::Error>
       where C: ?Sized + Context { ... }
    fn read_byte<C>(&mut self, cx: &C) -> Result<u8, C::Error>
       where C: ?Sized + Context { ... }
    fn read_array<C, const N: usize>(
        &mut self,
        cx: &C
    ) -> Result<[u8; N], C::Error>
       where C: ?Sized + Context { ... }
    fn limit(self, limit: usize) -> Limit<Self>
       where Self: Sized { ... }
}
Expand description

Trait governing how a source of bytes is read.

This requires the reader to be able to hand out contiguous references to the byte source through Reader::read_bytes.

Required Associated Types§

source

type Mut<'this>: Reader<'de> where Self: 'this

Type borrowed from self.

Why oh why would we want to do this over having a simple &'this mut T?

We want to avoid recursive types, which will blow up the compiler. And the above is a typical example of when that can go wrong. This ensures that each call to borrow_mut dereferences the Reader at each step to avoid constructing a large muted type, like &mut &mut &mut SliceReader<'de>.

Required Methods§

source

fn borrow_mut(&mut self) -> Self::Mut<'_>

Borrow the current reader.

source

fn skip<C>(&mut self, cx: &C, n: usize) -> Result<(), C::Error>
where C: ?Sized + Context,

Skip over the given number of bytes.

source

fn peek<C>(&mut self, cx: &C) -> Result<Option<u8>, C::Error>
where C: ?Sized + Context,

Peek the next value.

source

fn read_bytes<C, V>( &mut self, cx: &C, n: usize, visitor: V ) -> Result<V::Ok, C::Error>
where C: ?Sized + Context, V: ValueVisitor<'de, C, [u8]>,

Read a slice out of the current reader.

Provided Methods§

source

fn read<C>(&mut self, cx: &C, buf: &mut [u8]) -> Result<(), C::Error>
where C: ?Sized + Context,

Read a slice into the given buffer.

source

fn read_byte<C>(&mut self, cx: &C) -> Result<u8, C::Error>
where C: ?Sized + Context,

Read a single byte.

source

fn read_array<C, const N: usize>(&mut self, cx: &C) -> Result<[u8; N], C::Error>
where C: ?Sized + Context,

Read an array out of the current reader.

source

fn limit(self, limit: usize) -> Limit<Self>
where Self: Sized,

Keep an accurate record of the position within the reader.

Object Safety§

This trait is not object safe.

Implementations on Foreign Types§

source§

impl<'de> Reader<'de> for &'de [u8]

§

type Mut<'this> = &'this mut &'de [u8] where Self: 'this

source§

fn borrow_mut(&mut self) -> Self::Mut<'_>

source§

fn skip<C>(&mut self, cx: &C, n: usize) -> Result<(), C::Error>
where C: ?Sized + Context,

source§

fn read<C>(&mut self, cx: &C, buf: &mut [u8]) -> Result<(), C::Error>
where C: ?Sized + Context,

source§

fn read_bytes<C, V>( &mut self, cx: &C, n: usize, visitor: V ) -> Result<V::Ok, C::Error>
where C: ?Sized + Context, V: ValueVisitor<'de, C, [u8]>,

source§

fn read_byte<C>(&mut self, cx: &C) -> Result<u8, C::Error>
where C: ?Sized + Context,

source§

fn read_array<C, const N: usize>(&mut self, cx: &C) -> Result<[u8; N], C::Error>
where C: ?Sized + Context,

source§

fn peek<C>(&mut self, _: &C) -> Result<Option<u8>, C::Error>
where C: ?Sized + Context,

source§

impl<'de, R> Reader<'de> for &mut R
where R: ?Sized + Reader<'de>,

§

type Mut<'this> = &'this mut R where Self: 'this

source§

fn borrow_mut(&mut self) -> Self::Mut<'_>

source§

fn skip<C>(&mut self, cx: &C, n: usize) -> Result<(), C::Error>
where C: ?Sized + Context,

source§

fn read_bytes<C, V>( &mut self, cx: &C, n: usize, visitor: V ) -> Result<V::Ok, C::Error>
where C: ?Sized + Context, V: ValueVisitor<'de, C, [u8]>,

source§

fn peek<C>(&mut self, cx: &C) -> Result<Option<u8>, C::Error>
where C: ?Sized + Context,

source§

fn read<C>(&mut self, cx: &C, buf: &mut [u8]) -> Result<(), C::Error>
where C: ?Sized + Context,

source§

fn read_byte<C>(&mut self, cx: &C) -> Result<u8, C::Error>
where C: ?Sized + Context,

source§

fn read_array<C, const N: usize>(&mut self, cx: &C) -> Result<[u8; N], C::Error>
where C: ?Sized + Context,

Implementors§

source§

impl<'de> Reader<'de> for SliceReader<'de>

§

type Mut<'this> = &'this mut SliceReader<'de> where Self: 'this

source§

impl<'de, R> Reader<'de> for Limit<R>
where R: Reader<'de>,

§

type Mut<'this> = &'this mut Limit<R> where Self: 'this