Skip to main content

RawMoney

Struct RawMoney 

Source
pub struct RawMoney<C: Currency> { /* private fields */ }
Expand description

Represents a monetary value without automatic rounding.

RawMoney is exactly like Money except it doesn’t automatically round amounts after each operation. It keeps full decimal precision and lets callers decide when to round.

§Key Features

  • Type Safety: Provides compile-time checks to ensure valid state.
  • Precision: Uses 128-bit fixed-precision decimal for accurate calculations.
  • No Automatic Rounding: Preserves all decimal places until explicitly rounded.
  • Zero-Cost: Copy type with no heap allocations and currency metadata is zero-sized type.

§Conversion

§Where Rounding Happens

  • BaseMoney::round: rounds using currency’s minor unit (bankers rounding). Returns RawMoney.
  • BaseMoney::round_with: rounds using custom decimal points and strategy. Returns RawMoney.
  • RawMoney::finish: rounds to currency’s minor unit using bankers rounding back to Money.

§Examples

use moneylib::{Money, RawMoney, BaseMoney, macros::dec, iso::USD};

// Create RawMoney directly - no rounding
let raw = RawMoney::<USD>::new(dec!(100.567)).unwrap();
assert_eq!(raw.amount(), dec!(100.567));

// Convert from Money
let money = Money::<USD>::new(dec!(100.50)).unwrap();
let raw = money.into_raw();
assert_eq!(raw.amount(), dec!(100.50));

// Convert back to Money with rounding
let raw = RawMoney::<USD>::new(dec!(100.567)).unwrap();
let money = raw.finish();
assert_eq!(money.amount(), dec!(100.57));

§See Also

  • Money for automatically-rounded monetary values
  • BaseMoney trait for core money operations and accessors
  • BaseOps trait for arithmetic and comparison operations
  • MoneyFormatter trait for custom formatting and rounding

Implementations§

Source§

impl<C> RawMoney<C>
where C: Currency,

Source

pub const fn from_decimal(amount: Decimal) -> Self

Creates a new RawMoney instance from Decimal without rounding.

§Examples
use moneylib::{RawMoney, BaseMoney, macros::dec, iso::USD};

let raw = RawMoney::<USD>::from_decimal(dec!(123.309));
assert_eq!(raw.amount(), dec!(123.309));
Source

pub fn from_minor(minor_amount: i128) -> Result<Self, MoneyError>

Creates a new RawMoney from minor amount i128 without rounding.

§Examples
use moneylib::{RawMoney, BaseMoney, macros::dec, iso::{USD, BHD, JPY}};

// USD has 2 decimal places, so 12302 cents = $123.02
let raw = RawMoney::<USD>::from_minor(12302).unwrap();
assert_eq!(raw.amount(), dec!(123.02));

// JPY has 0 decimal places
let raw = RawMoney::<JPY>::from_minor(1000).unwrap();
assert_eq!(raw.amount(), dec!(1000));

// BHD has 3 decimal places
let raw = RawMoney::<BHD>::from_minor(12345).unwrap();
assert_eq!(raw.amount(), dec!(12.345));
Source

pub fn finish(self) -> Money<C>

Converts this RawMoney to Money, applying rounding.

Rounds the amount to the currency’s minor unit precision using the bankers rounding rule, then returns a Money instance.

§Examples
use moneylib::{RawMoney, BaseMoney, macros::dec, iso::{USD, JPY, BHD}};

let raw = RawMoney::<USD>::new(dec!(100.567)).unwrap();
let money = raw.finish();
assert_eq!(money.amount(), dec!(100.57));

let raw_jpy = RawMoney::<JPY>::new(dec!(100.567)).unwrap();
let money = raw_jpy.finish();
assert_eq!(money.amount(), dec!(101));

let raw_bhd = RawMoney::<BHD>::new(dec!(100.9999)).unwrap();
let money = raw_bhd.finish();
assert_eq!(money.amount(), dec!(101.000));
Source

pub fn from_code_comma_thousands(s: &str) -> Result<Self, MoneyError>

Parses a string in the format "CCC amount" (comma thousands separator and dot decimal separator).

The format is "CCC amount" where CCC is a currency code (1-15 letters).

For dot thousands separator format (e.g., "EUR 1.234,56"), use RawMoney::from_code_dot_thousands instead.

