Skip to main content

FrameWriter

Struct FrameWriter 

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

Sends messages as frames without allocating per message: it keeps the buffers a message is written into, so once they have grown to the biggest message, a send only writes. Each frame (its length, then its body) goes out in one write_all, then the writer is flushed.

  • FrameWriter::send_packed sends the packed frame layout (crate::frame): allocation-free once warm for messages whose fields are plain values, strings, lists and records.
  • FrameWriter::send sends the named binary form (SDAT): also allocation-free once warm, except where a field writes through a crate::Stream, which builds its body apart.
use substrate_data::io::{FrameWriter, read_frame};

let mut sender = FrameWriter::new();
let mut pipe = Vec::new();
sender.send(&mut pipe, &"hello".to_string())?;
sender.send(&mut pipe, &"again".to_string())?;
assert_eq!(read_frame::<String>(&mut pipe.as_slice())?.as_deref(), Some("hello"));

Implementations§

Source§

impl FrameWriter

Source

pub fn new() -> FrameWriter

A writer with nothing grown yet: the first sends allocate.

Source

pub fn send<T>(&mut self, w: &mut impl Write, message: &T) -> Result<(), Error>
where T: Schema,

Sends message in the named binary form, and flushes.

§Errors

The writer’s I/O error; io::ErrorKind::InvalidInput for a frame over super::MAX_FRAME (nothing is written).

Source

pub fn send_packed<T>( &mut self, w: &mut impl Write, message: &T, ) -> Result<(), Error>
where T: Schema,

Sends message in the packed frame layout, and flushes.

§Errors

The writer’s I/O error; io::ErrorKind::InvalidInput for a frame over super::MAX_FRAME (nothing is written).

Trait Implementations§

Source§

impl Debug for FrameWriter

Source§

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

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

impl Default for FrameWriter

Source§

fn default() -> FrameWriter

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.