Skip to main content

Crate bytebudget

Crate bytebudget 

Source
Expand description

Exact byte accounting for bounded systems.

bytebudget provides three deliberately small concepts:

  • ByteCount is a fixed-width byte quantity with checked arithmetic.
  • Retained is a consumer-supplied retained-storage contract.
  • ByteBudget maintains 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§

ByteBudget
Aggregate byte accounting beneath one immutable limit.
ByteCount
An exact byte quantity in the crate’s fixed u64 accounting domain.
ByteCountOverflow
An integer cannot represent a byte count without loss.
CapacityExceeded
A reservation was larger than the budget’s available capacity.
OverRelease
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.