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>
impl<F: FnMut() -> u32> FramedReader<F>
Sourcepub fn new(read_word: F) -> Self
pub fn new(read_word: F) -> Self
Consume the leading length word and prepare to stream the payload.
Sourcepub fn payload_len(&self) -> usize
pub fn payload_len(&self) -> usize
Total framed payload length in bytes (from the leading length word).
Sourcepub fn remaining(&self) -> usize
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.
Sourcepub fn discard_rest_of_frame(&mut self)
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>
impl<F: FnMut() -> u32> FramedRead for FramedReader<F>
Source§fn read(&mut self, out: &mut [u8]) -> Result<(), EndOfFrame>
fn read(&mut self, out: &mut [u8]) -> Result<(), EndOfFrame>
out completely, or return EndOfFrame without advancing if the
frame does not hold that many more bytes.