Skip to main content

FramedReader

Struct FramedReader 

Source
pub struct FramedReader<F: FnMut() -> u32> { /* private fields */ }
Expand description

Streaming counterpart to read_framed_bytes_with: a FramedRead source that pulls framed words on demand instead of first materializing the whole payload into a Vec<u8>. Only a single word is buffered at a time, so it holds O(1) memory regardless of payload size — letting a decoder run at ~1x peak memory rather than the ~2x of “buffer the blob, then decode it”.

The word source must yield the frame length word first, then payload words, exactly as frame_words_from_bytes lays them out.

Implementations§

Source§

impl<F: FnMut() -> u32> FramedReader<F>

Source

pub fn new(read_word: F) -> Self

Consume the leading length word and prepare to stream the payload.

Source

pub fn payload_len(&self) -> usize

Total framed payload length in bytes (from the leading length word).

Source

pub fn remaining(&self) -> usize

Payload bytes not yet handed to the decoder; zero once fully consumed. A non-zero value after a successful decode means trailing bytes.

Source

pub fn discard_rest_of_frame(&mut self)

Pull and discard any payload words the decoder did not consume, leaving the source positioned at the next frame’s length word. Buffered/decoded data is not touched, so Self::remaining still reports trailing bytes.

Callers should invoke this before returning — success or failure — so a rejected frame does not desync a subsequent read, matching the buffered read_framed_bytes_with which always consumes the whole frame.

Cost is bounded by the frame’s length word: draining an honestly-framed frame reads only the words the source actually holds, but a frame whose length word is far larger than its real content will issue that many read_word calls. This is only reachable via a caller that recovers from an error and keeps reading; it is not a concern for honestly-framed input (as the guest’s is) where the length word reflects the payload.

Trait Implementations§

Source§

impl<F: FnMut() -> u32> FramedRead for FramedReader<F>

Source§

fn read(&mut self, out: &mut [u8]) -> Result<(), EndOfFrame>

Fill out completely, or return EndOfFrame without advancing if the frame does not hold that many more bytes.

Auto Trait Implementations§

§

impl<F> Freeze for FramedReader<F>
where F: Freeze,

§

impl<F> RefUnwindSafe for FramedReader<F>
where F: RefUnwindSafe,

§

impl<F> Send for FramedReader<F>
where F: Send,

§

impl<F> Sync for FramedReader<F>
where F: Sync,

§

impl<F> Unpin for FramedReader<F>
where F: Unpin,

§

impl<F> UnwindSafe for FramedReader<F>
where F: UnwindSafe,

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 = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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.