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§
Required Methods§
Sourcefn pb_read_chunk(&mut self) -> Result<&[u8], Self::Error>
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.
Sourcefn pb_advance(&mut self, bytes: usize)
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§
Sourcefn pb_read_exact(
&mut self,
buf: &mut [MaybeUninit<u8>],
) -> Result<usize, Self::Error>
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.