pub struct Budget { /* private fields */ }Expand description
A cloneable handle to one node in a hierarchical resource budget.
Implementations§
Source§impl Budget
impl Budget
Sourcepub fn builder() -> BudgetBuilder
pub fn builder() -> BudgetBuilder
Starts a root budget builder.
Sourcepub fn child(&self) -> BudgetBuilder
pub fn child(&self) -> BudgetBuilder
Starts a child builder whose operations will also count against this node.
Sourcepub fn consume(
&self,
resource: &Resource,
amount: u64,
) -> Result<(), BudgetError>
pub fn consume( &self, resource: &Resource, amount: u64, ) -> Result<(), BudgetError>
Immediately and atomically consumes an amount across the entire lineage.
§Errors
Returns an error when the request overflows, exceeds a limit, or begins after the effective deadline or cancellation request.
Sourcepub fn reserve(
&self,
resource: &Resource,
amount: u64,
) -> Result<Reservation, BudgetError>
pub fn reserve( &self, resource: &Resource, amount: u64, ) -> Result<Reservation, BudgetError>
Atomically reserves capacity for one resource.
§Errors
Returns an error when the request overflows, exceeds a limit, or begins after the effective deadline or cancellation request.
Sourcepub fn reserve_many<'a>(
&self,
resources: impl IntoIterator<Item = (&'a Resource, u64)>,
) -> Result<ReservationSet, BudgetError>
pub fn reserve_many<'a>( &self, resources: impl IntoIterator<Item = (&'a Resource, u64)>, ) -> Result<ReservationSet, BudgetError>
Atomically reserves several resources, combining duplicate entries.
The input is canonicalized by resource name. Overflow or exhaustion changes no accounting state.
§Errors
Returns an error when canonicalization overflows, a request exceeds a limit, or the operation begins after the effective deadline or cancellation request.
Sourcepub fn remaining(&self, resource: &Resource) -> Remaining
pub fn remaining(&self, resource: &Resource) -> Remaining
Returns effective remaining capacity at one consistent instant.
The result is observational. Only Budget::consume,
Budget::reserve, and Budget::reserve_many authorize work.
Sourcepub fn snapshot(&self) -> BudgetSnapshot
pub fn snapshot(&self) -> BudgetSnapshot
Captures a consistent snapshot across this node’s complete lineage.
Source§impl Budget
impl Budget
Sourcepub fn is_cancelled(&self) -> bool
pub fn is_cancelled(&self) -> bool
Returns whether cancellation has been requested for this node.
Sourcepub async fn run<F>(&self, future: F) -> Result<F::Output, BudgetError>where
F: Future,
pub async fn run<F>(&self, future: F) -> Result<F::Output, BudgetError>where
F: Future,
Runs a future until it completes, cancellation is requested, or the effective deadline expires.
If several branches are ready simultaneously, cancellation wins over the deadline, which wins over future completion. Dropping the future is only safe when the future itself is cancellation-safe.
§Errors
Returns BudgetError::Cancelled or BudgetError::DeadlineExceeded
when the corresponding terminal condition wins.