Skip to main content

ResourceLimit

Struct ResourceLimit 

Source
pub struct ResourceLimit<R, Q = u64>
where Q: Copy + Debug,
{ /* private fields */ }
Expand description

An inclusive immutable maximum for one resource measurement.

§Type Parameters

  • R - Caller-defined resource value retained in limit failures.
  • Q - Copyable measurement value used by the maximum and checks.

§Examples

use qubit_budget::ResourceLimit;

let limit = ResourceLimit::new("payload bytes", 8_u64);
limit.check(8).expect("the inclusive maximum should fit");
assert!(limit.check(9).is_err());

Implementations§

Source§

impl<R, Q> ResourceLimit<R, Q>
where Q: Copy + Debug,

Source

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

Creates an immutable limit bound to resource.

§Parameters
  • resource - Domain resource reported when the limit is exceeded.
  • maximum - Inclusive maximum measurement accepted by Self::check.
§Returns

A limit that accepts measurements less than or equal to maximum.

Source

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

Returns the resource bound to this limit.

§Returns

Returns the resource bound to this limit.

Source

pub const fn maximum(&self) -> Q

Returns this limit’s inclusive maximum measurement.

§Returns

Returns this limit’s inclusive maximum measurement.

Source

pub fn check(&self, actual: Q) -> Result<(), LimitExceededError<R, Q>>
where R: Clone, Q: Ord,

Checks whether actual is within the inclusive maximum.

§Parameters
  • actual - Observed measurement to validate.
§Returns

Ok(()) when actual <= maximum; otherwise returns LimitExceededError containing the resource, observed value, and maximum. This method does not mutate the limit.

§Errors

Returns LimitExceededError when actual is greater than this limit’s maximum.

Source§

impl<R, Q> ResourceLimit<R, Q>

Source

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

Checks a machine-sized measurement without truncating it.

§Parameters
  • actual - Native measurement to convert and compare with the limit.
§Returns

Ok(()) when the converted measurement fits this limit.

§Errors

Returns MeasuredBudgetError::Quantity when actual cannot be represented by Q, or MeasuredBudgetError::Budget when the converted value exceeds this limit.

Source

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

Checks a 64-bit measurement without truncating it.

§Parameters
  • actual - 64-bit measurement to convert and compare with the limit.
§Returns

Ok(()) when the converted measurement fits this limit.

§Errors

Returns MeasuredBudgetError::Quantity when actual cannot be represented by Q, or MeasuredBudgetError::Budget when the converted value exceeds this limit.

Trait Implementations§

Source§

impl<R: Clone, Q> Clone for ResourceLimit<R, Q>
where Q: Copy + Debug + Clone,

Source§

fn clone(&self) -> ResourceLimit<R, Q>

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<R: Copy, Q> Copy for ResourceLimit<R, Q>
where Q: Copy + Debug + Copy,

Source§

impl<R: Debug, Q> Debug for ResourceLimit<R, Q>
where Q: Copy + Debug + Debug,

Source§

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

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

impl<R: Eq, Q> Eq for ResourceLimit<R, Q>
where Q: Copy + Debug + Eq,

Source§

impl<R: Hash, Q> Hash for ResourceLimit<R, Q>
where Q: Copy + Debug + Hash,

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl<R: PartialEq, Q> PartialEq for ResourceLimit<R, Q>
where Q: Copy + Debug + PartialEq,

Source§

fn eq(&self, other: &ResourceLimit<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 ResourceLimit<R, Q>
where Q: Copy + Debug + PartialEq,

Auto Trait Implementations§

§

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

§

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

§

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

§

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

§

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

§

impl<R, Q> UnsafeUnpin for ResourceLimit<R, Q>
where R: UnsafeUnpin, Q: UnsafeUnpin,

§

impl<R, Q> UnwindSafe for ResourceLimit<R, Q>
where R: UnwindSafe, Q: UnwindSafe,

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

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.