§Examples
use moneylib::{RawMoney, BaseMoney, macros::dec, iso::USD};
use std::str::FromStr;

let raw = RawMoney::<USD>::from_code_comma_thousands("USD 1,234.56789").unwrap();
assert_eq!(raw.amount(), dec!(1234.56789));
assert_eq!(raw.code(), "USD");

assert!(RawMoney::<USD>::from_code_comma_thousands("EUR 100.00").is_err());
Source

pub fn from_code_dot_thousands(s: &str) -> Result<Self, MoneyError>

Creates a new RawMoney from a string with dot as the thousands separator and comma as the decimal separator (e.g., "EUR 1.234,56").

The format is "CCC amount" where CCC is a currency code (1-15 letters) and

§Examples
use moneylib::{RawMoney, BaseMoney, iso::{EUR, USD}};

let raw = RawMoney::<EUR>::from_code_dot_thousands("EUR 1.234,56").unwrap();
assert_eq!(raw.code(), "EUR");

assert!(RawMoney::<USD>::from_code_dot_thousands("EUR 1.234,56").is_err());
Source

pub fn from_symbol_comma_thousands(s: &str) -> Result<Self, MoneyError>

Parse from string with symbol, comma-separated thousands, dot-separated decimal, no rounding Example: $1,234.2249 into USD 1234.2249

Source

pub fn from_symbol_dot_thousands(s: &str) -> Result<Self, MoneyError>

Parse from string with symbol, dot-separated thousands, comma-separated decimal, no rounding Example: $1.234,2249 into USD 1234.2249

Source

pub fn from_code_locale_separator(s: &str) -> Result<Self, MoneyError>

Parse from string with code, locale thousands and decimal separators.

Code is space separated with amount.

Currencies locale separators are from here: https://docs.rs/currencylib

§Example
use moneylib::{RawMoney, raw, iso::CHF, dec, BaseMoney};

let money = RawMoney::<CHF>::from_code_locale_separator("CHF 1'123'456.2223").unwrap();
assert_eq!(money.code(), "CHF");
assert_eq!(money.symbol(), "₣");
assert_eq!(money.amount(), dec!(1123456.2223));
assert_eq!(money, raw!(CHF, 1123456.2223));
Source

pub fn from_symbol_locale_separator(s: &str) -> Result<Self, MoneyError>

Parse from string with symbol, locale thousands and decimal separators.

There’s no space between symbol and amount.

Currencies locale separators are from here: https://docs.rs/currencylib

§Example
use moneylib::{RawMoney, raw, iso::CHF, dec, BaseMoney};

let money = RawMoney::<CHF>::from_symbol_locale_separator("₣1'123'456.2223").unwrap();
assert_eq!(money.code(), "CHF");
assert_eq!(money.symbol(), "₣");
assert_eq!(money.amount(), dec!(1123456.2223));
assert_eq!(money, raw!(CHF, 1123456.2223));

Trait Implementations§

Source§

impl<C> Add<Decimal> for RawMoney<C>
where C: Currency,

Source§

type Output = RawMoney<C>

The resulting type after applying the + operator.
Source§

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

Performs the + operation. Read more
Source§

impl<C> Add<RawMoney<C>> for Decimal
where C: Currency,

Source§

type Output = RawMoney<C>

The resulting type after applying the + operator.
Source§

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

Performs the + operation. Read more
Source§

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

RawMoney + RawMoney = RawMoney (no auto-rounding)

Source§

type Output = RawMoney<C>

The resulting type after applying the + operator.
Source§

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

Performs the + operation. Read more
Source§

impl<C> AddAssign for RawMoney<C>
where C: Currency,

RawMoney += RawMoney (no auto-rounding)

Source§

fn add_assign(&mut self, other: Self)

Performs the += operation. Read more
Source§

impl<C> BaseMoney<C> for RawMoney<C>
where C: Currency,

Source§

fn round(self) -> Self

Rounds explicitly to the currency’s minor unit using bankers rounding.

Unlike Money, this must be called explicitly. Returns RawMoney.

§Examples
use moneylib::{RawMoney, BaseMoney, macros::dec, iso::USD};

let raw = RawMoney::<USD>::from_decimal(dec!(100.567));
let rounded = raw.round();
assert_eq!(rounded.amount(), dec!(100.57));

