Expand description
Exact byte accounting for bounded systems.
bytebudget provides three deliberately small concepts:
ByteCountis a fixed-width byte quantity with checked arithmetic.Retainedis a consumer-supplied retained-storage contract.ByteBudgetmaintains aggregate use beneath a fixed limit.
The crate does not inspect memory or allocators. Owners define what their values retain, measure once when admitting a value, and store that charge beside the value until release.
§Accounting model
A budget preserves one invariant: used <= limit. Reservation succeeds
exactly when the requested count is no greater than the available count.
Release succeeds exactly when the released count is no greater than the
used count. Every rejected operation leaves the budget unchanged.
use bytebudget::{ByteBudget, ByteCount, Retained};
struct Payload {
retained: ByteCount,
}
impl Retained for Payload {
fn retained_bytes(&self) -> ByteCount {
self.retained
}
}
struct Entry<T> {
value: T,
charged: ByteCount,
}
let mut budget = ByteBudget::new(ByteCount::new(64));
let value = Payload { retained: ByteCount::new(24) };
let charged = value.retained_bytes();
budget.try_reserve(charged)?;
let entry = Entry { value, charged };
// The stored admission charge, not a new measurement, is released.
budget.release(entry.charged)?;
assert_eq!(budget.used(), ByteCount::ZERO);Structs§
- Byte
Budget - Aggregate byte accounting beneath one immutable limit.
- Byte
Count - An exact byte quantity in the crate’s fixed
u64accounting domain. - Byte
Count Overflow - An integer cannot represent a byte count without loss.
- Capacity
Exceeded - A reservation was larger than the budget’s available capacity.
- Over
Release - A release was larger than the budget’s current use.
Traits§
- Retained
- Reports variable storage retained by one value under an implementation’s documented accounting model.