pub struct ResourceLimit<R, Q = u64>{ /* 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>
impl<R, Q> ResourceLimit<R, Q>
Sourcepub const fn new(resource: R, maximum: Q) -> Self
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 bySelf::check.
§Returns
A limit that accepts measurements less than or equal to maximum.
Sourcepub const fn maximum(&self) -> Q
pub const fn maximum(&self) -> Q
Returns this limit’s inclusive maximum measurement.
§Returns
Returns this limit’s inclusive maximum measurement.
Sourcepub fn check(&self, actual: Q) -> Result<(), LimitExceededError<R, Q>>
pub fn check(&self, actual: Q) -> Result<(), LimitExceededError<R, Q>>
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>where
Q: ResourceQuantity,
impl<R, Q> ResourceLimit<R, Q>where
Q: ResourceQuantity,
Sourcepub fn check_usize(
&self,
actual: usize,
) -> Result<(), MeasuredBudgetError<R, Q>>where
R: Clone,
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.
Sourcepub fn check_u64(&self, actual: u64) -> Result<(), MeasuredBudgetError<R, Q>>where
R: Clone,
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>
impl<R: Clone, Q> Clone for ResourceLimit<R, Q>
Source§fn clone(&self) -> ResourceLimit<R, Q>
fn clone(&self) -> ResourceLimit<R, Q>
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more