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_packedsends the packed frame layout (crate::frame): allocation-free once warm for messages whose fields are plain values, strings, lists and records.FrameWriter::sendsends the named binary form (SDAT): also allocation-free once warm, except where a field writes through acrate::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
impl FrameWriter
Sourcepub fn new() -> FrameWriter
pub fn new() -> FrameWriter
A writer with nothing grown yet: the first sends allocate.
Sourcepub fn send<T>(&mut self, w: &mut impl Write, message: &T) -> Result<(), Error>where
T: Schema,
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).
Sourcepub fn send_packed<T>(
&mut self,
w: &mut impl Write,
message: &T,
) -> Result<(), Error>where
T: Schema,
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
impl Debug for FrameWriter
Source§impl Default for FrameWriter
impl Default for FrameWriter
Source§fn default() -> FrameWriter
fn default() -> FrameWriter
Returns the “default value” for a type. Read more
Auto Trait Implementations§
impl Freeze for FrameWriter
impl RefUnwindSafe for FrameWriter
impl Send for FrameWriter
impl Sync for FrameWriter
impl Unpin for FrameWriter
impl UnsafeUnpin for FrameWriter
impl UnwindSafe for FrameWriter
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more