Skip to main content

Money

Struct Money 

Source
pub struct Money<C> { /* private fields */ }
Expand description

An amount of money in a particular currency.

Money instances are immutable. All operations that would alter the state return a new instance with that new state, leaving the original instance unaltered.

Money instances also support Copy semantics. The amount Decimal is 128 bits, but statically-typed Currency implementations are typically unit structs, so they don’t add any more. References to a dynamic currency add the size of a pointer.

Implementations§

Source§

impl<C> Money<C>
where C: Currency + Copy,

Functions specifically for owned statically-typed Currency instances.

Source

pub fn format(&self, locale: &Locale) -> String

Formats this Money instance as a locale-aware string suitable for showing to a user. This uses the icu crate for CLDR formatting rules.

Source

pub fn format_with_options( &self, locale: &Locale, options: FormattingOptions, ) -> String

Same as format but allows the caller to specify FormattingOptions.

Source§

impl Money<&dyn Currency>

Functions specifically for borrowed dynamically-typed currencies.

Source

pub fn format(&self, locale: &Locale) -> String

Formats this Money instance as a locale-aware string suitable for showing to a user. This uses the icu crate for CLDR formatting rules.

Source

pub fn format_with_options( &self, locale: &Locale, options: FormattingOptions, ) -> String

Same as format but allows the caller to specify FormattingOptions.

Source§

impl<C> Money<C>
where C: Copy,

Common functions for statically and dynamically-typed currencies.

Source

pub fn new<N: Into<Decimal>>(amount: N, currency: C) -> Self

Constructs a new Money given a decimal amount and Currency. The currency argument can be either an owned statically-typed Currency instance, or a dynamically-typed reference to a Currency instance (i.e., &dyn Currency).

Source

pub fn amount(&self) -> Decimal

Returns a copy of the amount as a Decimal.

Source

pub fn is_zero(&self) -> bool

Returns true if the amount is zero.

Source

pub fn is_positive(&self) -> bool

Returns true if the amount is positive.

Source

pub fn is_negative(&self) -> bool

Returns true if the amount is negative.

Source

pub fn round_to_precision( &self, decimal_places: u32, strategy: RoundingStrategy, ) -> Self

Returns a new instance rounded to the specified number of decimal places, using the specified strategy.

Source§

impl<C> Money<C>
where C: MinorUnits + Copy,

Methods that require knowing the minor_units of the currency.

Source

pub fn from_minor_units(minor_units: i64, currency: C) -> Self

Constructs a Money from some number of minor units in the specified Currency. For example, 100 USD minor units is one USD, but 100 JPY minor units is 100 JPY.

Source

pub fn round(&self, strategy: RoundingStrategy) -> Self

Returns a new instance rounded to the amount of minor units defined by the Currency.

Source

pub fn to_minor_units(&self, rounding_strategy: RoundingStrategy) -> Option<i64>

Returns the amount in currency minor units, suitable for sending to a payment processor. If the amount is at a higher precision than the currency’s number of minor units, the amount will be rounded using the specified rounding strategy. If the amount can’t be safely represented as an i64, None will be returned.

Source§

impl<C> Money<C>
where C: Currency + Copy,

Functions specifically for owned statically-typed Currency instances.

Source

pub fn currency(&self) -> C

Returns a copy of the Money’s Currency.

Source§

impl Money<&dyn Currency>

Functions specifically for borrowed dynamically-typed currencies.

Source

pub fn currency(&self) -> &dyn Currency

Returns the reference to the dynamically-typed Currency.

Trait Implementations§

Source§

impl<C> Add<Money<&dyn Currency>> for Money<C>
where C: Currency,

Supports Add for a Money instance with a statically-typed Currency and a Money instance with a dynamically-typed Currency. The output is a Result since the operation can fail if the currencies are incompatible.

Source§

type Output = Result<Money<C>, MoneyMathError>

The resulting type after applying the + operator.
Source§

fn add(self, rhs: Money<&dyn Currency>) -> Self::Output

Performs the + operation. Read more
Source§

impl<C> Add<Money<C>> for Money<&dyn Currency>
where C: Currency,

Supports Add for a Money instance with a dynamically-typed Currency and a Money instance with a statically-typed Currency. The output is a Result since the operation can fail if the currencies are incompatible.

