Skip to main content

EstimatedUsd

Struct EstimatedUsd 

Source
pub struct EstimatedUsd(/* private fields */);
Expand description

Dollars derived from published rates. Not the bill.

There is deliberately no ActualUsd alongside this: a client that never sees an invoice cannot produce an actual. And there is deliberately no way to get an f64 out (COST-2) — no From<EstimatedUsd> for f64, no Into, no Deref, no as_f64. The Python original could only omit __float__ and hope; the cheapest way to launder an estimate into a bill is f"${float(x):.2f}", and the Rust equivalent does not compile.

EstimatedUsd::amount returns a Decimal, which is one visible step and keeps the type name at the call site. A caller who genuinely needs a float reaches for Decimal::to_f64, which is two named steps and a Option — and which is a decision a reviewer can see.

Add is implemented only against itself, so summing an amount that might be Amount::Unpriced is a type error rather than a silent zero.

§The coercion does not compile (COST-2)

The requirement is about an impl that is absent, so the check has to be a program that fails to build. compile_fail doctests are that check, and they are run by cargo test like any other:

The error code is pinned on each one, because a bare compile_fail passes for any build failure — including a typo in the test itself, which would leave the guard green while measuring nothing.

ⓘ
let estimate = EstimatedUsd::new(dec!(1.23));
// No `From<EstimatedUsd> for f64`, so no `Into` either. This is the line the
// Python client could only ask a reader not to write.
let laundered: f64 = estimate.into();
ⓘ
let estimate = EstimatedUsd::new(dec!(1.23));
// No `Deref` and no public field: the figure comes out through `amount()`, which
// names the type at the call site.
let bare = *estimate;
ⓘ
let estimate = EstimatedUsd::new(dec!(1.23));
let unknown = Amount::unpriced("no rate is published");
// `Add` is implemented only against `EstimatedUsd`, so the Python's runtime
// `TypeError` from `NotImplemented` is a compile error here (COST-4).
let wrong = estimate + unknown;

Implementations§

Source§

impl EstimatedUsd

Source

pub const ZERO: EstimatedUsd

Zero dollars, which is a legitimate priced figure — a zero-length hold, a zero-cycle comparison — and is not the same claim as Amount::Unpriced.

Source

pub fn new(amount: Decimal) -> EstimatedUsd

An estimate from a decimal figure already in the money path.

Takes a Decimal and not an f64, so this constructor cannot be the second float boundary: gb_decimal and seconds_of are the only two, which is what makes COST-6’s “exactly once” checkable by reading them.

Source

pub fn amount(self) -> Decimal

The figure, in decimal.

The one visible step out. Deliberately not Into<Decimal>: an explicit call keeps EstimatedUsd in the reader’s view at the point the label is dropped.

Source

pub fn amount_string(self) -> String

The figure rendered at display precision, without the estimate label.

For a caller assembling its own sentence. fmt::Display is what carries the label, and it is what a report renders through.

Source

pub fn parse(text: &str) -> Result<EstimatedUsd, Error>

A dollar figure from a caller’s decimal string — a budget, typically.

A string and not an f64, so this is not a third float boundary beside gb_decimal and seconds_of: the caller typed a decimal, and parsing the text they typed keeps 0.1 meaning a tenth rather than the nearest binary float. Negative is refused here rather than compared later, because every total this could be compared against is non-negative and a negative budget can only ever be a sign error the comparison would launder into “always breached”.

Trait Implementations§

Source§

impl Add for EstimatedUsd

Source§

fn add(self, other: EstimatedUsd) -> EstimatedUsd

Only an estimate adds to an estimate.

There is no Add<Amount> and no Add<Unpriced>, so the Python’s NotImplemented — a runtime TypeError — becomes a compile error. This is the last line of defence for COST-4: even a consumer that ignores CostReport::is_complete cannot sum an unknown into a dollar figure.

Source§

type Output = EstimatedUsd

The resulting type after applying the + operator.
Source§

impl Clone for EstimatedUsd

Source§

fn clone(&self) -> EstimatedUsd

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 Copy for EstimatedUsd

Source§

impl Debug for EstimatedUsd

Source§

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

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

impl Display for EstimatedUsd

A figure copied out of a terminal loses its docstring, so the label travels in Display rather than sitting in the type name alone.

Source§

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

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

impl Eq for EstimatedUsd

Source§

impl Ord for EstimatedUsd

Source§

fn cmp(&self, other: &EstimatedUsd) -> 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 EstimatedUsd

Source§

fn eq(&self, other: &EstimatedUsd) -> 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 EstimatedUsd

Source§

fn partial_cmp(&self, other: &EstimatedUsd) -> 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 StructuralPartialEq for EstimatedUsd

Source§

impl Sum for EstimatedUsd

Source§

fn sum<I>(iter: I) -> EstimatedUsd
where I: Iterator<Item = EstimatedUsd>,

Takes an iterator and generates Self from the elements by “summing up” the items.

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> AsOut<T> for T
where T: Copy,

Source§

fn as_out(&mut self) -> Out<'_, T>

Returns an out reference to self.
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> 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<Q, K> Comparable<K> for Q
where Q: Ord + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn compare(&self, key: &K) -> Ordering

Compare self to key and return their ordering.
Source§

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

Source§

fn __clone_box(&self, _: Private) -> *mut ()

Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self> ⓘ

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self> ⓘ

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<Unshared, Shared> IntoShared<Shared> for Unshared
where Shared: FromUnshared<Unshared>,

Source§

fn into_shared(self) -> Shared

Creates a shared type from an unshared type.
Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
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<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> ⓘ
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self> ⓘ

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more