pub struct ResourcePool<R, Q = u64>where
Q: ResourceQuantity,{ /* private fields */ }Expand description
A finite, non-synchronizing pool of releasable resource capacity.
Acquisition subtracts from available; release adds only after checking
the amount is no greater than in_use. The object has no lifecycle state,
waiting, fairness, permits or cancellation. An unconfigured dimension is
represented by Option<ResourcePool<R>> = None.
§Type Parameters
R- Caller-defined resource value retained for diagnostics.Q- Exact unsigned quantity used for the capacity and accounting.
§Examples
use qubit_budget::ResourcePool;
let mut pool = ResourcePool::new("workers", 2_u64);
pool.try_acquire(1).expect("one worker should fit");
assert_eq!(pool.in_use(), 1);
pool.release(1).expect("the worker is returned");
assert_eq!(pool.available(), 2);Implementations§
Source§impl<R, Q> ResourcePool<R, Q>where
Q: ResourceQuantity,
impl<R, Q> ResourcePool<R, Q>where
Q: ResourceQuantity,
Sourcepub const fn new(resource: R, limit: Q) -> Self
pub const fn new(resource: R, limit: Q) -> Self
Creates an entirely available finite pool.
§Parameters
resource- Domain resource value retained in errors.limit- Finite pool capacity.
§Returns
A pool with available == limit.
§Examples
use qubit_budget::ResourcePool;
let mut pool = ResourcePool::new("readers", 2_u64);
pool.try_acquire(1).expect("one reader should fit");
pool.release(1).expect("the reader is returned explicitly");
assert_eq!(pool.in_use(), 0);Sourcepub fn from_limit(limit: ResourceLimit<R, Q>) -> Self
pub fn from_limit(limit: ResourceLimit<R, Q>) -> Self
Sourcepub fn try_acquire(
&mut self,
amount: Q,
) -> Result<(), InsufficientBudgetError<R, Q>>where
R: Clone,
pub fn try_acquire(
&mut self,
amount: Q,
) -> Result<(), InsufficientBudgetError<R, Q>>where
R: Clone,
Acquires capacity when enough units are available.
§Parameters
amount- Quantity to acquire.
§Returns
Ok(()) after subtracting the amount, or
InsufficientBudgetError with no state change when it does
not fit.
§Errors
Returns InsufficientBudgetError when amount exceeds current
availability. The pool remains unchanged in that case.
Sourcepub fn release(&mut self, amount: Q) -> Result<(), ResourceReleaseError<R, Q>>where
R: Clone,
pub fn release(&mut self, amount: Q) -> Result<(), ResourceReleaseError<R, Q>>where
R: Clone,
Releases previously acquired capacity.
§Parameters
amount- Quantity to return to the pool.
§Returns
Ok(()) after increasing availability, or
ResourceReleaseError with no state change when the
amount exceeds current occupancy.
§Errors
Returns ResourceReleaseError when amount exceeds
current occupancy. The pool remains unchanged in that case.
Sourcepub const fn resource_limit(&self) -> &ResourceLimit<R, Q>
pub const fn resource_limit(&self) -> &ResourceLimit<R, Q>
Returns the immutable resource limit that configures this pool.
§Returns
Returns the immutable resource limit that configures this pool.