Skip to main content

DecodeContext

Struct DecodeContext 

Source
pub struct DecodeContext<'a> { /* private fields */ }
Expand description

Per-decode limits threaded through every merge call.

Carries the remaining recursion depth and a shared unknown-field allowance. The context is Copy — passing it to a callee hands over the current depth by value, while the unknown-field allowance lives in a Cell owned by the top-level decode entry point, so every field decoded under one entry point draws from the same allowance.

Constructed automatically by the Message convenience methods (decode, decode_from_slice, merge_from_slice) and by DecodeOptions. Construct one manually only when calling Message::merge or the other depth-threading methods directly — and construct a fresh limit cell per top-level decode. Reusing one cell across decode calls makes the limit cumulative: each call drains it further until every decode fails with DecodeError::UnknownFieldLimitExceeded.

use core::cell::Cell;
use buffa::{DecodeContext, Message, DEFAULT_UNKNOWN_FIELD_LIMIT, RECURSION_LIMIT};

let limit = Cell::new(DEFAULT_UNKNOWN_FIELD_LIMIT);
let mut msg = Person::default();
msg.merge(&mut bytes, DecodeContext::new(RECURSION_LIMIT, &limit))?;

Implementations§

Source§

impl<'a> DecodeContext<'a>

Source

pub fn new(depth: u32, unknown_field_limit: &'a Cell<usize>) -> Self

Create a context with depth remaining recursion levels and the remaining unknown-field allowance stored in unknown_field_limit.

Source

pub fn depth(&self) -> u32

The remaining recursion depth.

Source

pub fn remaining_unknown_fields(&self) -> usize

The number of additional unknown fields this decode may materialize.

Source

pub fn descend(self) -> Result<Self, DecodeError>

Consume one level of recursion depth.

§Errors

Returns DecodeError::RecursionLimitExceeded when the depth budget is exhausted.

Source

pub fn register_unknown_field(&self) -> Result<(), DecodeError>

Consume one slot of the shared unknown-field allowance.

Call before materializing an UnknownField.

§Errors

Returns DecodeError::UnknownFieldLimitExceeded (leaving the allowance unchanged) when no slots remain.

Trait Implementations§

Source§

impl<'a> Clone for DecodeContext<'a>

Source§

fn clone(&self) -> DecodeContext<'a>

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<'a> Copy for DecodeContext<'a>

Source§

impl<'a> Debug for DecodeContext<'a>

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<'a> !RefUnwindSafe for DecodeContext<'a>

§

impl<'a> !Send for DecodeContext<'a>

§

impl<'a> !Sync for DecodeContext<'a>

§

impl<'a> !UnwindSafe for DecodeContext<'a>

§

impl<'a> Freeze for DecodeContext<'a>

§

impl<'a> Unpin for DecodeContext<'a>

§

impl<'a> UnsafeUnpin for DecodeContext<'a>

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.