Skip to main content

JsonEncodeSession

Struct JsonEncodeSession 

Source
pub struct JsonEncodeSession<'a, R = JsonResource, Q = usize>{ /* private fields */ }
Available on crate feature json only.
Expand description

Mutable resource accounting for one JSON encoding operation.

Use Self::from_limits for a session that owns budgets created from immutable limits, or Self::borrowing_output when the caller owns the output and value budgets. Accepted output bytes are charged immediately; value measurements are staged until JsonEncodeAttempt::commit.

§Type Parameters

  • R - Caller-defined resource identity retained by limits and errors.
  • Q - Exact unsigned quantity used for measurements and accounting.

§Examples

use qubit_budget::json::JsonEncodeLimits;
use qubit_budget::json::JsonEncodeSession;
use qubit_budget::json::JsonMeasurement;

let limits = JsonEncodeLimits::builder()
    .max_output_bytes(4_usize)
    .max_nodes(1_usize)
    .build();
let mut session = JsonEncodeSession::from_limits(limits);
let mut attempt = session.begin_value();
attempt
    .try_consume_output_bytes(4)
    .expect("the output should fit");
attempt
    .try_admit(JsonMeasurement::Null { depth: 1 })
    .expect("the value should fit");
attempt.commit()?;
assert_eq!(session.output_budget().expect("output budget").used(), 4);

Implementations§

Source§

impl<'a, R, Q> JsonEncodeSession<'a, R, Q>
where R: Clone, Q: ResourceQuantity,

Source

pub fn borrowing_value(value: &'a mut JsonValueBudget<R, Q>) -> Self

Creates a session borrowing only a caller-owned value budget.

§Parameters
  • value - Caller-owned JSON value budget to update after a successful encode attempt.
§Returns

Creates a session borrowing only a caller-owned value budget.

Source

pub fn borrowing_output( output: &'a mut ResourceBudget<R, Q>, value: &'a mut JsonValueBudget<R, Q>, ) -> Self

Creates a session borrowing caller-owned output and value budgets.

§Parameters
  • output - Output supplied to this operation.
  • value - Caller-owned JSON value budget to update after a successful encode attempt.
§Returns

Creates a session borrowing caller-owned output and value budgets.

Source

pub fn begin_value(&mut self) -> JsonEncodeAttempt<'_, R, Q>

Starts accounting for one complete JSON value.

The returned attempt charges accepted output immediately, but publishes staged JSON value accounting only after a successful commit. A value-admission failure poisons commit; dropping the attempt rolls back only the staged value state.

§Returns

Starts accounting for one complete JSON value.

Source

pub fn output_budget(&self) -> Option<&ResourceBudget<R, Q>>

Returns the output budget when configured.

§Returns

Returns the output budget when configured.

None indicates that the corresponding limit or budget dimension is unconfigured.

Source

pub fn max_output_bytes(&self) -> Option<Q>

Returns the configured output-byte maximum.

§Returns

Returns the configured output-byte maximum.

None indicates that the corresponding limit or budget dimension is unconfigured.

Source

pub fn value_budget(&self) -> &JsonValueBudget<R, Q>

Returns the value budget for read-only inspection.

§Returns

Returns the value budget for read-only inspection.

Source§

impl<R, Q> JsonEncodeSession<'static, R, Q>
where R: Clone, Q: ResourceQuantity,

Source

pub fn from_limits(limits: JsonEncodeLimits<R, Q>) -> Self

Creates a session that owns budgets initialized from immutable limits.

§Parameters
  • limits - Immutable encoding limits used to initialize owned accounting budgets.
§Returns

Creates a session that owns budgets initialized from immutable limits.

Trait Implementations§

Source§

impl<'a, R: Debug, Q> Debug for JsonEncodeSession<'a, R, Q>

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<'a, R = JsonResource, Q = usize> !UnwindSafe for JsonEncodeSession<'a, R, Q>

§

impl<'a, R, Q> Freeze for JsonEncodeSession<'a, R, Q>
where EncodeStorage<'a, R, Q>: Freeze,

§

impl<'a, R, Q> RefUnwindSafe for JsonEncodeSession<'a, R, Q>
where EncodeStorage<'a, R, Q>: RefUnwindSafe,

§

impl<'a, R, Q> Send for JsonEncodeSession<'a, R, Q>
where EncodeStorage<'a, R, Q>: Send,

§

impl<'a, R, Q> Sync for JsonEncodeSession<'a, R, Q>
where EncodeStorage<'a, R, Q>: Sync,

§

impl<'a, R, Q> Unpin for JsonEncodeSession<'a, R, Q>
where EncodeStorage<'a, R, Q>: Unpin,

§

impl<'a, R, Q> UnsafeUnpin for JsonEncodeSession<'a, R, Q>
where EncodeStorage<'a, R, Q>: UnsafeUnpin,

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> 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, 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, <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.