fractal_utils::amount

Struct Amount

Source
pub struct Amount { /* private fields */ }
Expand description

Fractal Global Credits amount

This struct can be used the same way as any other number. An Amount can be added or substracted to another Amount, and it can be divided and multiplied by an integer. All operations that are defined in the Amount scope and that are exact can be used directly as usual integer / float point operations.

No negative amounts can exist, since an Amount is unsigned, sothe negation operator ‘-’, then, has no use with an Amount.

Its internal representation is a 64 bit unsigned number, that is displayed as a fixed point, number of factor 1/1,000. This means that an internal representation of 1,000 would be an external amount of 1. The internal representation shouldn’t be used except when serializing and deserializing the data, since this type is sent in JSON as its internal u64.

The use is the following:

use fractal_utils::Amount;

let amount = Amount::from_repr(1_654); // 1.654
let ten = Amount::from_repr(10_000); // 10
let add_ten = amount + ten;
assert_eq!(add_ten, Amount::from_repr(11_654)); // 11.654

They can be divided and multiplied by any other unsigned integer:

let mut amount = Amount::from_repr(7_000); // 7
amount *= 10u32;
assert_eq!(amount, Amount::from_repr(70_000)); // 70

amount = amount / 30u16;
assert_eq!(amount, Amount::from_repr(2_333)); // 2.333

amount %= 1u8;
assert_eq!(amount, Amount::from_repr(333)); // 0.333

Amounts can easily be displayed using the Display trait as any other number:

let amount = Amount::from_repr(56_000);
assert_eq!(format!("{}", amount), "56");
assert_eq!(format!("{:.2}", amount), "56.00");
assert_eq!(format!("{:.5}", amount), "56.00000");
assert_eq!(format!("{:05.1}", amount), "056.0");

// And with rounding:
let amount = Amount::from_repr(56); // 0.056
assert_eq!(format!("{:.2}", amount), "0.06");

Implementations§

Source§

impl Amount

Source

pub fn from_repr(value: u64) -> Amount

Creates a new amount from its internal representation.

Source

pub fn get_repr(&self) -> u64

Gets the internal representation of the amount.

Source

pub fn min_value() -> Amount

Returns the smallest value that can be represented as a currency amount.

Source

pub fn max_value() -> Amount

Returns the largest value that can be represented as a currency amount.

Trait Implementations§

Source§

impl Add for Amount

Source§

type Output = Amount

The resulting type after applying the + operator.
Source§

fn add(self, rhs: Amount) -> Amount

Performs the + operation. Read more
Source§

impl AddAssign for Amount

Source§

fn add_assign(&mut self, rhs: Amount)

Performs the += operation. Read more
Source§

impl Clone for Amount

Source§

fn clone(&self) -> Amount

Returns a copy 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 Debug for Amount

Source§

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

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

impl Decodable for Amount

Source§

fn decode<D: Decoder>(d: &mut D) -> Result<Amount, D::Error>

Deserialize a value using a Decoder.
Source§

impl Display for Amount

Source§

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

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

impl Div<u16> for Amount

Source§

type Output = Amount

The resulting type after applying the / operator.
Source§

fn div(self, rhs: u16) -> Amount

Performs the / operation. Read more
Source§

impl Div<u32> for Amount

Source§

type Output = Amount

The resulting type after applying the / operator.
Source§

fn div(self, rhs: u32) -> Amount

Performs the / operation. Read more
Source§

impl Div<u64> for Amount

Source§

type Output = Amount

The resulting type after applying the / operator.
Source§

fn div(self, rhs: u64) -> Amount

Performs the / operation. Read more
Source§

impl Div<u8> for Amount

Source§

type Output = Amount

The resulting type after applying the / operator.
Source§

fn div(self, rhs: u8) -> Amount

Performs the / operation. Read more
Source§

impl Div<usize> for Amount

Source§

type Output = Amount

The resulting type after applying the / operator.
Source§

fn div(self, rhs: usize) -> Amount

Performs the / operation. Read more
Source§

impl DivAssign<u16> for Amount

Source§

fn div_assign(&mut self, rhs: u16)

Performs the /= operation. Read more
Source§

impl DivAssign<u32> for Amount

Source§

fn div_assign(&mut self, rhs: u32)

Performs the /= operation. Read more
Source§

impl DivAssign<u64> for Amount

Source§

fn div_assign(&mut self, rhs: u64)

Performs the /= operation. Read more
Source§

impl DivAssign<u8> for Amount

Source§

fn div_assign(&mut self, rhs: u8)

Performs the /= operation. Read more
Source§

impl DivAssign<usize> for Amount

Source§

fn div_assign(&mut self, rhs: usize)

Performs the /= operation. Read more
Source§

impl Encodable for Amount

Source§

fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error>

Serialize a value using an Encoder.
Source§

impl FromStr for Amount

Source§

type Err = AmountParseError

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

fn from_str(s: &str) -> Result<Amount, AmountParseError>

Parses a string s to return a value of this type. Read more
Source§

impl Mul<Amount> for u16

Source§

type Output = Amount

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: Amount) -> Amount

