pub struct FrameDecoder { /* private fields */ }Expand description
Reassembles length-prefixed frames from a byte stream.
Implementations§
Source§impl FrameDecoder
impl FrameDecoder
pub fn new(max_frame: usize) -> Self
Sourcepub fn needed(&self) -> usize
pub fn needed(&self) -> usize
How many more bytes are needed before Self::next_frame can return.
This is what lets a reader size its read to the frame. A caller that reads into a small fixed buffer issues one syscall per chunk — for a 10 MiB fetch response and a 16 KiB buffer, more than six hundred of them — and copies every byte twice. Knowing the length prefix turns that into a handful of large reads.
Returns 4 while the prefix itself is incomplete, since that is what must be read before anything else can be known.
Sourcepub fn next_frame(&mut self) -> Result<Option<Bytes>>
pub fn next_frame(&mut self) -> Result<Option<Bytes>>
Take the next complete frame, if one has arrived.
The length prefix is consumed and not returned: every caller wants the body, and handing back the prefix invites double-counting it.
§Errors
Error::FrameTooLarge if the prefix exceeds the configured limit. The
check happens before reserving, which is the entire point of it.