Source§

type Output = Result<Money<&dyn Currency>, MoneyMathError>

The resulting type after applying the + operator.
Source§

fn add(self, rhs: Money<C>) -> Self::Output

Performs the + operation. Read more
Source§

impl Add for Money<&dyn Currency>

Supports Add for two Money instances with dynamically-typed currencies. The Output is a Result instead of a Money since the operation can fail if the currencies are incompatible.

Source§

type Output = Result<Money<&dyn Currency>, MoneyMathError>

The resulting type after applying the + operator.
Source§

fn add(self, rhs: Self) -> Self::Output

Performs the + operation. Read more
Source§

impl<C> Add for Money<C>
where C: Currency,

Supports Add for Money instances with a static currency.

Source§

type Output = Money<C>

The resulting type after applying the + operator.
Source§

fn add(self, rhs: Self) -> Self::Output

Performs the + operation. Read more
Source§

impl<C: Clone> Clone for Money<C>

Source§

fn clone(&self) -> Money<C>

Returns a duplicate of the value. Read more
1.0.0 · Source§

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

Performs copy-assignment from source. Read more
Source§

impl<C: Debug> Debug for Money<C>

Source§

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

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

impl Display for Money<&dyn Currency>

Source§

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

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

impl<C> Display for Money<C>
where C: Currency,

Display::fmt is supposed to be infallible, so this just writes the amount followed by the currency code. For more sophisticated formatting, use the the format method available with the “formatting” crate feature.

Source§

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

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

impl<N> Div<N> for Money<&dyn Currency>
where N: Into<Decimal>,

Supports Div for Money instances with a dynamic currency. The right-hand-side of the operation can be anything that can be converted into a Decimal.

Source§

type Output = Money<&dyn Currency>

The resulting type after applying the / operator.
Source§

fn div(self, rhs: N) -> Self::Output

Performs the / operation. Read more
Source§

impl<C, N> Div<N> for Money<C>
where C: Currency, N: Into<Decimal>,

Supports Div for Money instances with a static currency. The right-hand-side of the operation can be anything that can be converted into a Decimal.

Source§

type Output = Money<C>

The resulting type after applying the / operator.
Source§

fn div(self, rhs: N) -> Self::Output

Performs the / operation. Read more
Source§

impl<N> Mul<N> for Money<&dyn Currency>
where N: Into<Decimal>,

Supports Mul for Money instances with a dynamic currency. The right-hand-side of the operation can be anything that can be converted into a Decimal.

Source§

type Output = Money<&dyn Currency>

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: N) -> Self::Output

Performs the * operation. Read more
Source§

impl<C, N> Mul<N> for Money<C>
where C: Currency, N: Into<Decimal>,

Supports Mul for Money instances with a static currency. The right-hand-side of the operation can be anything that can be converted into a Decimal.

Source§

type Output = Money<C>

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: N) -> Self::Output

Performs the * operation. Read more
Source§

impl Neg for Money<&dyn Currency>

Supports Neg for Money instances with a dynamic currency.

Source§

type Output = Money<&dyn Currency>

The resulting type after applying the - operator.
Source§

fn neg(self) -> Self::Output

Performs the unary - operation. Read more
Source§

impl<C> Neg for Money<C>
where C: Currency,

Supports Neg for Money instances with a static currency.

Source§

type Output = Money<C>

The resulting type after applying the - operator.
Source§

fn neg(self) -> Self::Output

Performs the unary - operation. Read more
Source§

impl<C> PartialEq<Money<&dyn Currency>> for Money<C>
where C: Currency + PartialEq,

Allows equality comparisons between Money instances with dynamically-typed currencies and those with statically-typed currencies. Both the amounts and the currency codes must match.

Source§