Performs the * operation. Read more
Source§

impl Mul<Amount> for u32

Source§

type Output = Amount

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: Amount) -> Amount

Performs the * operation. Read more
Source§

impl Mul<Amount> for u64

Source§

type Output = Amount

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: Amount) -> Amount

Performs the * operation. Read more
Source§

impl Mul<Amount> for u8

Source§

type Output = Amount

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: Amount) -> Amount

Performs the * operation. Read more
Source§

impl Mul<Amount> for usize

Source§

type Output = Amount

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: Amount) -> Amount

Performs the * operation. Read more
Source§

impl Mul<u16> for Amount

Source§

type Output = Amount

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: u16) -> Amount

Performs the * operation. Read more
Source§

impl Mul<u32> for Amount

Source§

type Output = Amount

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: u32) -> Amount

Performs the * operation. Read more
Source§

impl Mul<u64> for Amount

Source§

type Output = Amount

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: u64) -> Amount

Performs the * operation. Read more
Source§

impl Mul<u8> for Amount

Source§

type Output = Amount

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: u8) -> Amount

Performs the * operation. Read more
Source§

impl Mul<usize> for Amount

Source§

type Output = Amount

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: usize) -> Amount

Performs the * operation. Read more
Source§

impl MulAssign<u16> for Amount

Source§

fn mul_assign(&mut self, rhs: u16)

Performs the *= operation. Read more
Source§

impl MulAssign<u32> for Amount

Source§

fn mul_assign(&mut self, rhs: u32)

Performs the *= operation. Read more
Source§

impl MulAssign<u64> for Amount

Source§

fn mul_assign(&mut self, rhs: u64)

Performs the *= operation. Read more
Source§

impl MulAssign<u8> for Amount

Source§

fn mul_assign(&mut self, rhs: u8)

Performs the *= operation. Read more
Source§

impl MulAssign<usize> for Amount

Source§

fn mul_assign(&mut self, rhs: usize)

Performs the *= operation. Read more
Source§

impl Ord for Amount

Source§

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

This method returns an Ordering between self and other. Read more
1.21.0 · Source§

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

Compares and returns the maximum of two values. Read more
1.21.0 · Source§

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

Compares and returns the minimum of two values. Read more
1.50.0 · Source§

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

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

impl PartialEq for Amount

Source§

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

Source§

fn partial_cmp(&self, other: &Amount) -> 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 Rem<u16> for Amount

Source§

type Output = Amount

The resulting type after applying the % operator.
Source§

fn rem(self, rhs: u16) -> Amount

Performs the % operation. Read more
Source§

impl Rem<u32> for Amount

Source§

type Output = Amount

The resulting type after applying the % operator.
Source§

fn rem(self, rhs: u32) -> Amount

Performs the % operation. Read more
Source§

impl Rem<u64> for Amount

Source§

type Output = Amount

The resulting type after applying the % operator.
Source§

fn rem(self, rhs: u64) -> Amount

Performs the % operation. Read more
Source§

impl Rem<u8> for Amount

Source§

type Output = Amount

The resulting type after applying the % operator.
Source§

fn rem(self, rhs: u8) -> Amount

Performs the % operation. Read more
Source§

impl Rem<usize> for Amount

Source§

type Output = Amount

The resulting type after applying the % operator.
Source§

fn rem(self, rhs: usize) -> Amount

Performs the % operation. Read more
Source§

impl RemAssign<u16> for Amount

Source§

fn rem_assign(&mut self, rhs: u16)

Performs the %= operation. Read more
Source§

impl RemAssign<u32> for Amount

Source§

fn rem_assign(&mut self, rhs: u32)

Performs the %= operation. Read more
Source§

impl RemAssign<u64> for Amount

Source§

fn rem_assign(&mut self, rhs: u64)

Performs the %= operation. Read more
Source§

impl RemAssign<u8> for Amount

Source§

fn rem_assign(&mut self, rhs: u8)

Performs the %= operation. Read more
Source§

impl RemAssign<usize> for Amount

Source§

fn rem_assign(&mut self, rhs: usize)

Performs the %= operation. Read more
Source§

impl Sub for Amount

Source§

type Output = Amount

The resulting type after applying the - operator.
Source§

fn sub(self, rhs: Amount) -> Amount

Performs the - operation. Read more
Source§

impl SubAssign for Amount

Source§

fn sub_assign(&mut self, rhs: Amount)

Performs the -= operation. Read more
Source§

impl Copy for Amount

Source§

impl Eq for Amount

Source§

impl StructuralPartialEq for Amount

Auto Trait Implementations§

§

impl Freeze for Amount

§

impl RefUnwindSafe for Amount

§

impl Send for Amount

§

impl Sync for Amount

§

impl Unpin for Amount

§

impl UnwindSafe for Amount

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, dst: *mut T)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. 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> 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§

default 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, Rhs> NumAssignOps<Rhs> for T
where T: AddAssign<Rhs> + SubAssign<Rhs> + MulAssign<Rhs> + DivAssign<Rhs> + RemAssign<Rhs>,