Skip to main content

Budget

Struct Budget 

Source
pub struct Budget {
    pub tokens: Option<u64>,
    pub usd: Option<Decimal>,
    pub wall: Option<Duration>,
    pub calls: Option<u32>,
}
Expand description

What a node is permitted to spend, in whichever units were declared.

Any subset of the four may be set. A unit left as None is unbounded in that unit — it does not weaken the units that were declared.

§Example

use boundgraph_core::Budget;
use std::time::Duration;

let b = Budget::UNBOUNDED
    .with_usd("0.50".parse().unwrap())
    .with_wall(Duration::from_secs(30));

assert!(!b.is_unbounded());
assert_eq!(b.tokens, None);      // tokens were never bounded
assert_eq!(b.declared_units(), ["usd", "wall"]);

Fields§

§tokens: Option<u64>

Total tokens, prompt plus completion.

§usd: Option<Decimal>

Spend in US dollars.

A decimal, never a float: money that round-trips through f64 stops being auditable, and the ledger is meant to be audited.

§wall: Option<Duration>

Wall-clock time.

§calls: Option<u32>

Number of model calls.

Implementations§

Source§

impl Budget

Source

pub const UNBOUNDED: Self

No limit in any unit.

The default, and deliberately something you have to name — see the module documentation.

Source

pub fn with_tokens(self, tokens: u64) -> Self

Bound the token count.

Source

pub fn with_usd(self, usd: Decimal) -> Self

Bound the spend.

The amount is normalised, so 1.50 and 1.5 are the same budget and therefore encode to the same bytes and the same digest.

Source

pub fn with_wall(self, wall: Duration) -> Self

Bound the wall-clock time.

Source

pub fn with_calls(self, calls: u32) -> Self

Bound the number of model calls.

Source

pub fn is_unbounded(&self) -> bool

Whether nothing at all is bounded.

Source

pub fn amount(&self, unit: Unit) -> Option<u64>

What this budget bounds in one unit, normalised to an integer.

Currency becomes micro-dollars and time becomes milliseconds — see the module documentation for why there is one space to compare in. None means this budget does not bound that unit, which is not the same as bounding it at zero.

Source

pub fn bounded_units(&self) -> Vec<Unit>

Which units this budget bounds, in a stable order.

Source

pub fn excesses(&self, parent: &Self) -> Vec<Excess>

Every unit in which this budget asks for more than parent permits.

Only units both budgets bound can be exceeded — see the module documentation for why the two asymmetric cases are legal. Returns them all rather than the first, in unit order.

Source

pub fn fits_within(&self, parent: &Self) -> bool

Whether this budget can be declared beneath parent.

True when it claims no more than the parent has to give, in every unit they both bound.

Source

pub fn clamped_to(&self, parent: &Self) -> Self

This budget as it is actually enforced beneath parent.

The tighter of the two in every unit, which is the composition rule itself: an effective ceiling is the smallest bound on the path down to a node. Applying it to a budget that already fits_within its parent returns that budget with the parent’s bounds filled in where it declared none.

Idempotent, and associative down a chain: clamping to a parent and then to a grandparent gives what clamping straight to the tightest of the three would.

Source

pub fn declared_units(&self) -> Vec<&'static str>

Which units this budget actually constrains, in a stable order.

Useful for diagnostics: “this flow bounds usd but not wall” is a reviewable statement.

Trait Implementations§

Source§

impl Clone for Budget

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Budget

Source§

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

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

impl Default for Budget

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl<'de> Deserialize<'de> for Budget

Source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl Display for Budget

Source§

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

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

impl Eq for Budget

Source§

impl Hash for Budget

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl Ord for Budget

Source§

fn cmp(&self, other: &Self) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 (const: unstable) · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 (const: unstable) · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 (const: unstable) · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

fn clamp_to<R>(self, range: R) -> Self
where Self: Sized, R: ClampBounds<Self>,

🔬This is a nightly-only experimental API. (clamp_to)
Restrict a value to a certain range. Read more
Source§

impl PartialEq for Budget

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 PartialOrd for Budget

Source§

fn partial_cmp(&self, other: &Self) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 (const: unstable) · Source§

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

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 (const: unstable) · Source§

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

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 (const: unstable) · Source§

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

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 (const: unstable) · Source§

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

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl Serialize for Budget

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl StructuralPartialEq for Budget

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> Canonical for T
where T: Serialize + ?Sized,

Source§

fn canonical_bytes(&self) -> Result<Vec<u8>, CanonicalError>

The canonical CBOR encoding of this value.
Source§

fn digest(&self) -> Result<Digest, CanonicalError>

The blake3 digest of this value’s canonical encoding.
Source§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,

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> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
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.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V