pub struct VarintPrefixed { /* private fields */ }Expand description
Varint length-prefixed framer.
Unlike the deprecated LengthPrefixed::new, this framer is bounded by
default: VarintPrefixed::new caps payloads
at VarintPrefixed::DEFAULT_MAX_PAYLOAD, and it rejects overlong
(non-canonical) prefixes so every frame has exactly one valid encoding.
Both can be relaxed.
§Example
use wire_codec::WriteBuf;
use wire_codec::framing::{Framer, VarintPrefixed};
let framer = VarintPrefixed::new();
let mut out = [0u8; 16];
let mut buf = WriteBuf::new(&mut out);
framer.write_frame(b"hello", &mut buf).unwrap();
assert_eq!(buf.written(), b"\x05hello");
let frame = framer.next_frame(&out[..6]).unwrap().unwrap();
assert_eq!(frame.payload(), b"hello");
assert_eq!(frame.consumed(), 6);Implementations§
Source§impl VarintPrefixed
impl VarintPrefixed
Sourcepub const DEFAULT_MAX_PAYLOAD: usize
pub const DEFAULT_MAX_PAYLOAD: usize
Default payload limit: 8 MiB, matching
LengthPrefixed::DEFAULT_MAX_PAYLOAD.
Sourcepub const fn new() -> Self
pub const fn new() -> Self
Build a framer with the default payload limit that rejects non-canonical prefixes.
§Example
use wire_codec::framing::VarintPrefixed;
let framer = VarintPrefixed::new();
assert_eq!(framer.max_payload(), VarintPrefixed::DEFAULT_MAX_PAYLOAD);
assert!(framer.is_canonical());Sourcepub const fn with_max_payload(self, max: usize) -> Self
pub const fn with_max_payload(self, max: usize) -> Self
Set the payload size limit enforced when reading and writing frames.
§Example
use wire_codec::framing::{Framer, VarintPrefixed};
use wire_codec::Error;
let framer = VarintPrefixed::new().with_max_payload(4);
assert!(matches!(
framer.next_frame(&[0x05, 1, 2, 3, 4, 5]),
Err(Error::FrameTooLarge { len: 5, limit: 4 })
));Sourcepub const fn with_canonical(self, canonical: bool) -> Self
pub const fn with_canonical(self, canonical: bool) -> Self
Choose whether overlong prefixes (such as [0x85, 0x00] for length 5)
are rejected with Error::InvalidEncoding (true, the default) or
accepted the way protobuf parsers do (false).
§Example
use wire_codec::framing::{Framer, VarintPrefixed};
use wire_codec::Error;
let overlong = [0x81, 0x00, b'x'];
assert_eq!(VarintPrefixed::new().next_frame(&overlong), Err(Error::InvalidEncoding));
let lenient = VarintPrefixed::new().with_canonical(false);
assert_eq!(lenient.next_frame(&overlong).unwrap().unwrap().payload(), b"x");Sourcepub const fn max_payload(&self) -> usize
pub const fn max_payload(&self) -> usize
Configured maximum payload size in bytes.
Sourcepub const fn is_canonical(&self) -> bool
pub const fn is_canonical(&self) -> bool
Whether overlong prefixes are rejected.
Trait Implementations§
Source§impl Clone for VarintPrefixed
impl Clone for VarintPrefixed
impl Copy for VarintPrefixed
Source§impl Debug for VarintPrefixed
impl Debug for VarintPrefixed
Source§impl Default for VarintPrefixed
impl Default for VarintPrefixed
impl Eq for VarintPrefixed
Source§impl Framer for VarintPrefixed
impl Framer for VarintPrefixed
Source§fn next_frame<'a>(&self, input: &'a [u8]) -> Result<Option<Frame<'a>>>
fn next_frame<'a>(&self, input: &'a [u8]) -> Result<Option<Frame<'a>>>
Extract one frame if the varint prefix and the full payload are present.
§Errors
Error::VarintOverflowif the prefix runs past ten bytes or overflowsu64.Error::InvalidEncodingif the prefix is overlong and the framer is canonical.Error::FrameTooLargeif the length exceeds the configured limit.
Source§fn write_frame(&self, payload: &[u8], out: &mut WriteBuf<'_>) -> Result<()>
fn write_frame(&self, payload: &[u8], out: &mut WriteBuf<'_>) -> Result<()>
Write the varint prefix and the payload.
§Errors
Error::FrameTooLargeif the payload exceeds the configured limit.Error::BufferFullifoutcannot hold the whole frame.
Nothing is written on error.
Source§fn max_frame_len(&self, payload_len: usize) -> Option<usize>
fn max_frame_len(&self, payload_len: usize) -> Option<usize>
Framer::write_frame writes for
a payload of payload_len bytes. Read moreSource§fn next_frame_from<'a>(
&self,
input: &'a [u8],
scanned: &mut usize,
) -> Result<Option<Frame<'a>>>
fn next_frame_from<'a>( &self, input: &'a [u8], scanned: &mut usize, ) -> Result<Option<Frame<'a>>>
Framer::next_frame for streaming input. Read more