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
impl FrameReader
Sourcepub fn read_bytes(&mut self, r: &mut impl Read) -> Result<Option<&[u8]>>
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 MAX_FRAME.
Trait Implementations§
Source§impl Debug for FrameReader
impl Debug for FrameReader
Auto Trait Implementations§
impl Freeze for FrameReader
impl RefUnwindSafe for FrameReader
impl Send for FrameReader
impl Sync for FrameReader
impl Unpin for FrameReader
impl UnsafeUnpin for FrameReader
impl UnwindSafe for FrameReader
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