Skip to main content

VarintPrefixed

Struct VarintPrefixed 

Source
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

Source

pub const DEFAULT_MAX_PAYLOAD: usize

Default payload limit: 8 MiB, matching LengthPrefixed::DEFAULT_MAX_PAYLOAD.

Source

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());
Source

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 })
));
Source

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");
Source

pub const fn max_payload(&self) -> usize

Configured maximum payload size in bytes.

Source

pub const fn is_canonical(&self) -> bool

Whether overlong prefixes are rejected.

Trait Implementations§

Source§

impl Clone for VarintPrefixed

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Copy for VarintPrefixed

Source§

impl Debug for VarintPrefixed

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for VarintPrefixed

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl Eq for VarintPrefixed

Source§

impl Framer for VarintPrefixed

Source§

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
Source§

fn write_frame(&self, payload: &[u8], out: &mut WriteBuf<'_>) -> Result<()>

Write the varint prefix and the payload.

§Errors

Nothing is written on error.

Source§

fn max_frame_len(&self, payload_len: usize) -> Option<usize>

Upper bound on the number of bytes Framer::write_frame writes for a payload of payload_len bytes. Read more
Source§

fn next_frame_from<'a>( &self, input: &'a [u8], scanned: &mut usize, ) -> Result<Option<Frame<'a>>>

Resumable variant of Framer::next_frame for streaming input. Read more
Source§

impl Hash for VarintPrefixed

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl PartialEq for VarintPrefixed

Source§

fn eq(&self, other: &Self) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl StructuralPartialEq for VarintPrefixed

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.