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>
Functions specifically for owned statically-typed Currency instances.
impl<C> Money<C>
Functions specifically for owned statically-typed Currency instances.
Sourcepub fn format(&self, locale: &Locale) -> String
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.
Sourcepub fn format_with_options(
&self,
locale: &Locale,
options: FormattingOptions,
) -> String
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.
impl Money<&dyn Currency>
Functions specifically for borrowed dynamically-typed currencies.
Sourcepub fn format(&self, locale: &Locale) -> String
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.
Sourcepub fn format_with_options(
&self,
locale: &Locale,
options: FormattingOptions,
) -> String
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.
impl<C> Money<C>where
C: Copy,
Common functions for statically and dynamically-typed currencies.
Sourcepub fn new<N: Into<Decimal>>(amount: N, currency: C) -> Self
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).
Sourcepub fn is_positive(&self) -> bool
pub fn is_positive(&self) -> bool
Returns true if the amount is positive.
Sourcepub fn is_negative(&self) -> bool
pub fn is_negative(&self) -> bool
Returns true if the amount is negative.
Sourcepub fn round_to_precision(
&self,
decimal_places: u32,
strategy: RoundingStrategy,
) -> Self
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.
impl<C> Money<C>where
C: MinorUnits + Copy,
Methods that require knowing the minor_units of the currency.
Sourcepub fn from_minor_units(minor_units: i64, currency: C) -> Self
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.
Sourcepub fn round(&self, strategy: RoundingStrategy) -> Self
pub fn round(&self, strategy: RoundingStrategy) -> Self
Returns a new instance rounded to the amount of minor units defined by the Currency.
Sourcepub fn to_minor_units(&self, rounding_strategy: RoundingStrategy) -> Option<i64>
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.
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.
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§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.
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§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.
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§impl<C> Add for Money<C>where
C: Currency,
Supports
Add
for Money instances with a static currency.
impl<C> Add for Money<C>where
C: Currency,
Supports Add for Money instances with a static currency.
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.
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§impl<N> Div<N> for Money<&dyn Currency>
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.
impl<N> Div<N> for Money<&dyn Currency>
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§impl<C, N> Div<N> for Money<C>
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.
impl<C, N> Div<N> for Money<C>
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§impl<N> Mul<N> for Money<&dyn Currency>
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.
impl<N> Mul<N> for Money<&dyn Currency>
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§impl<C, N> Mul<N> for Money<C>
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.
impl<C, N> Mul<N> for Money<C>
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§impl<C> Neg for Money<C>where
C: Currency,
Supports
Neg
for Money instances with a static currency.
impl<C> Neg for Money<C>where
C: Currency,
Supports Neg for Money instances with a static currency.
Source§impl<C> PartialEq<Money<&dyn Currency>> for Money<C>
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.
impl<C> PartialEq<Money<&dyn Currency>> for Money<C>
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§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.
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§impl PartialEq for Money<&dyn Currency>
Allows equality comparisons between Money instances with dynamically-typed
currencies. Both the amounts and currency codes must match.
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§impl<C> PartialEq for Money<C>
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.
impl<C> PartialEq for Money<C>
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§impl PartialOrd for Money<&dyn Currency>
Allows ordering comparisons for Money instances with
dynamically-typed currencies.
impl PartialOrd for Money<&dyn Currency>
Allows ordering comparisons for Money instances with dynamically-typed currencies.
Source§impl<C> PartialOrd for Money<C>where
C: Currency + PartialOrd,
Allows ordering comparisons for Money instances with the same
statically-typed currency.
impl<C> PartialOrd for Money<C>where
C: Currency + PartialOrd,
Allows ordering comparisons for Money instances with the same statically-typed currency.
Source§impl<N> Rem<N> for Money<&dyn Currency>
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.
impl<N> Rem<N> for Money<&dyn Currency>
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§impl<C, N> Rem<N> for Money<C>
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.
impl<C, N> Rem<N> for Money<C>
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§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.
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§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.
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§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.
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§impl<C> Sub for Money<C>where
C: Currency,
Supports
Sub
for Money instances with a static currency.
impl<C> Sub for Money<C>where
C: Currency,
Supports Sub for Money instances with a static currency.
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
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