Skip to main content

ResourcePool

Struct ResourcePool 

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

Source

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);
Source

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

Creates an entirely available pool from an immutable resource limit.

§Parameters
  • limit - Resource identity and finite capacity for this pool.
§Returns

A pool whose available capacity equals the limit maximum.

Source

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.

Source

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.

Source

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

Returns the associated resource.

§Returns

Returns the associated resource.

Source

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.

Source

pub const fn capacity(&self) -> Q

Returns the total finite capacity.

§Returns

Returns the total finite capacity.

Source

pub const fn available(&self) -> Q

Returns currently available capacity.

§Returns

Returns currently available capacity.

Source

pub fn in_use(&self) -> Q

Returns currently acquired capacity.

§Returns

Returns currently acquired capacity.

Trait Implementations§

Source§

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

Source§

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

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

impl<R: Eq, Q> Eq for ResourcePool<R, Q>
where Q: ResourceQuantity + Eq,

Source§

impl<R: PartialEq, Q> PartialEq for ResourcePool<R, Q>

Source§

fn eq(&self, other: &ResourcePool<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 ResourcePool<R, Q>

Auto Trait Implementations§

§

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

§

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

§

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

§

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

§

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

§

impl<R, Q> UnsafeUnpin for ResourcePool<R, Q>

§

impl<R, Q> UnwindSafe for ResourcePool<R, Q>

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