// round() returns RawMoney, not Money
let rounded_again = rounded.round();
assert_eq!(rounded_again.amount(), dec!(100.57));
Source§

fn minor_amount(&self) -> Option<i128>

Returns the money amount in its smallest unit, rounding as needed.

Since minor amounts must be integers, this rounds the raw amount to the currency’s minor unit precision before computing the minor amount.

§Examples
use moneylib::{RawMoney, BaseMoney, macros::dec, iso::USD};

let raw = RawMoney::<USD>::from_decimal(dec!(123.45));
assert_eq!(raw.minor_amount().unwrap(), 12345_i128);

// Extra precision is rounded before computing minor amount
let raw = RawMoney::<USD>::from_decimal(dec!(123.238533));
assert_eq!(raw.minor_amount().unwrap(), 12324_i128);
Source§

fn new(amount: impl DecimalNumber) -> Result<Self, MoneyError>

Creates a new Money instance with amount of Decimal, f64, i32, i64, i128. Read more
Source§

fn amount(&self) -> Decimal

Returns the decimal amount of this money value. Read more
Source§

fn round_with(self, decimal_points: u32, strategy: RoundingStrategy) -> Self

Rounds the money amount to a specified number of decimal places using the given strategy. Read more
Source§

fn truncate(&self) -> Self

Truncates the money amount removing the fraction. Read more
Source§

fn truncate_with(&self, scale: u32) -> Self

Truncates the money amount to certain scale. Read more
Source§

fn name(&self) -> &str

Returns the full name of the currency. Read more
Source§

fn symbol(&self) -> &str

Returns the currency symbol. Read more
Source§

fn code(&self) -> &str

Returns the ISO 4217 currency code. Read more
Source§

fn numeric_code(&self) -> i32

Returns the ISO 4217 numeric code for the currency. Read more
Source§

fn minor_unit(&self) -> u16

Returns the number of decimal places used by the currency’s minor unit. Read more
Source§

fn thousand_separator(&self) -> &str

Returns the thousands separator used by the currency. Read more
Source§

fn decimal_separator(&self) -> &str

Returns the decimal separator used by the currency. Read more
Source§

fn is_zero(&self) -> bool

Returns true if the amount is zero. Read more
Source§

fn is_positive(&self) -> bool

Returns true if the amount is positive. Read more
Source§

fn is_negative(&self) -> bool

Returns true if the amount is negative. Read more
Source§

fn mantissa(&self) -> i128

Returns the mantissa(significand digits) of money. Read more
Source§

fn fraction(&self) -> Decimal

Returns the fractional part of the money. Read more
Source§

fn scale(&self) -> u32

Returns the the scale. Read more
Source§

fn format_code(&self) -> String

Formats money with currency code along with thousands and decimal separators. Read more
Source§

fn format_symbol(&self) -> String

Formats money with currency symbol along with thousands and decimal separators. Read more
Source§

fn format_code_minor(&self) -> String

Formats money with currency code in the smallest unit along with thousands separators. Read more
Source§

fn format_symbol_minor(&self) -> String

Formats money with currency symbol in the smallest unit along with thousands separators. Read more
Source§

fn display(&self) -> String

Returns the default display format for money (same as format_code). Read more
Source§

impl<C> BaseOps<C> for RawMoney<C>
where C: Currency,

Source§

fn abs(&self) -> Self

Returns the absolute value of the money amount. Read more
Source§

fn checked_add<RHS>(&self, rhs: RHS) -> Option<Self>
where RHS: Amount<C>,

Adds another money value to this one. Read more
Source§

fn checked_sub<RHS>(&self, rhs: RHS) -> Option<Self>
where RHS: Amount<C>,

Subtracts another money value from this one. Read more
Source§

fn checked_mul<RHS>(&self, rhs: RHS) -> Option<Self>
where RHS: DecimalNumber,

Multiplies this money value by another value. Read more
Source§

fn checked_div<RHS>(&self, rhs: RHS) -> Option<Self>
where RHS: DecimalNumber,

Divides this money value by another value. Read more
Source§

fn checked_rem<RHS>(&self, rhs: RHS) -> Option<Self>
where RHS: DecimalNumber,

Get remainder of self % rhs. Read more
Source§

fn is_approx<M, T>(&self, m: M, tolerance: T) -> bool
where M: BaseMoney<C> + BaseOps<C> + Amount<C>, T: DecimalNumber,

