Skip to main content

ManagedResourcePool

Struct ManagedResourcePool 

Source
pub struct ManagedResourcePool<R, Q = u64>{ /* 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>

Source

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

Creates a managed pool with all finite capacity available.

§Parameters
  • resource - Resource identity retained in acquisition errors.
  • limit - Total finite capacity shared by all handles.
§Returns

A new managed pool with limit units available.

Source

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

Creates a managed pool from an immutable resource limit.

§Parameters
  • limit - Resource identity and total finite capacity.
§Returns

A new managed pool preserving the supplied limit.

Source

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.

Source

pub fn resource(&self) -> &R

Returns the resource associated with this shared pool.

Source

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

Returns the immutable resource limit configuring this pool.

Source

pub fn capacity(&self) -> Q

Returns total finite capacity.

Source

pub fn available(&self) -> Q

Returns capacity not currently owned by permits.

Source

pub fn in_use(&self) -> Q

Returns capacity currently owned by permits.

Trait Implementations§

Source§

impl<R, Q> Clone for ManagedResourcePool<R, Q>

Source§

fn clone(&self) -> Self

Clones the shared handle without duplicating finite capacity.

1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

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

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<R, Q> Freeze for ManagedResourcePool<R, Q>
where Arc<ManagedResourcePoolInner<R, Q>>: Freeze,

§

impl<R, Q> RefUnwindSafe for ManagedResourcePool<R, Q>
where Arc<ManagedResourcePoolInner<R, Q>>: RefUnwindSafe,

§

impl<R, Q> Send for ManagedResourcePool<R, Q>
where Arc<ManagedResourcePoolInner<R, Q>>: Send,

§

impl<R, Q> Sync for ManagedResourcePool<R, Q>
where Arc<ManagedResourcePoolInner<R, Q>>: Sync,

§

impl<R, Q> Unpin for ManagedResourcePool<R, Q>
where Arc<ManagedResourcePoolInner<R, Q>>: Unpin,

§

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

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.