pub trait Framer {
// Required methods
fn next_frame<'a>(&self, input: &'a [u8]) -> Result<Option<Frame<'a>>>;
fn write_frame(&self, payload: &[u8], out: &mut WriteBuf<'_>) -> Result<()>;
}Expand description
Strategy that extracts and emits one frame at a time.
Implementations are stateless: each call to Framer::next_frame is
independent. To process a stream, the caller advances their own read
position by Frame::consumed after each successful call.
§Example
use wire_codec::framing::{Framer, LengthPrefixed, LengthWidth, Endian};
let framer = LengthPrefixed::new(LengthWidth::U16, Endian::Big);
let input = &[0x00, 0x03, b'h', b'i', b'!'];
let frame = framer.next_frame(input).unwrap().unwrap();
assert_eq!(frame.payload(), b"hi!");
assert_eq!(frame.consumed(), 5);Required Methods§
Sourcefn next_frame<'a>(&self, input: &'a [u8]) -> Result<Option<Frame<'a>>>
fn next_frame<'a>(&self, input: &'a [u8]) -> Result<Option<Frame<'a>>>
Try to extract a single frame from the start of input.
Returns Ok(Some(frame)) when a complete frame is available,
Ok(None) when more bytes are needed, and Err(_) when the input
violates the protocol.
§Errors
Implementation-defined; see each concrete framer for its error contract.