PbRead

Trait PbRead 

Source
pub trait PbRead {
    type Error;

    // Required methods
    fn pb_read_chunk(&mut self) -> Result<&[u8], Self::Error>;
    fn pb_advance(&mut self, bytes: usize);

    // Provided method
    fn pb_read_exact(
        &mut self,
        buf: &mut [MaybeUninit<u8>],
    ) -> Result<usize, Self::Error> { ... }
}
Expand description

A reader from which Protobuf data is read, similar to std::io::BufRead.

Like std::io::BufRead, this trait assumes that the reader uses an underlying buffer. PbDecoder uses this trait as the interface to decode incoming Protobuf messages.

Required Associated Types§

Source

type Error

I/O error returned on read failure.

Required Methods§

Source

fn pb_read_chunk(&mut self) -> Result<&[u8], Self::Error>

Returns the internal buffer, filling it with more data if necessary.

This call does not consume the underlying buffer, so calling it consecutively may yield the same contents. As such, this call must be followed by a pb_advance call with the number of bytes that are “consumed” from the returned buffer, to ensure that consumed bytes won’t be returned again.

Empty buffer is returned if and only if the underlying reader has reached EOF.

Source

fn pb_advance(&mut self, bytes: usize)

Consumes bytes from the underlying buffer.

This function should be called after pb_read_chunk. It advances the internal buffer by bytes so that those bytes won’t be returned from future calls to pb_read_chunk. This function doesn’t perform I/O, so it’s infallible.

The bytes should not exceed the length of the buffer returned from pb_read_chunk. Otherwise, the behaviour is implementation-defined.

Provided Methods§

Source

fn pb_read_exact( &mut self, buf: &mut [MaybeUninit<u8>], ) -> Result<usize, Self::Error>

Try to read exactly the number of bytes needed to fill buf.

Returns the number of bytes read, which will be at most the size of buf. If the return is less than buf, then the reader reached EOF before filling buf. This function will advance the reader by the amount of bytes read, so no need to call pb_advance.

Implementations on Foreign Types§

Source§

impl PbRead for &[u8]

Source§

type Error = Infallible

Source§

fn pb_read_chunk(&mut self) -> Result<&[u8], Self::Error>

Source§

fn pb_advance(&mut self, bytes: usize)

Source§

fn pb_read_exact( &mut self, buf: &mut [MaybeUninit<u8>], ) -> Result<usize, Self::Error>

Source§

impl<T: PbRead> PbRead for &mut T

Source§

type Error = <T as PbRead>::Error

Source§

fn pb_read_chunk(&mut self) -> Result<&[u8], Self::Error>

Source§

fn pb_advance(&mut self, bytes: usize)

Source§

fn pb_read_exact( &mut self, buf: &mut [MaybeUninit<u8>], ) -> Result<usize, Self::Error>

Implementors§

Source§

impl<R: BufRead> PbRead for StdReader<R>

Available on crate feature std only.