Skip to main content

BlockingFrameReader

Struct BlockingFrameReader 

Source
pub struct BlockingFrameReader<R> { /* private fields */ }
Expand description

Blocking frame reader. Reads length-prefixed frames from any Read source.

Uses BufReader to amortize read syscalls — a single recv fills the buffer with many frames, so subsequent read_exact calls hit the buffer instead of making kernel transitions. This is critical for round-trip latency: without buffering, each frame requires 2 read syscalls (4-byte length prefix + payload).

Generic over the reader type so it works with both std::net::TcpStream and std::os::unix::net::UnixStream.

Implementations§

Source§

impl<R: Read> BlockingFrameReader<R>

Source

pub fn new(reader: R) -> Self

Source

pub fn read_frame(&mut self) -> Result<Option<&[u8]>>

Read the next complete frame into the internal buffer. Returns a borrowed slice of the frame payload, or None on clean disconnect. The slice is valid until the next read_frame() call.

A prefix declaring more than MAX_FRAME_SIZE is io::ErrorKind::InvalidData wrapping a FrameTooLarge (reachable through io::Error::get_ref); the stream has no frame boundary left to resume from.

Source

pub fn frame(&self) -> &[u8] ⓘ

The frame the last successful read_frame returned, for callers that decided what it was and now want it back without holding the borrow across that decision. Empty before the first read.

Source

pub fn get_ref(&self) -> &R

Borrow the underlying reader. Mirrors BufReader::get_ref — used by callers that need to reach the raw stream for socket-level configuration (set_read_timeout, set_nodelay, …) without going through the framed layer.

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.