Skip to main content

ResourceBudget

Struct ResourceBudget 

Source
pub struct ResourceBudget<R, Q = u64>{ /* private fields */ }
Expand description

A finite, non-releasable resource budget.

The budget stores remaining capacity and only subtracts after a request has been checked. Therefore used() is computed as limit - remaining and no cumulative addition can overflow. Callers represent an unconfigured dimension with Option<ResourceBudget<R>> = None rather than constructing an unlimited object.

§Type Parameters

  • R - Caller-defined resource value retained for diagnostics.
  • Q - Exact unsigned quantity used for the limit and accounting.

A budget is intentionally not cloneable because cloning would create a second independently consumable copy of the same finite allowance.

§Examples

use qubit_budget::ResourceBudget;

let mut budget = ResourceBudget::new("response bytes", 8_u64);
budget.try_consume(3).expect("three bytes should fit");
assert_eq!(budget.used(), 3);
assert_eq!(budget.remaining(), 5);
use qubit_budget::ResourceBudget;
let budget = ResourceBudget::new("bytes", 8_u64);
let _copy = budget.clone();

Implementations§

Source§

impl<R, Q> ResourceBudget<R, Q>
where R: Clone + Debug, Q: ResourceQuantity,

Source

pub fn try_write_string<E, F>( &mut self, render: F, ) -> Result<String, BudgetedStringError<R, E, Q>>
where E: Debug + Display, F: FnOnce(&mut BudgetedStringWriter<'_, R, Q>) -> Result<(), E>,

Renders and transactionally commits a UTF-8 string under this budget.

§Type Parameters
  • E - Error type returned by the caller-provided renderer.
  • F - Closure used to render the output string.
§Parameters
  • render - Caller-provided renderer writing into the transactional adapter.
§Returns

Ok(rendered) after the complete UTF-8 output is charged and committed.

§Errors

Returns BudgetedStringError when rendering, allocation, UTF-8 validation, measurement, or budget accounting fails.

Source§

impl<R, Q> ResourceBudget<R, Q>

Source

pub const fn new(resource: R, limit: Q) -> Self

Creates a zero-used finite budget.

§Parameters
  • resource - Domain resource value retained in errors.
  • limit - Finite maximum for this budget.
§Returns

A budget whose remaining capacity equals limit.

§Examples
use qubit_budget::ResourceBudget;

let mut budget = ResourceBudget::new("bytes", 8_u64);
budget.try_consume(3).expect("three bytes should fit");
assert_eq!(budget.used(), 3);
assert_eq!(budget.remaining(), 5);
Source

pub fn from_limit(limit: ResourceLimit<R, Q>) -> Self

Creates a zero-used budget from an immutable resource limit.

§Parameters
  • limit - Resource identity and finite maximum for this budget.
§Returns

A budget whose remaining capacity equals the limit maximum.

Source

pub fn check_available( &self, amount: Q, ) -> Result<(), InsufficientBudgetError<R, Q>>
where R: Clone,

Checks whether a complete consumption would fit.

§Parameters
  • amount - Quantity that would be consumed.
§Returns

Ok(()) when amount <= remaining; otherwise returns InsufficientBudgetError containing the resource, limit and pre-failure balance. This method never changes the budget.

§Errors

Returns InsufficientBudgetError when amount exceeds the remaining capacity.

Source

pub fn try_consume( &mut self, amount: Q, ) -> Result<(), InsufficientBudgetError<R, Q>>
where R: Clone,

Consumes an amount atomically when it fits.

§Parameters
  • amount - Quantity to consume.
§Returns

Ok(()) after subtracting the amount, or a structured error while leaving remaining unchanged when the amount does not fit.

§Errors

Returns InsufficientBudgetError when amount exceeds the remaining capacity. The budget remains unchanged in that case.

Source

pub fn check_available_usize( &self, amount: usize, ) -> Result<(), MeasuredBudgetError<R, Q>>
where R: Clone,

Checks a machine-sized consumption request without truncating it.

§Parameters
  • amount - Native quantity to convert and check.
§Returns

Ok(()) when the converted request fits the remaining capacity.

§Errors

Returns MeasuredBudgetError::Quantity when amount cannot be represented by Q, or MeasuredBudgetError::Budget when it exceeds the remaining capacity. The budget is unchanged on either failure.

Source

pub fn check_available_u64( &self, amount: u64, ) -> Result<(), MeasuredBudgetError<R, Q>>
where R: Clone,

Checks a 64-bit consumption request without truncating it.

§Parameters
  • amount - 64-bit quantity to convert and check.
§Returns

Ok(()) when the converted request fits the remaining capacity.

§Errors

Returns MeasuredBudgetError::Quantity when amount cannot be represented by Q, or MeasuredBudgetError::Budget when it exceeds the remaining capacity. The budget is unchanged on either failure.

Source

pub fn try_consume_usize( &mut self, amount: usize, ) -> Result<(), MeasuredBudgetError<R, Q>>
where R: Clone,

Consumes a machine-sized quantity without truncating it.

§Parameters
  • amount - Native quantity to convert and consume.
§Returns

Ok(()) after the converted quantity is consumed.

§Errors

Returns MeasuredBudgetError::Quantity when amount cannot be represented by Q, or MeasuredBudgetError::Budget when it exceeds the remaining capacity. The budget is unchanged on either failure.

Source

pub fn try_consume_u64( &mut self, amount: u64, ) -> Result<(), MeasuredBudgetError<R, Q>>
where R: Clone,

Consumes a 64-bit quantity without truncating it.

§Parameters
  • amount - 64-bit quantity to convert and consume.
§Returns

Ok(()) after the converted quantity is consumed.

§Errors

Returns MeasuredBudgetError::Quantity when amount cannot be represented by Q, or MeasuredBudgetError::Budget when it exceeds the remaining capacity. The budget is unchanged on either failure.

Source

pub fn try_consume_group( budgets: &mut [&mut Self], amount: Q, ) -> Result<(), BudgetGroupError<R, Q>>
where R: Clone,

Atomically consumes the same amount from every budget in a group.

Every member is checked before any member is changed. This is useful when one operation must count against both a local and a shared budget.

§Parameters
  • budgets - Ordered group of budgets that must all accept the charge.
  • amount - Quantity to consume from each budget.
§Returns

Ok(()) after every budget is charged, or a BudgetGroupError identifying the first rejecting member. Failure leaves every budget unchanged.

§Errors

Returns BudgetGroupError when any member has insufficient remaining capacity.

Source

pub fn consume_available(&mut self, requested: Q) -> Q

Consumes as much of a request as remains available.

§Parameters
  • requested - Maximum quantity the caller wants to consume.
§Returns

The exact consumed quantity, equal to min(requested, remaining). This operation always succeeds and never increases the balance.

Source

pub const fn resource(&self) -> &R

Returns the associated resource.

§Returns

Returns the associated resource.

Source

pub const fn resource_limit(&self) -> &ResourceLimit<R, Q>

Returns the immutable resource limit that configures this budget.

§Returns

Returns the immutable resource limit that configures this budget.

Source

pub const fn limit(&self) -> Q

Returns the finite limit.

§Returns

Returns the finite limit.

Source

pub const fn remaining(&self) -> Q

Returns remaining capacity.

§Returns

Returns remaining capacity.

Source

pub fn used(&self) -> Q

Returns the quantity consumed so far.

§Returns

Returns the quantity consumed so far.

Trait Implementations§

Source§

impl<R: Debug, Q> Debug for ResourceBudget<R, Q>

Source§

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

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

impl<R: Eq, Q> Eq for ResourceBudget<R, Q>
where Q: ResourceQuantity + Eq,

Source§

impl<R: PartialEq, Q> PartialEq for ResourceBudget<R, Q>

Source§

fn eq(&self, other: &ResourceBudget<R, Q>) -> bool

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

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

Inequality operator !=. Read more
Source§

impl<R: PartialEq, Q> StructuralPartialEq for ResourceBudget<R, Q>

Auto Trait Implementations§

§

impl<R, Q> Freeze for ResourceBudget<R, Q>
where ResourceLimit<R, Q>: Freeze, Q: Freeze,

§

impl<R, Q> RefUnwindSafe for ResourceBudget<R, Q>

§

impl<R, Q> Send for ResourceBudget<R, Q>
where ResourceLimit<R, Q>: Send, Q: Send,

§

impl<R, Q> Sync for ResourceBudget<R, Q>
where ResourceLimit<R, Q>: Sync, Q: Sync,

§

impl<R, Q> Unpin for ResourceBudget<R, Q>
where ResourceLimit<R, Q>: Unpin, Q: Unpin,

§

impl<R, Q> UnsafeUnpin for ResourceBudget<R, Q>

§

impl<R, Q> UnwindSafe for ResourceBudget<R, Q>

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<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. 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, !>

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.