Struct massa_models::amount::Amount
source · pub struct Amount(u64);
Expand description
A structure representing a decimal Amount of coins with safe operations
this allows ensuring that there is never an uncontrolled overflow or precision loss
while providing a convenient decimal interface for users
The underlying u64
raw representation if a fixed-point value with factor AMOUNT_DECIMAL_FACTOR
The minimal value is 0 and the maximal value is 18446744073.709551615(std::u64::MAX/1e9)
Tuple Fields§
§0: u64
Implementations§
source§impl Amount
impl Amount
sourcefn to_decimal(self) -> Decimal
fn to_decimal(self) -> Decimal
Convert to decimal
sourcefn from_decimal(dec: Decimal) -> Result<Self, ModelsError>
fn from_decimal(dec: Decimal) -> Result<Self, ModelsError>
Create an Amount from a Decimal
sourcepub const fn const_init(mantissa: u64, scale: u32) -> Self
pub const fn const_init(mantissa: u64, scale: u32) -> Self
Create an Amount from the form mantissa / (10^scale)
in a const way.
WARNING: Panics on any error.
Used only for constant initialization.
let amount_1: Amount = Amount::from_str("0.042").unwrap();
let amount_2: Amount = Amount::const_init(42, 3);
assert_eq!(amount_1, amount_2);
let amount_1: Amount = Amount::from_str("1000").unwrap();
let amount_2: Amount = Amount::const_init(1000, 0);
assert_eq!(amount_1, amount_2);
sourcepub fn to_mantissa_scale(&self) -> (u64, u32)
pub fn to_mantissa_scale(&self) -> (u64, u32)
Returns the value in the (mantissa, scale) format where amount = mantissa * 10^(-scale)
let amount = Amount::from_str("0.123456789").unwrap();
let (mantissa, scale) = amount.to_mantissa_scale();
assert_eq!(mantissa, 123456789);
assert_eq!(scale, AMOUNT_DECIMAL_SCALE);
sourcepub fn from_mantissa_scale(
mantissa: u64,
scale: u32,
) -> Result<Self, ModelsError>
pub fn from_mantissa_scale( mantissa: u64, scale: u32, ) -> Result<Self, ModelsError>
Creates an amount in the format mantissa*10^(-scale).
let a = Amount::from_mantissa_scale(123, 2).unwrap();
assert_eq!(a.to_string(), "1.23");
let a = Amount::from_mantissa_scale(123, 100);
assert!(a.is_err());
sourcepub const fn to_raw(&self) -> u64
pub const fn to_raw(&self) -> u64
Obtains the underlying raw u64
representation
Warning: do not use this unless you know what you are doing
because the raw value does not take the AMOUNT_DECIMAL_FACTOR
into account.
sourcepub const fn from_raw(raw: u64) -> Self
pub const fn from_raw(raw: u64) -> Self
constructs an Amount
from the underlying raw u64
representation
Warning: do not use this unless you know what you are doing
because the raw value does not take the AMOUNT_DECIMAL_FACTOR
into account
In most cases, you should be using Amount::from_str("11.23")
sourcepub fn saturating_add(self, amount: Amount) -> Self
pub fn saturating_add(self, amount: Amount) -> Self
safely add self to another amount, saturating the result on overflow
sourcepub fn saturating_sub(self, amount: Amount) -> Self
pub fn saturating_sub(self, amount: Amount) -> Self
safely subtract another amount from self, saturating the result on underflow
sourcepub fn checked_sub(self, amount: Amount) -> Option<Self>
pub fn checked_sub(self, amount: Amount) -> Option<Self>
safely subtract another amount from self, returning None on underflow
let amount_1 : Amount = Amount::from_str("42").unwrap();
let amount_2 : Amount = Amount::from_str("7").unwrap();
let res : Amount = amount_1.checked_sub(amount_2).unwrap();
assert_eq!(res, Amount::from_str("35").unwrap())
sourcepub fn checked_add(self, amount: Amount) -> Option<Self>
pub fn checked_add(self, amount: Amount) -> Option<Self>
safely add self to another amount, returning None on overflow
let amount_1 : Amount = Amount::from_str("42").unwrap();
let amount_2 : Amount = Amount::from_str("7").unwrap();
let res : Amount = amount_1.checked_add(amount_2).unwrap();
assert_eq!(res, Amount::from_str("49").unwrap())
sourcepub fn checked_mul_u64(self, factor: u64) -> Option<Self>
pub fn checked_mul_u64(self, factor: u64) -> Option<Self>
safely multiply self with a u64
, returning None on overflow
let amount_1 : Amount = Amount::from_str("42").unwrap();
let res : Amount = amount_1.checked_mul_u64(7).unwrap();
assert_eq!(res, Amount::from_str("294").unwrap())
sourcepub const fn saturating_mul_u64(self, factor: u64) -> Self
pub const fn saturating_mul_u64(self, factor: u64) -> Self
safely multiply self with a u64
, saturating the result on overflow
let amount_1 : Amount = Amount::from_str("42").unwrap();
let res : Amount = amount_1.saturating_mul_u64(7);
assert_eq!(res, Amount::from_str("294").unwrap());
sourcepub fn checked_div_u64(self, factor: u64) -> Option<Self>
pub fn checked_div_u64(self, factor: u64) -> Option<Self>
safely divide self by a u64
, returning None if the factor is zero
let amount_1 : Amount = Amount::from_str("42").unwrap();
let res : Amount = amount_1.checked_div_u64(7).unwrap();
assert_eq!(res, Amount::from_str("6").unwrap());
sourcepub fn checked_div(self, divisor: Self) -> Option<u64>
pub fn checked_div(self, divisor: Self) -> Option<u64>
safely divide self by an amount, returning None if the divisor is zero
let amount_1 : Amount = Amount::from_str("42").unwrap();
let amount_2 : Amount = Amount::from_str("7").unwrap();
let res : u64 = amount_1.checked_div(amount_2).unwrap();
assert_eq!(res, 6);
sourcepub fn checked_rem(&self, divisor: &Amount) -> Option<Amount>
pub fn checked_rem(&self, divisor: &Amount) -> Option<Amount>
compute self % divisor, return None if divisor is zero
let amount_1 : Amount = Amount::from_str("42").unwrap();
let amount_2 : Amount = Amount::from_str("10").unwrap();
let res : Amount = amount_1.checked_rem(&amount_2).unwrap();
assert_eq!(res, Amount::from_str("2").unwrap());
sourcepub fn checked_rem_u64(&self, divisor: u64) -> Option<Amount>
pub fn checked_rem_u64(&self, divisor: u64) -> Option<Amount>
compute self % divisor, return None if divisor is zero
let amount_1 : Amount = Amount::from_str("42").unwrap();
let res : Amount = amount_1.checked_rem_u64(40000000000).unwrap();
assert_eq!(res, Amount::from_str("2").unwrap());
Trait Implementations§
source§impl<'de> Deserialize<'de> for Amount
impl<'de> Deserialize<'de> for Amount
source§fn deserialize<D>(deserializer: D) -> Result<Amount, D::Error>where
D: Deserializer<'de>,
fn deserialize<D>(deserializer: D) -> Result<Amount, D::Error>where
D: Deserializer<'de>,
source§impl Deserializer<Amount> for AmountDeserializer
impl Deserializer<Amount> for AmountDeserializer
source§fn deserialize<'a, E: ParseError<&'a [u8]> + ContextError<&'a [u8]>>(
&self,
buffer: &'a [u8],
) -> IResult<&'a [u8], Amount, E>
fn deserialize<'a, E: ParseError<&'a [u8]> + ContextError<&'a [u8]>>( &self, buffer: &'a [u8], ) -> IResult<&'a [u8], Amount, E>
§Example
use massa_models::amount::{Amount, AmountSerializer, AmountDeserializer};
use massa_serialization::{Serializer, Deserializer, DeserializeError};
use std::str::FromStr;
use std::ops::Bound::Included;
let amount = Amount::from_str("11.111").unwrap();
let serializer = AmountSerializer::new();
let deserializer = AmountDeserializer::new(Included(Amount::MIN), Included(Amount::MAX));
let mut serialized = vec![];
serializer.serialize(&amount, &mut serialized).unwrap();
let (rest, amount_deser) = deserializer.deserialize::<DeserializeError>(&serialized).unwrap();
assert!(rest.is_empty());
assert_eq!(amount_deser, amount);
source§impl Display for Amount
impl Display for Amount
display an Amount in decimal string form (like “10.33”)
let value = Amount::from_str("11.111").unwrap();
assert_eq!(format!("{}", value), "11.111")
source§impl FromStr for Amount
impl FromStr for Amount
build an Amount from decimal string form (like “10.33”) note that this will fail if the string format is invalid or if the conversion would cause an overflow, underflow or precision loss
assert!(Amount::from_str("11.1").is_ok());
assert!(Amount::from_str("11.1111111111111111111111").is_err());
assert!(Amount::from_str("1111111111111111111111").is_err());
assert!(Amount::from_str("-11.1").is_err());
assert!(Amount::from_str("abc").is_err());
source§impl Ord for Amount
impl Ord for Amount
source§impl PartialEq for Amount
impl PartialEq for Amount
source§impl PartialOrd for Amount
impl PartialOrd for Amount
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self
and other
) and is used by the <=
operator. Read moresource§impl Serializer<Amount> for AmountSerializer
impl Serializer<Amount> for AmountSerializer
source§fn serialize(
&self,
value: &Amount,
buffer: &mut Vec<u8>,
) -> Result<(), SerializeError>
fn serialize( &self, value: &Amount, buffer: &mut Vec<u8>, ) -> Result<(), SerializeError>
§Example
use massa_models::amount::{Amount, AmountSerializer};
use massa_serialization::Serializer;
use std::str::FromStr;
use std::ops::Bound::Included;
let amount = Amount::from_str("11.111").unwrap();
let serializer = AmountSerializer::new();
let mut serialized = vec![];
serializer.serialize(&amount, &mut serialized).unwrap();
impl Copy for Amount
impl Eq for Amount
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> 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: Copy,
impl<T> CloneToUninit for Twhere
T: Copy,
source§unsafe fn clone_to_uninit(&self, dst: *mut T)
unsafe fn clone_to_uninit(&self, dst: *mut T)
clone_to_uninit
)source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
source§default unsafe fn clone_to_uninit(&self, dst: *mut T)
default unsafe fn clone_to_uninit(&self, dst: *mut T)
clone_to_uninit
)§impl<Q, K> Comparable<K> for Q
impl<Q, K> Comparable<K> for Q
§impl<T> Conv for T
impl<T> Conv for T
§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key
and return true
if they are equal.source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key
and return true
if they are equal.§impl<T> FmtForward for T
impl<T> FmtForward for T
§fn fmt_binary(self) -> FmtBinary<Self>where
Self: Binary,
fn fmt_binary(self) -> FmtBinary<Self>where
Self: Binary,
self
to use its Binary
implementation when Debug
-formatted.§fn fmt_display(self) -> FmtDisplay<Self>where
Self: Display,
fn fmt_display(self) -> FmtDisplay<Self>where
Self: Display,
self
to use its Display
implementation when
Debug
-formatted.§fn fmt_lower_exp(self) -> FmtLowerExp<Self>where
Self: LowerExp,
fn fmt_lower_exp(self) -> FmtLowerExp<Self>where
Self: LowerExp,
self
to use its LowerExp
implementation when
Debug
-formatted.§fn fmt_lower_hex(self) -> FmtLowerHex<Self>where
Self: LowerHex,
fn fmt_lower_hex(self) -> FmtLowerHex<Self>where
Self: LowerHex,
self
to use its LowerHex
implementation when
Debug
-formatted.§fn fmt_octal(self) -> FmtOctal<Self>where
Self: Octal,
fn fmt_octal(self) -> FmtOctal<Self>where
Self: Octal,
self
to use its Octal
implementation when Debug
-formatted.§fn fmt_pointer(self) -> FmtPointer<Self>where
Self: Pointer,
fn fmt_pointer(self) -> FmtPointer<Self>where
Self: Pointer,
self
to use its Pointer
implementation when
Debug
-formatted.§fn fmt_upper_exp(self) -> FmtUpperExp<Self>where
Self: UpperExp,
fn fmt_upper_exp(self) -> FmtUpperExp<Self>where
Self: UpperExp,
self
to use its UpperExp
implementation when
Debug
-formatted.§fn fmt_upper_hex(self) -> FmtUpperHex<Self>where
Self: UpperHex,
fn fmt_upper_hex(self) -> FmtUpperHex<Self>where
Self: UpperHex,
self
to use its UpperHex
implementation when
Debug
-formatted.§fn fmt_list(self) -> FmtList<Self>where
&'a Self: for<'a> IntoIterator,
fn fmt_list(self) -> FmtList<Self>where
&'a Self: for<'a> IntoIterator,
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
source§impl<T> IntoRequest<T> for T
impl<T> IntoRequest<T> for T
source§fn into_request(self) -> Request<T>
fn into_request(self) -> Request<T>
T
in a tonic::Request
§impl<T> Pipe for Twhere
T: ?Sized,
impl<T> Pipe for Twhere
T: ?Sized,
§fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> Rwhere
Self: Sized,
fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> Rwhere
Self: Sized,
§fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> Rwhere
R: 'a,
fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> Rwhere
R: 'a,
self
and passes that borrow into the pipe function. Read more§fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> Rwhere
R: 'a,
fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> Rwhere
R: 'a,
self
and passes that borrow into the pipe function. Read more§fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> R
fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> R
§fn pipe_borrow_mut<'a, B, R>(
&'a mut self,
func: impl FnOnce(&'a mut B) -> R,
) -> R
fn pipe_borrow_mut<'a, B, R>( &'a mut self, func: impl FnOnce(&'a mut B) -> R, ) -> R
§fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> R
fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> R
self
, then passes self.as_ref()
into the pipe function.§fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> R
fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> R
self
, then passes self.as_mut()
into the pipe
function.§fn pipe_deref<'a, T, R>(&'a self, func: impl FnOnce(&'a T) -> R) -> R
fn pipe_deref<'a, T, R>(&'a self, func: impl FnOnce(&'a T) -> R) -> R
self
, then passes self.deref()
into the pipe function.§impl<T> Tap for T
impl<T> Tap for T
§fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Self
fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Self
Borrow<B>
of a value. Read more§fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Self
fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Self
BorrowMut<B>
of a value. Read more§fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Self
fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Self
AsRef<R>
view of a value. Read more§fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Self
fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Self
AsMut<R>
view of a value. Read more§fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Self
fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Self
Deref::Target
of a value. Read more§fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Self
fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Self
Deref::Target
of a value. Read more§fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self
fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self
.tap()
only in debug builds, and is erased in release builds.§fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self
fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self
.tap_mut()
only in debug builds, and is erased in release
builds.§fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Self
fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Self
.tap_borrow()
only in debug builds, and is erased in release
builds.§fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Self
fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Self
.tap_borrow_mut()
only in debug builds, and is erased in release
builds.§fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Self
fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Self
.tap_ref()
only in debug builds, and is erased in release
builds.§fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Self
fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Self
.tap_ref_mut()
only in debug builds, and is erased in release
builds.§fn tap_deref_dbg<T>(self, func: impl FnOnce(&T)) -> Self
fn tap_deref_dbg<T>(self, func: impl FnOnce(&T)) -> Self
.tap_deref()
only in debug builds, and is erased in release
builds.