Skip to main content

FrameReader

Struct FrameReader 

Source
pub struct FrameReader { /* private fields */ }
Expand description

Reads frames into a buffer it keeps, so a stream of messages costs no allocation per frame once the buffer has grown to the biggest. The end rules are those of super::read_frame_bytes: a clean end only between frames; a stream cut inside a length or a frame is an error.

use substrate_data::io::{FrameReader, write_frame};

let mut pipe = Vec::new();
write_frame(&mut pipe, &7_u32)?;
write_frame(&mut pipe, &8_u32)?;
let (mut r, mut frames) = (pipe.as_slice(), FrameReader::new());
assert_eq!(frames.read::<u32>(&mut r)?, Some(7));
assert_eq!(frames.read::<u32>(&mut r)?, Some(8));
assert_eq!(frames.read::<u32>(&mut r)?, None);

Implementations§

Source§

impl FrameReader

Source

pub fn new() -> Self

A reader with an empty buffer.

Source

pub fn read_bytes(&mut self, r: &mut impl Read) -> Result<Option<&[u8]>>

The next frame’s bytes, valid until the next read; None where the stream ends between frames. The buffer grows as bytes arrive, never by a length the peer merely claims.

§Errors

The reader’s I/O error; io::ErrorKind::UnexpectedEof when the stream ends inside a length or a frame; io::ErrorKind::InvalidData for a length over super::MAX_FRAME.

Source

pub fn read<T: Schema>(&mut self, r: &mut impl Read) -> Result<Option<T>>

The next message; None where the stream ends between frames.

§Errors

The reader’s I/O error, or io::ErrorKind::InvalidData for a frame that isn’t a T.

Trait Implementations§

Source§

impl Debug for FrameReader

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for FrameReader

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.