pub struct Amount(/* private fields */);Expand description
A 128-bit signed amount.
This is a general purpose signed integer, but is primarily used to represent the smallest unit of value in resources/vaults etc.
This allows Tari to support a massive number tokens within resources. e.g. 2 ETH = 2 x 10^18 Gwei.
Implementations§
Source§impl Amount
impl Amount
pub const BITS: usize
pub const BYTE_SIZE: usize
Sourcepub const NUM_U64_DIGITS: usize
pub const NUM_U64_DIGITS: usize
The number of u64 digits used to represent a Amount i.e. 2
Sourcepub const ONE_HUNDRED: Amount
pub const ONE_HUNDRED: Amount
The value of one hundred, represented as an Amount.
Sourcepub const ONE_THOUSAND: Amount
pub const ONE_THOUSAND: Amount
The value of one thousand, represented as an Amount.
Sourcepub fn from_integer<T>(amount: T) -> Amount
pub fn from_integer<T>(amount: T) -> Amount
Creates a new Amount from an integer value.
pub const fn from_usize(value: usize) -> Amount
Sourcepub const fn is_positive(&self) -> bool
pub const fn is_positive(&self) -> bool
Returns true if the amount is positive (greater than zero).
Sourcepub const fn is_negative(&self) -> bool
pub const fn is_negative(&self) -> bool
Always returns false since this type cannot represent negative values. This method is provided for symmetry with other amount types that may be signed.
pub const fn from_u64(value: u64) -> Amount
pub const fn to_u128(&self) -> u128
Sourcepub const fn checked_add(&self, other: Amount) -> Option<Amount>
pub const fn checked_add(&self, other: Amount) -> Option<Amount>
Returns the value of this amount + other. Returns None if the result underflows or overflows.
Sourcepub const fn saturating_add(&self, other: Amount) -> Amount
pub const fn saturating_add(&self, other: Amount) -> Amount
Returns the sum of two amounts, saturating at i64::MAX if the result exceeds it.
Sourcepub const fn checked_sub(&self, other: Amount) -> Option<Amount>
pub const fn checked_sub(&self, other: Amount) -> Option<Amount>
Returns the difference of two amounts, saturating at 0 if the result is negative.
pub fn sum_from_positive<A, I>(iter: I) -> Option<Amount>
Sourcepub const fn saturating_sub(&self, other: Amount) -> Amount
pub const fn saturating_sub(&self, other: Amount) -> Amount
Returns the difference of two amounts, saturating at Amount::MIN if the result underflows.
If negative results are not desired, use saturating_sub_positive.
Sourcepub const fn checked_mul(&self, other: Amount) -> Option<Amount>
pub const fn checked_mul(&self, other: Amount) -> Option<Amount>
Returns the product of two amounts, returning None if the result overflows.
Sourcepub const fn saturating_mul(&self, other: Amount) -> Amount
pub const fn saturating_mul(&self, other: Amount) -> Amount
Returns the product of two amounts, saturating at i64::MAX if the result exceeds it.
Sourcepub const fn checked_div(&self, other: Amount) -> Option<Amount>
pub const fn checked_div(&self, other: Amount) -> Option<Amount>
Returns the quotient of two amounts, returning None if the divisor is zero or if the result overflows.
Sourcepub const fn checked_div_ceil(&self, other: Amount) -> Option<Amount>
pub const fn checked_div_ceil(&self, other: Amount) -> Option<Amount>
Returns the quotient of two amounts, returning None if the divisor is zero or if the result overflows.
Sourcepub const fn div_ceil(&self, other: Amount) -> Amount
pub const fn div_ceil(&self, other: Amount) -> Amount
Returns the quotient of two amounts, returning None if the divisor is zero or if the result overflows.
§Panics
If the divisor is zero, this function will panic.
Sourcepub const fn saturating_div(&self, other: &Amount) -> Amount
pub const fn saturating_div(&self, other: &Amount) -> Amount
Returns the quotient of two amounts, saturating at i64::MAX if the result exceeds it.
Sourcepub fn to_u64_checked(&self) -> Option<u64>
pub fn to_u64_checked(&self) -> Option<u64>
Returns the value as an u64 if possible, otherwise returns None. Since the internal representation is i64, this will return None if the value is negative.
Sourcepub fn to_canonical_bytes(&self) -> [u8; 16]
pub fn to_canonical_bytes(&self) -> [u8; 16]
Returns the value as an BYTE_SIZE byte array in canonical order (little-endian).
pub fn to_le_bytes(&self) -> [u8; 16]
pub fn from_le_bytes(bytes: [u8; 16]) -> Amount
pub const fn from_le_digits(digits: [u64; 2]) -> Amount
Sourcepub fn from_le_slice(bytes: &[u8]) -> Option<Amount>
pub fn from_le_slice(bytes: &[u8]) -> Option<Amount>
Creates an integer value from a slice of bytes in little endian. The value is wrapped in an Option as the bytes may represent an integer too large to be represented by the type.
If the length of the slice is shorter than Self::BYTES, the slice is padded with zeros or ones at the end so
that it’s length equals Self::BYTES.
If the length of the slice is longer than Self::BYTES, None will be returned, unless the bytes have
trailing zeros that can be removed until the length of the slice equals Self::BYTES.
pub fn to_le_digits(&self) -> [u64; 2]
Sourcepub fn non_negative_checked(self) -> Option<Amount>
pub fn non_negative_checked(self) -> Option<Amount>
If the amount is negative (< 0), returns None, otherwise returns Some(self).
Sourcepub fn negative_checked(self) -> Option<Amount>
pub fn negative_checked(self) -> Option<Amount>
If the amount is positive (> 0), returns None, otherwise returns Some(self).
Sourcepub const fn checked_pow(&self, exp: u32) -> Option<Amount>
pub const fn checked_pow(&self, exp: u32) -> Option<Amount>
Returns the amount raised to the power of exp, returning None if the result overflows.
Sourcepub const fn try_from_str_radix(s: &str, radix: u32) -> Option<Amount>
pub const fn try_from_str_radix(s: &str, radix: u32) -> Option<Amount>
Parses a string as an Amount in the specified radix.
This function works in constant context, allowing it to be used to define constants.
Sourcepub const fn from_str_radix(s: &str, radix: u32) -> Amount
pub const fn from_str_radix(s: &str, radix: u32) -> Amount
Sourcepub fn to_decimal_string(&self, decimals: u32) -> String
pub fn to_decimal_string(&self, decimals: u32) -> String
Formats the amount as a decimal string with the specified number of decimal places.
§Panics
Panics if decimals is greater than 57.
pub fn fmt_decimals<F>(&self, f: &mut F, decimals: u32) -> Result<(), Error>where
F: Write,
Trait Implementations§
Source§impl AddAssign for Amount
impl AddAssign for Amount
Source§fn add_assign(&mut self, other: Amount)
fn add_assign(&mut self, other: Amount)
+= operation. Read moreSource§impl<'de> Deserialize<'de> for Amount
impl<'de> Deserialize<'de> for Amount
Source§fn deserialize<D>(
deserializer: D,
) -> Result<Amount, <D as Deserializer<'de>>::Error>where
D: Deserializer<'de>,
fn deserialize<D>(
deserializer: D,
) -> Result<Amount, <D as Deserializer<'de>>::Error>where
D: Deserializer<'de>,
Source§impl DivAssign for Amount
impl DivAssign for Amount
Source§fn div_assign(&mut self, other: Amount)
fn div_assign(&mut self, other: Amount)
/= operation. Read moreSource§impl MulAssign for Amount
impl MulAssign for Amount
Source§fn mul_assign(&mut self, other: Amount)
fn mul_assign(&mut self, other: Amount)
*= operation. Read moreSource§impl Ord for Amount
impl Ord for Amount
Source§impl PartialOrd<i128> for Amount
impl PartialOrd<i128> for Amount
Source§impl PartialOrd<i16> for Amount
impl PartialOrd<i16> for Amount
Source§impl PartialOrd<i32> for Amount
impl PartialOrd<i32> for Amount
Source§impl PartialOrd<i64> for Amount
impl PartialOrd<i64> for Amount
Source§impl PartialOrd<i8> for Amount
impl PartialOrd<i8> for Amount
Source§impl PartialOrd<u128> for Amount
impl PartialOrd<u128> for Amount
Source§impl PartialOrd<u16> for Amount
impl PartialOrd<u16> for Amount
Source§impl PartialOrd<u32> for Amount
impl PartialOrd<u32> for Amount
Source§impl PartialOrd<u64> for Amount
impl PartialOrd<u64> for Amount
Source§impl PartialOrd<u8> for Amount
impl PartialOrd<u8> for Amount
Source§impl PartialOrd<usize> for Amount
impl PartialOrd<usize> for Amount
Source§impl PartialOrd for Amount
impl PartialOrd for Amount
Source§impl RemAssign for Amount
impl RemAssign for Amount
Source§fn rem_assign(&mut self, other: Amount)
fn rem_assign(&mut self, other: Amount)
%= operation. Read moreSource§impl Serialize for Amount
impl Serialize for Amount
Source§fn serialize<S>(
&self,
serializer: S,
) -> Result<<S as Serializer>::Ok, <S as Serializer>::Error>where
S: Serializer,
fn serialize<S>(
&self,
serializer: S,
) -> Result<<S as Serializer>::Ok, <S as Serializer>::Error>where
S: Serializer,
Source§impl SubAssign for Amount
impl SubAssign for Amount
Source§fn sub_assign(&mut self, other: Amount)
fn sub_assign(&mut self, other: Amount)
-= operation. Read more