Skip to main content

CompactSizeDecoder

Struct CompactSizeDecoder 

Source
pub struct CompactSizeDecoder { /* private fields */ }
Expand description

Decodes a compact size encoded integer as a length prefix.

The decoded value is returned as a usize and is bounded by a configurable limit (default: 4,000,000). This limit is a denial-of-service protection: a malicious peer can send a compact size value up to 2^64-1, and without a limit check the caller might attempt to allocate an enormous buffer based on that value. CompactSizeDecoder prevents this by rejecting values that exceed the limit before returning them to the caller.

If you are decoding an arbitrary u64 integer that is genuinely not a length prefix, use CompactSizeU64Decoder instead.

For more information about decoders see the documentation of the Decoder trait.

Implementations§

Source§

impl CompactSizeDecoder

Source

pub const fn new() -> Self

Constructs a new compact size decoder with the default 32MB length limit.

Source

pub const fn new_with_limit(limit: usize) -> Self

Constructs a new compact size decoder with a custom length limit.

The decoded value must not exceed limit, otherwise end will return an error. Use this when you know the field you are decoding has a tighter bound than the default limit of 32MB.

Trait Implementations§

Source§

impl Clone for CompactSizeDecoder

Source§

fn clone(&self) -> CompactSizeDecoder

Returns a duplicate of the value. Read more
1.0.0 · Source§

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

Performs copy-assignment from source. Read more
Source§

impl Debug for CompactSizeDecoder

Source§

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

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

impl Decoder for CompactSizeDecoder

Source§

type Output = usize

The type that this decoder produces when decoding is complete.
Source§

type Error = CompactSizeDecoderError

The error type that this decoder can produce.
Source§

fn push_bytes(&mut self, bytes: &mut &[u8]) -> Result<bool, Self::Error>

Pushes bytes into the decoder, consuming as much as possible. Read more
Source§

fn end(self) -> Result<Self::Output, Self::Error>

Completes the decoding process and return the final result. Read more
Source§

fn read_limit(&self) -> usize

Returns the maximum number of bytes this decoder can consume without over-reading. Read more
Source§

impl Default for CompactSizeDecoder

Source§

fn default() -> Self

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

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 = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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.