Compare 2 moneys within tolerance(inclusive). Read more
Source§

fn split<P, R>(&self, p: P) -> Option<R>
where R: Split<Self, C, P>,

Split money without losing a single penny. Read more
Source§

impl<C: Currency> Clone for RawMoney<C>

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<C> Debug for RawMoney<C>
where C: Currency,

Source§

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

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

impl<C: Currency> Default for RawMoney<C>

Source§

fn default() -> Self

Returns money with zero amount.

Source§

impl<'de, C: Currency> Deserialize<'de> for RawMoney<C>

Source§

fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error>

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

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

Formats RawMoney using the currency code and full decimal precision.

§Examples

use moneylib::{RawMoney, macros::dec, iso::USD};

let raw = RawMoney::<USD>::from_decimal(dec!(1234.567));
assert_eq!(format!("{}", raw), "USD 1,234.567");

let raw = RawMoney::<USD>::from_decimal(dec!(-1234.56));
assert_eq!(format!("{}", raw), "USD -1,234.56");
Source§

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

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

impl<C> Div<Decimal> for RawMoney<C>
where C: Currency,

Source§

type Output = RawMoney<C>

The resulting type after applying the / operator.
Source§

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

Performs the / operation. Read more
Source§

impl<C> Div<RawMoney<C>> for Decimal
where C: Currency,

Source§

type Output = RawMoney<C>

The resulting type after applying the / operator.
Source§

fn div(self, rhs: RawMoney<C>) -> Self::Output

Performs the / operation. Read more
Source§

impl<C> Div for RawMoney<C>
where C: Currency,

RawMoney / RawMoney = RawMoney (no auto-rounding)

Source§

type Output = RawMoney<C>

The resulting type after applying the / operator.
Source§

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

Performs the / operation. Read more
Source§

impl<C> DivAssign for RawMoney<C>
where C: Currency,

RawMoney /= RawMoney (no auto-rounding)

Source§

fn div_assign(&mut self, other: Self)

Performs the /= operation. Read more
Source§

impl<C> FromStr for RawMoney<C>
where C: Currency,

Source§

fn from_str(s: &str) -> Result<Self, Self::Err>

Parse money from string number.

§Examples
use moneylib::{BaseMoney, RawMoney, iso::USD, raw, dec};
use std::str::FromStr;

let money = RawMoney::<USD>::from_str("12334.4439").unwrap();
assert_eq!(money, raw!(USD, 12334.4439));
assert_eq!(money.amount(), dec!(12334.4439));
Source§

type Err = MoneyError

The associated error which can be returned from parsing.
Source§

impl<C> MoneyFormatter<C> for RawMoney<C>
where C: Currency,

Source§

fn format(&self, format_str: &str) -> String

Format money according to the provided format string format_str. Read more
Source§

fn format_with_separator( &self, format_str: &str, thousand_separator: &str, decimal_separator: &str, ) -> String

Format money according to the provided format string format_str. Read more
Source§

fn format_locale_amount( &self, locale_str: &str, format_str: &str, ) -> Result<String, MoneyError>

Format money’s amount using locale standard with format_str format. Read more
Source§

impl<C> Mul<Decimal> for RawMoney<C>
where C: Currency,

Source§

type Output = RawMoney<C>

The resulting type after applying the * operator.
Source§

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

Performs the * operation. Read more
Source§

impl<C> Mul<RawMoney<C>> for Decimal
where C: Currency,

Source§

type Output = RawMoney<C>

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: RawMoney<C>) -> Self::Output

Performs the * operation. Read more
Source§

impl<C> Mul for RawMoney<C>
where C: Currency,

RawMoney * RawMoney = RawMoney (no auto-rounding)

Source§

type Output = RawMoney<C>

The resulting type after applying the * operator.
Source§

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

Performs the * operation. Read more
Source§

impl<C> MulAssign for RawMoney<C>
where C: Currency,

RawMoney *= RawMoney (no auto-rounding)

Source§

fn mul_assign(&mut self, other: Self)

Performs the *= operation. Read more
Source§

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

Negation: -RawMoney = RawMoney

Source§

type Output = RawMoney<C>

The resulting type after applying the - operator.
Source§

fn neg(self) -> Self::Output

Performs the unary - operation. Read more
Source§

impl<C: Currency + Copy + 'static + Send + Sync> ObjMoney for RawMoney<C>

