Skip to main content

ByteBudget

Struct ByteBudget 

Source
pub struct ByteBudget { /* private fields */ }
Expand description

Aggregate byte accounting beneath one immutable limit.

A budget maintains used <= limit. Successful operations update used; rejected operations leave all observable state unchanged.

A live budget is deliberately neither cloneable nor defaultable, and its limit cannot be changed.

ⓘ
use bytebudget::{ByteBudget, ByteCount};
let budget = ByteBudget::new(ByteCount::new(8));
let _fork = budget.clone();
ⓘ
use bytebudget::ByteBudget;
fn require_copy<T: Copy>() {}
require_copy::<ByteBudget>();
ⓘ
use bytebudget::ByteBudget;
let _: ByteBudget = Default::default();
ⓘ
use bytebudget::{ByteBudget, ByteCount};
let mut budget = ByteBudget::new(ByteCount::new(8));
budget.set_limit(ByteCount::new(16));

Implementations§

Source§

impl ByteBudget

Source

pub const fn new(limit: ByteCount) -> Self

Creates an unused budget with the supplied fixed limit.

Source

pub const fn limit(&self) -> ByteCount

Returns the immutable byte limit.

Source

pub const fn used(&self) -> ByteCount

Returns the bytes currently reserved.

Source

pub const fn available(&self) -> ByteCount

Returns the bytes that can still be reserved.

Source

pub const fn can_reserve(&self, amount: ByteCount) -> bool

Returns whether amount can be reserved without exceeding the limit.

Source

pub const fn is_exhausted(&self) -> bool

Returns whether no additional nonzero byte count can be reserved.

Source

pub fn try_reserve(&mut self, amount: ByteCount) -> Result<(), CapacityExceeded>

Reserves amount when it fits in the currently available capacity.

On failure, the returned error captures both the attempted amount and the capacity available at rejection, and the budget remains unchanged.

Source

pub fn release(&mut self, amount: ByteCount) -> Result<(), OverRelease>

Releases amount when no more than that amount is currently used.

On failure, the returned error captures both the attempted release and the current use at rejection, and the budget remains unchanged.

Trait Implementations§

Source§

impl Debug for ByteBudget

Source§

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

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

impl Eq for ByteBudget

Source§

impl PartialEq for ByteBudget

Source§

fn eq(&self, other: &Self) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl StructuralPartialEq for ByteBudget

Auto Trait Implementations§

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