Skip to main content

parse_frame

Function parse_frame 

Source
pub fn parse_frame(
    buf: &[u8],
    max: u32,
) -> Result<Option<(usize, usize)>, FrameError>
Expand description

Try to locate one complete length-prefixed frame at the front of buf without consuming it, returning (header_len, payload_len) — the byte counts of the varint prefix and the payload it announces. Returns Ok(None) when buf does not yet hold a complete frame (the varint is truncated, or fewer than payload_len payload bytes have arrived).

This is the synchronous, non-consuming counterpart to read_frame, for a caller that fills a byte buffer incrementally with cancellation-safe reads and must split frames off it after each top-up — e.g. the per-voucher reader’s read-ahead (#1486), which reads ahead under a timeout that read_frame (built on read_exact) could not survive without losing partial bytes. The caller slices the payload as buf[header_len..header_len + payload_len], decodes it with decode_message, then drains header_len + payload_len bytes.

The announced length is validated against max — the read’s frame ceiling — as soon as the varint is complete, before the caller waits for (or buffers) the payload. This mirrors read_frame’s pre-allocation denial-of-service bound.

§Errors

FrameError::Varint for a malformed length prefix (a non-terminating continuation run within the first 5 bytes, or a 5th byte overflowing u32); FrameError::TooLarge when the announced length exceeds max.