Source§

fn amount(&self) -> Decimal

Returns the decimal amount of this money value.
Source§

fn code(&self) -> &str

Returns the ISO 4217 currency code (e.g. "USD").
Source§

fn symbol(&self) -> &str

Returns the currency symbol (e.g. "$").
Source§

fn name(&self) -> &str

Returns the full name of the currency (e.g. "United States dollar").
Source§

fn minor_unit(&self) -> u16

Returns the number of decimal places in the currency’s minor unit (e.g. 2 for USD).
Source§

fn thousand_separator(&self) -> &str

Returns the thousands separator used by the currency’s locale (e.g. "," for USD).
Source§

fn decimal_separator(&self) -> &str

Returns the decimal separator used by the currency’s locale (e.g. "." for USD).
Source§

fn minor_unit_symbol(&self) -> &str

Returns the minor-unit symbol (e.g. "¢" for USD, "minor" when none is defined).
Source§

fn minor_amount(&self) -> Option<i128>

Returns the money amount in its smallest unit (e.g. cents for USD, pence for GBP). Read more
Source§

fn as_any(&self) -> &dyn Any

Get object money as Any
Source§

fn convert( &self, to_code: &str, rate: &dyn ObjRate, ) -> Result<Box<dyn ObjMoney>, MoneyError>

Convert ObjMoney to to_code with rate
Source§

fn is_zero(&self) -> bool

Returns true if the amount is zero.
Source§

fn is_positive(&self) -> bool

Returns true if the amount is positive. Read more
Source§

fn is_negative(&self) -> bool

Returns true if the amount is negative. Read more
Source§

fn scale(&self) -> u32

Returns the scale (number of decimal places) of the stored amount.
Source§

fn fraction(&self) -> Decimal

Returns the fractional part of the amount.
Source§

fn mantissa(&self) -> i128

Returns the mantissa (significand digits) of the amount.
Source§

fn format_code(&self) -> String

Formats money with currency code and locale separators (e.g. "USD 1,234.56").
Source§

fn format_symbol(&self) -> String

Formats money with currency symbol and locale separators (e.g. "$1,234.56").
Source§

fn format_code_minor(&self) -> String

Formats money with currency code in the smallest unit (e.g. "USD 123,456 ¢").
Source§

fn format_symbol_minor(&self) -> String

Formats money with currency symbol in the smallest unit (e.g. "$123,456 ¢").
Source§

impl<C> Ord for RawMoney<C>
where C: Currency + PartialEq + Eq,

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§

impl<C: PartialEq + Currency> PartialEq for RawMoney<C>

Source§

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

Tests for self and other values to be equal, and is used by ==.
1.0.0 (const: unstable) · 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> PartialOrd for RawMoney<C>
where C: Currency + PartialEq + Eq,

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<From: Currency, To: Currency> Rate<From, To> for RawMoney<To>

Source§

fn get_rate(&self) -> Option<Decimal>

Get T’s rate relative to C.
Source§

impl<C> Rem<Decimal> for RawMoney<C>
where C: Currency,

Source§

type Output = RawMoney<C>

The resulting type after applying the % operator.
Source§

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

Performs the % operation. Read more
Source§

impl<C: Currency> Serialize for RawMoney<C>

Source§

fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error>

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

impl<C> Sub<Decimal> for RawMoney<C>
where C: Currency,

Source§

type Output = RawMoney<C>

The resulting type after applying the - operator.
Source§

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

Performs the - operation. Read more
Source§

impl<C> Sub<RawMoney<C>> for Decimal
where C: Currency,

Source§

type Output = RawMoney<C>

The resulting type after applying the - operator.
Source§

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

Performs the - operation. Read more
Source§

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

RawMoney - RawMoney = RawMoney (no auto-rounding)

Source§

type Output = RawMoney<C>

The resulting type after applying the - operator.
Source§

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

Performs the - operation. Read more
Source§

impl<C> SubAssign for RawMoney<C>
where C: Currency,

RawMoney -= RawMoney (no auto-rounding)

Source§

fn sub_assign(&mut self, other: Self)

Performs the -= operation. Read more
Source§

impl<'a, C: Currency> Sum<&'a RawMoney<C>> for RawMoney<C>

Source§

fn sum<I: Iterator<Item = &'a RawMoney<C>>>(iter: I) -> Self

