pub struct Amount<U = ()> { /* private fields */ }Expand description
Amount can be any unit
Note: PartialOrd is implemented manually for Amount<CurrencyUnit> to return None
when comparing amounts with different units. Ord is only implemented for Amount<()>.
Implementations§
Source§impl Amount
impl Amount
Sourcepub fn with_unit(self, unit: CurrencyUnit) -> Amount<CurrencyUnit>
pub fn with_unit(self, unit: CurrencyUnit) -> Amount<CurrencyUnit>
Convert an untyped amount to a typed one by adding a unit
This is used at the boundary between protocol and application layers.
Protocol types use Amount<()> (no unit), while application types
use Amount<CurrencyUnit> (with unit from keyset).
§Example
let untyped = Amount::from(100);
let typed = untyped.with_unit(CurrencyUnit::Sat);
assert_eq!(typed.value(), 100);
assert_eq!(typed.unit(), &CurrencyUnit::Sat);Sourcepub fn split(
&self,
fee_and_amounts: &FeeAndAmounts,
) -> Result<Vec<Amount>, Error>
pub fn split( &self, fee_and_amounts: &FeeAndAmounts, ) -> Result<Vec<Amount>, Error>
Split into parts that are powers of two
Returns an error if the amount cannot be fully represented with the available denominations.
Sourcepub fn split_targeted(
&self,
target: &SplitTarget,
fee_and_amounts: &FeeAndAmounts,
) -> Result<Vec<Amount>, Error>
pub fn split_targeted( &self, target: &SplitTarget, fee_and_amounts: &FeeAndAmounts, ) -> Result<Vec<Amount>, Error>
Split into parts that are powers of two by target
Sourcepub fn split_with_fee(
&self,
fee_and_amounts: &FeeAndAmounts,
) -> Result<Vec<Amount>, Error>
pub fn split_with_fee( &self, fee_and_amounts: &FeeAndAmounts, ) -> Result<Vec<Amount>, Error>
Splits amount into powers of two while accounting for the swap fee
Sourcepub fn checked_add(self, other: Amount) -> Option<Amount>
pub fn checked_add(self, other: Amount) -> Option<Amount>
Checked addition for Amount. Returns None if overflow occurs.
Sourcepub fn checked_sub(self, other: Amount) -> Option<Amount>
pub fn checked_sub(self, other: Amount) -> Option<Amount>
Checked subtraction for Amount. Returns None if overflow occurs.
Sourcepub fn checked_mul(self, other: Amount) -> Option<Amount>
pub fn checked_mul(self, other: Amount) -> Option<Amount>
Checked multiplication for Amount. Returns None if overflow occurs.
Sourcepub fn checked_div(self, other: Amount) -> Option<Amount>
pub fn checked_div(self, other: Amount) -> Option<Amount>
Checked division for Amount. Returns None if overflow occurs.
Sourcepub fn saturating_sub(self, other: Amount) -> Amount
pub fn saturating_sub(self, other: Amount) -> Amount
Subtracts other from self, returning zero if the result would be negative.
Sourcepub fn try_sum<I>(iter: I) -> Result<Amount, Error>where
I: IntoIterator<Item = Amount>,
pub fn try_sum<I>(iter: I) -> Result<Amount, Error>where
I: IntoIterator<Item = Amount>,
Try sum to check for overflow
Sourcepub fn convert_unit(
&self,
current_unit: &CurrencyUnit,
target_unit: &CurrencyUnit,
) -> Result<Amount, Error>
pub fn convert_unit( &self, current_unit: &CurrencyUnit, target_unit: &CurrencyUnit, ) -> Result<Amount, Error>
Convert unit
Source§impl Amount<CurrencyUnit>
impl Amount<CurrencyUnit>
Sourcepub fn new(value: u64, unit: CurrencyUnit) -> Amount<CurrencyUnit>
pub fn new(value: u64, unit: CurrencyUnit) -> Amount<CurrencyUnit>
Create a new Amount with an explicit unit
This is the primary constructor for typed amounts. It works with all CurrencyUnit variants including Custom.
§Example
let sat_amount = Amount::new(1000, CurrencyUnit::Sat);
let custom = Amount::new(50, CurrencyUnit::Custom("BTC".into()));Sourcepub fn value(&self) -> u64
pub fn value(&self) -> u64
Get the numeric value
§Example
let amount = Amount::new(1000, CurrencyUnit::Sat);
assert_eq!(amount.value(), 1000);Sourcepub fn unit(&self) -> &CurrencyUnit
pub fn unit(&self) -> &CurrencyUnit
Get a reference to the unit
§Example
let amount = Amount::new(1000, CurrencyUnit::Sat);
assert_eq!(amount.unit(), &CurrencyUnit::Sat);Sourcepub fn into_parts(self) -> (u64, CurrencyUnit)
pub fn into_parts(self) -> (u64, CurrencyUnit)
Consume self and return both value and unit
§Example
let amount = Amount::new(1000, CurrencyUnit::Sat);
let (value, unit) = amount.into_parts();
assert_eq!(value, 1000);
assert_eq!(unit, CurrencyUnit::Sat);Sourcepub fn checked_add(
&self,
other: &Amount<CurrencyUnit>,
) -> Result<Amount<CurrencyUnit>, Error>
pub fn checked_add( &self, other: &Amount<CurrencyUnit>, ) -> Result<Amount<CurrencyUnit>, Error>
Checked addition with unit verification
Returns an error if units don’t match or if overflow occurs.
§Example
let a = Amount::new(100, CurrencyUnit::Sat);
let b = Amount::new(50, CurrencyUnit::Sat);
let sum = a.checked_add(&b).unwrap();
assert_eq!(sum.value(), 150);
// Different units cause an error
let c = Amount::new(100, CurrencyUnit::Msat);
assert!(a.checked_add(&c).is_err());Sourcepub fn checked_sub(
&self,
other: &Amount<CurrencyUnit>,
) -> Result<Amount<CurrencyUnit>, Error>
pub fn checked_sub( &self, other: &Amount<CurrencyUnit>, ) -> Result<Amount<CurrencyUnit>, Error>
Checked subtraction with unit verification
Returns an error if units don’t match or if underflow occurs.
§Example
let a = Amount::new(100, CurrencyUnit::Sat);
let b = Amount::new(30, CurrencyUnit::Sat);
let diff = a.checked_sub(&b).unwrap();
assert_eq!(diff.value(), 70);Sourcepub fn convert_to(
&self,
target_unit: &CurrencyUnit,
) -> Result<Amount<CurrencyUnit>, Error>
pub fn convert_to( &self, target_unit: &CurrencyUnit, ) -> Result<Amount<CurrencyUnit>, Error>
Convert to a different unit
§Example
let sat = Amount::new(1000, CurrencyUnit::Sat);
let msat = sat.convert_to(&CurrencyUnit::Msat).unwrap();
assert_eq!(msat.value(), 1_000_000);
assert_eq!(msat.unit(), &CurrencyUnit::Msat);Sourcepub fn display_with_unit(&self) -> String
pub fn display_with_unit(&self) -> String
Returns a string representation that includes the unit
Trait Implementations§
Source§impl AddAssign for Amount
impl AddAssign for Amount
Source§fn add_assign(&mut self, rhs: Amount)
fn add_assign(&mut self, rhs: Amount)
+= operation. Read moreSource§impl Clone for Amount<CurrencyUnit>
impl Clone for Amount<CurrencyUnit>
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. 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 Ord for Amount
impl Ord for Amount
Source§impl PartialOrd for Amount
impl PartialOrd for Amount
Source§impl PartialOrd for Amount<CurrencyUnit>
impl PartialOrd for Amount<CurrencyUnit>
Source§impl<U> Serialize for Amount<U>
impl<U> Serialize for Amount<U>
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