pub struct ManagedResourcePool<R, Q = u64>where
Q: ResourceQuantity,{ /* private fields */ }Expand description
A cloneable finite pool whose acquired capacity is owned by RAII permits.
Clones share one synchronized capacity state. Dropping a returned
ManagedResourcePermit makes its quantity available again, including
during early returns and panic unwinding. Use crate::ResourcePool when
explicit try_acquire/release pairing is preferred.
§Type Parameters
R- Caller-defined resource value retained for diagnostics.Q- Exact unsigned quantity shared by the pool and its permits.
§Examples
use qubit_budget::ManagedResourcePool;
let pool = ManagedResourcePool::new("workers", 2_u64);
let permit = pool.try_acquire(1).expect("one worker should fit");
assert_eq!(pool.in_use(), 1);
drop(permit);
assert_eq!(pool.available(), 2);Implementations§
Source§impl<R, Q> ManagedResourcePool<R, Q>where
Q: ResourceQuantity,
impl<R, Q> ManagedResourcePool<R, Q>where
Q: ResourceQuantity,
Sourcepub fn from_limit(limit: ResourceLimit<R, Q>) -> Self
pub fn from_limit(limit: ResourceLimit<R, Q>) -> Self
Sourcepub fn try_acquire(
&self,
amount: Q,
) -> Result<ManagedResourcePermit<R, Q>, InsufficientBudgetError<R, Q>>where
R: Clone,
pub fn try_acquire(
&self,
amount: Q,
) -> Result<ManagedResourcePermit<R, Q>, InsufficientBudgetError<R, Q>>where
R: Clone,
Acquires capacity and returns a permit that releases it on Drop.
§Parameters
amount- Quantity to acquire from current availability.
§Returns
A permit owning amount units when they fit.
§Errors
Returns InsufficientBudgetError when amount exceeds current
availability. Failure leaves the shared pool unchanged. The resource is
cloned only after releasing the internal lock.
Sourcepub fn resource_limit(&self) -> &ResourceLimit<R, Q>
pub fn resource_limit(&self) -> &ResourceLimit<R, Q>
Returns the immutable resource limit configuring this pool.
Trait Implementations§
Source§impl<R, Q> Clone for ManagedResourcePool<R, Q>where
Q: ResourceQuantity,
impl<R, Q> Clone for ManagedResourcePool<R, Q>where
Q: ResourceQuantity,
Auto Trait Implementations§
impl<R, Q> Freeze for ManagedResourcePool<R, Q>
impl<R, Q> RefUnwindSafe for ManagedResourcePool<R, Q>where
Arc<ManagedResourcePoolInner<R, Q>>: RefUnwindSafe,
impl<R, Q> Send for ManagedResourcePool<R, Q>
impl<R, Q> Sync for ManagedResourcePool<R, Q>
impl<R, Q> Unpin for ManagedResourcePool<R, Q>
impl<R, Q> UnsafeUnpin for ManagedResourcePool<R, Q>where
Arc<ManagedResourcePoolInner<R, Q>>: UnsafeUnpin,
impl<R, Q> UnwindSafe for ManagedResourcePool<R, Q>where
Arc<ManagedResourcePoolInner<R, Q>>: UnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more