Sum all moneys(borrowed)

WARN: PANIC!!! if overflowed.

Source§

impl<C: Currency> Sum for RawMoney<C>

Source§

fn sum<I: Iterator<Item = Self>>(iter: I) -> Self

Sum all moneys

WARN: PANIC! if overflowed.

Source§

impl<C: Currency + Copy + 'static + Send + Sync> TryFrom<&dyn ObjMoney> for RawMoney<C>

Converts a reference to an ObjMoney trait object into RawMoney<C>.

The conversion succeeds when the currency code of the trait object matches C::CODE. The amount is stored without any rounding, preserving full decimal precision, exactly as RawMoney::from_decimal does.

§Errors

Returns MoneyError::CurrencyMismatchError when the currency codes do not match.

§Examples

use moneylib::{RawMoney, ObjMoney, BaseMoney, MoneyError, macros::dec, iso::{USD, EUR}};

let obj: Box<dyn ObjMoney> = Box::new(RawMoney::<USD>::new(dec!(100.567)).unwrap());

// Successful conversion
let raw = RawMoney::<USD>::try_from(obj.as_ref()).unwrap();
assert_eq!(BaseMoney::amount(&raw), dec!(100.567));
assert_eq!(BaseMoney::code(&raw), "USD");

// Currency mismatch returns an error
assert!(RawMoney::<EUR>::try_from(obj.as_ref()).is_err());
Source§

type Error = MoneyError

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

fn try_from(value: &dyn ObjMoney) -> Result<Self, Self::Error>

Performs the conversion.
Source§

impl<C: Copy + Currency> Copy for RawMoney<C>

Source§

impl<C: Eq + Currency> Eq for RawMoney<C>

Source§

impl<C> MoneyOps<C> for RawMoney<C>
where C: Currency,

Source§

impl<C: Currency> StructuralPartialEq for RawMoney<C>

Auto Trait Implementations§

§

impl<C> Freeze for RawMoney<C>

§

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

§

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

§

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

§

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

§

impl<C> UnsafeUnpin for RawMoney<C>

§

impl<C> UnwindSafe for RawMoney<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<M, From> Exchange<From> for M
where M: BaseMoney<From> + BaseOps<From> + Convert<From>, From: Currency,

Source§

type Target<T: Currency> = <M as Convert<T>>::Output where M: Convert<T>

Target conversion.
Source§

fn convert<To>( &self, rate: impl Rate<From, To>, ) -> Result<<M as Exchange<From>>::Target<To>, MoneyError>
where To: Currency, M: Convert<To>,

Method to do conversion from Self<From> into Target<To>. 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<M, C> PercentOps<C> for M
where M: BaseMoney<C> + BaseOps<C> + Amount<C>, C: Currency,

Source§

type Output = M

Source§

fn percent<D>(&self, pcn: D) -> Option<<M as PercentOps<C>>::Output>
where D: DecimalNumber,

Calculates what a certain percentage of a money amount equals. Read more
Source§

fn percent_add<D>(&self, pcn: D) -> Option<<M as PercentOps<C>>::Output>
where D: DecimalNumber,

Adds amount by percentage Read more
Source§

fn percent_adds_fixed<D, I>( &self, pcns: I, ) -> Option<<M as PercentOps<C>>::Output>
where &'a I: for<'a> IntoIterator<Item = &'a D>, D: DecimalNumber,

Adds self by multiple percentages from original amount. Read more
Source§

fn percent_adds_compound<D, I>( &self, pcns: I, ) -> Option<<M as PercentOps<C>>::Output>
where &'a I: for<'a> IntoIterator<Item = &'a D>, D: DecimalNumber,

Adds self by multiple percentages compounding. Read more
Source§

fn percent_sub<D>(&self, pcn: D) -> Option<<M as PercentOps<C>>::Output>
where D: DecimalNumber,

Subtracts amount by percentage(discount) Read more
Source§

fn percent_subs_sequence<D, I>( &self, pcns: I, ) -> Option<<M as PercentOps<C>>::Output>
where &'a I: for<'a> IntoIterator<Item = &'a D>, D: DecimalNumber,

Subtracts self by multiple percentages in sequence. Read more
Source§

fn percent_of<D>(&self, rhs: D) -> Option<Decimal>
where D: Amount<C>,

Determines what percentage one money is of another. 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> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,

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>,