Skip to main content

DecodeOptions

Struct DecodeOptions 

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

Options for configuring message decoding behavior.

Use this to set custom recursion depth limits or maximum message sizes when decoding from untrusted input.

§Examples

use buffa::DecodeOptions;

// Restrict recursion depth to 50 and message size to 1 MiB:
let msg: Person = DecodeOptions::new()
    .with_recursion_limit(50)
    .with_max_message_size(1024 * 1024)
    .decode_from_slice(bytes)?;

Implementations§

Source§

impl DecodeOptions

Source

pub fn new() -> Self

Create new decode options with defaults.

Defaults:

  • recursion_limit: 100 (same as RECURSION_LIMIT)
  • max_message_size: 2 GiB - 1
Source

pub fn with_recursion_limit(self, limit: u32) -> Self

Set the maximum recursion depth for nested messages.

Each nested sub-message consumes one level of depth budget. When the budget reaches zero, decoding returns DecodeError::RecursionLimitExceeded.

Default: 100.

Source

pub fn with_max_message_size(self, max_bytes: usize) -> Self

Set the maximum total message size in bytes.

If the input buffer or length-delimited payload exceeds this size, decoding returns DecodeError::MessageTooLarge.

This is checked at the top-level decode entry point. Individual sub-messages are still bounded by the internal 2 GiB limit regardless of this setting.

Default: 2 GiB - 1 (0x7FFF_FFFF).

Source

pub fn recursion_limit(&self) -> u32

Returns the configured recursion depth limit.

Source

pub fn max_message_size(&self) -> usize

Returns the configured maximum message size in bytes.

Source

pub fn decode<M: Message>(&self, buf: &mut impl Buf) -> Result<M, DecodeError>

Decode a message from a buffer.

Source

pub fn decode_from_slice<M: Message>( &self, data: &[u8], ) -> Result<M, DecodeError>

Decode a message from a byte slice.

Source

pub fn decode_length_delimited<M: Message>( &self, buf: &mut impl Buf, ) -> Result<M, DecodeError>

Decode a length-delimited message from a buffer.

Source

pub fn merge<M: Message>( &self, msg: &mut M, buf: &mut impl Buf, ) -> Result<(), DecodeError>

Merge fields from a buffer into an existing message.

Source

pub fn merge_from_slice<M: Message>( &self, msg: &mut M, data: &[u8], ) -> Result<(), DecodeError>

Merge fields from a byte slice into an existing message.

Source

pub fn decode_view<'a, V: MessageView<'a>>( &self, buf: &'a [u8], ) -> Result<V, DecodeError>

Decode a zero-copy view from a byte slice.

Source

pub fn decode_reader<M: Message>( &self, reader: &mut impl Read, ) -> Result<M, Error>

Available on crate feature std only.

Decode a message by reading all bytes from a std::io::Read source.

Reads until EOF, enforces max_message_size, then decodes the buffered bytes. Returns std::io::Error to be compatible with Read-based error handling.

Source

pub fn decode_length_delimited_reader<M: Message>( &self, reader: &mut impl Read, ) -> Result<M, Error>

Available on crate feature std only.

Decode a length-delimited message from a std::io::Read source.

Reads a varint length prefix, enforces max_message_size, reads exactly that many bytes, then decodes. Useful for reading sequential length-delimited messages from a file or stream.

Trait Implementations§

Source§

impl Clone for DecodeOptions

Source§

fn clone(&self) -> DecodeOptions

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 DecodeOptions

Source§

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

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

impl Default for DecodeOptions

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.