fn eq(&self, other: &Money<&dyn Currency>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

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

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<C> PartialEq<Money<C>> for Money<&dyn Currency>
where C: Currency,

Allows equality comparisons between Money instances with dynamically-typed currencies and those with statically-typed currencies. Both the amounts and currency codes must match.

Source§

fn eq(&self, other: &Money<C>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

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

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl PartialEq for Money<&dyn Currency>

Allows equality comparisons between Money instances with dynamically-typed currencies. Both the amounts and currency codes must match.

Source§

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

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

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

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<C> PartialEq for Money<C>
where C: Currency + PartialEq,

Allows equality comparisons between Money instances with statically-typed currencies. The compiler will already ensure that C is the same for both instances, so only the amounts must match.

Source§

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

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

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

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl PartialOrd for Money<&dyn Currency>

Allows ordering comparisons for Money instances with dynamically-typed currencies.

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 · 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 · 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 · 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 · 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<C> PartialOrd for Money<C>
where C: Currency + PartialOrd,

Allows ordering comparisons for Money instances with the same statically-typed currency.

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 · 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 · 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 · 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 · 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<N> Rem<N> for Money<&dyn Currency>
where N: Into<Decimal>,

Supports Rem for Money instances with a dynamic currency. The right-hand-side of the operation can be anything that can be converted into a Decimal.

Source§

type Output = Money<&dyn Currency>

The resulting type after applying the % operator.
Source§

fn rem(self, rhs: N) -> Self::Output

Performs the % operation. Read more
Source§

impl<C, N> Rem<N> for Money<C>
where C: Currency, N: Into<Decimal>,

Supports Rem for Money instances with a static currency. The right-hand-side of the operation can be anything that can be converted into a Decimal.

Source§

type Output = Money<C>

The resulting type after applying the % operator.
Source§

fn rem(self, rhs: N) -> Self::Output

Performs the % operation. Read more
Source§

impl Serialize for Money<&dyn Currency>

Available on crate feature serde only.
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<C> Serialize for Money<C>
where C: Currency,

Available on crate feature serde only.
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<C> Sub<Money<&dyn Currency>> for Money<C>
where C: Currency,

Supports Sub for a Money instance with a statically-typed Currency and a Money instance with a dynamically-typed Currency. The output is a Result since the operation can fail if the currencies are incompatible.

Source§

type Output = Result<Money<C>, MoneyMathError>

The resulting type after applying the - operator.
Source§

fn sub(self, rhs: Money<&dyn Currency>) -> Self::Output

Performs the - operation. Read more
Source§

impl<C> Sub<Money<C>> for Money<&dyn Currency>
where C: Currency,

Supports Sub for a Money instance with a dynamically-typed Currency and a Money instance with a statically-typed Currency. The output is a Result since the operation can fail if the currencies are incompatible.

Source§

type Output = Result<Money<&dyn Currency>, MoneyMathError>

The resulting type after applying the - operator.
Source§

fn sub(self, rhs: Money<C>) -> Self::Output

Performs the - operation. Read more
Source§

impl Sub for Money<&dyn Currency>

Supports Sub for two Money instances with dynamically-typed currencies. The Output is a Result instead of a Money since the operation can fail if the currencies are incompatible.

Source§

type Output = Result<Money<&dyn Currency>, MoneyMathError>

The resulting type after applying the - operator.
Source§

fn sub(self, rhs: Self) -> Self::Output

Performs the - operation. Read more
Source§

impl<C> Sub for Money<C>
where C: Currency,

Supports Sub for Money instances with a static currency.

Source§

type Output = Money<C>

The resulting type after applying the - operator.
Source§

fn sub(self, rhs: Self) -> Self::Output

Performs the - operation. Read more
Source§

impl<C: Copy> Copy for Money<C>

Auto Trait Implementations§

§

impl<C> Freeze for Money<C>
where C: Freeze,

§

impl<C> RefUnwindSafe for Money<C>
where C: RefUnwindSafe,

§

impl<C> Send for Money<C>
where C: Send,

§

impl<C> Sync for Money<C>
where C: Sync,

§

impl<C> Unpin for Money<C>
where C: Unpin,

§

impl<C> UnwindSafe for Money<C>
where C: UnwindSafe,

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> 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> 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> 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<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 = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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> ErasedDestructor for T
where T: 'static,

Source§

impl<T, Rhs, Output> NumOps<Rhs, Output> for T
where T: Sub<Rhs, Output = Output> + Mul<Rhs, Output = Output> + Div<Rhs, Output = Output> + Add<Rhs, Output = Output> + Rem<Rhs, Output = Output>,