pub struct Amount(_);
Expand description
Amount
The Amount type can be used to express Bitcoin amounts that support arithmetic and conversion to various denominations.
Warning!
This type implements several arithmetic operations from core::ops.
To prevent errors due to overflow or underflow when using these operations,
it is advised to instead use the checked arithmetic methods whose names
start with checked_
. The operations from core::ops that Amount
implements will panic when overflow or underflow occurs. Also note that
since the internal representation of amounts is unsigned, subtracting below
zero is considered an underflow and will cause a panic if you’re not using
the checked arithmetic methods.
Implementations§
§impl Amount
impl Amount
pub const MAX_MONEY: Amount = Amount(21000000 * 100000000)
pub const MAX_MONEY: Amount = Amount(21000000 * 100000000)
The maximum value allowed as an amount. Useful for sanity checking.
pub const fn from_sat(satoshi: u64) -> Amount
pub const fn from_sat(satoshi: u64) -> Amount
Create an Amount with satoshi precision and the given number of satoshis.
pub fn from_btc(btc: f64) -> Result<Amount, ParseAmountError>
pub fn from_btc(btc: f64) -> Result<Amount, ParseAmountError>
Convert from a value expressing bitcoins to an Amount.
pub fn from_str_in(
s: &str,
denom: Denomination
) -> Result<Amount, ParseAmountError>
pub fn from_str_in( s: &str, denom: Denomination ) -> Result<Amount, ParseAmountError>
Parse a decimal string as a value in the given denomination.
Note: This only parses the value string. If you want to parse a value with denomination, use FromStr.
pub fn from_str_with_denomination(s: &str) -> Result<Amount, ParseAmountError>
pub fn from_str_with_denomination(s: &str) -> Result<Amount, ParseAmountError>
Parses amounts with denomination suffix like they are produced with Self::to_string_with_denomination or with fmt::Display. If you want to parse only the amount without the denomination, use Self::from_str_in.
pub fn to_float_in(self, denom: Denomination) -> f64
pub fn to_float_in(self, denom: Denomination) -> f64
Express this Amount as a floating-point value in the given denomination.
Please be aware of the risk of using floating-point numbers.
pub fn from_float_in(
value: f64,
denom: Denomination
) -> Result<Amount, ParseAmountError>
pub fn from_float_in( value: f64, denom: Denomination ) -> Result<Amount, ParseAmountError>
Convert this Amount in floating-point notation with a given denomination. Can return error if the amount is too big, too precise or negative.
Please be aware of the risk of using floating-point numbers.
pub fn display_in(self, denomination: Denomination) -> Display
pub fn display_in(self, denomination: Denomination) -> Display
Create an object that implements fmt::Display
using specified denomination.
pub fn display_dynamic(self) -> Display
pub fn display_dynamic(self) -> Display
Create an object that implements fmt::Display
dynamically selecting denomination.
This will use BTC for values greater than or equal to 1 BTC and satoshis otherwise. To avoid confusion the denomination is always shown.
pub fn fmt_value_in(
self,
f: &mut dyn Write,
denom: Denomination
) -> Result<(), Error>
pub fn fmt_value_in( self, f: &mut dyn Write, denom: Denomination ) -> Result<(), Error>
Format the value of this Amount in the given denomination.
Does not include the denomination.
pub fn to_string_in(self, denom: Denomination) -> String
pub fn to_string_in(self, denom: Denomination) -> String
Get a string number of this Amount in the given denomination.
Does not include the denomination.
pub fn to_string_with_denomination(self, denom: Denomination) -> String
pub fn to_string_with_denomination(self, denom: Denomination) -> String
Get a formatted string of this Amount in the given denomination, suffixed with the abbreviation for the denomination.
pub fn checked_add(self, rhs: Amount) -> Option<Amount>
pub fn checked_add(self, rhs: Amount) -> Option<Amount>
Checked addition. Returns None if overflow occurred.
pub fn checked_sub(self, rhs: Amount) -> Option<Amount>
pub fn checked_sub(self, rhs: Amount) -> Option<Amount>
Checked subtraction. Returns None if overflow occurred.
pub fn checked_mul(self, rhs: u64) -> Option<Amount>
pub fn checked_mul(self, rhs: u64) -> Option<Amount>
Checked multiplication. Returns None if overflow occurred.
pub fn checked_div(self, rhs: u64) -> Option<Amount>
pub fn checked_div(self, rhs: u64) -> Option<Amount>
Checked integer division. Be aware that integer division loses the remainder if no exact division can be made. Returns None if overflow occurred.
pub fn checked_rem(self, rhs: u64) -> Option<Amount>
pub fn checked_rem(self, rhs: u64) -> Option<Amount>
Checked remainder. Returns None if overflow occurred.
pub fn to_signed(self) -> Result<SignedAmount, ParseAmountError>
pub fn to_signed(self) -> Result<SignedAmount, ParseAmountError>
Convert to a signed amount.
Trait Implementations§
§impl AddAssign<Amount> for Amount
impl AddAssign<Amount> for Amount
§fn add_assign(&mut self, other: Amount)
fn add_assign(&mut self, other: Amount)
+=
operation. Read more§impl DivAssign<u64> for Amount
impl DivAssign<u64> for Amount
§fn div_assign(&mut self, rhs: u64)
fn div_assign(&mut self, rhs: u64)
/=
operation. Read more§impl MulAssign<u64> for Amount
impl MulAssign<u64> for Amount
§fn mul_assign(&mut self, rhs: u64)
fn mul_assign(&mut self, rhs: u64)
*=
operation. Read more§impl Ord for Amount
impl Ord for Amount
§impl PartialOrd<Amount> for Amount
impl PartialOrd<Amount> for Amount
§fn partial_cmp(&self, other: &Amount) -> Option<Ordering>
fn partial_cmp(&self, other: &Amount) -> Option<Ordering>
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 more§impl RemAssign<u64> for Amount
impl RemAssign<u64> for Amount
§fn rem_assign(&mut self, modulus: u64)
fn rem_assign(&mut self, modulus: u64)
%=
operation. Read more§impl SerdeAmount for Amount
impl SerdeAmount for Amount
fn ser_sat<S>( self, s: S ) -> Result<<S as Serializer>::Ok, <S as Serializer>::Error>where S: Serializer,
fn des_sat<'d, D>(d: D) -> Result<Amount, <D as Deserializer<'d>>::Error>where D: Deserializer<'d>,
fn ser_btc<S>( self, s: S ) -> Result<<S as Serializer>::Ok, <S as Serializer>::Error>where S: Serializer,
fn des_btc<'d, D>(d: D) -> Result<Amount, <D as Deserializer<'d>>::Error>where D: Deserializer<'d>,
§impl SerdeAmountForOpt for Amount
impl SerdeAmountForOpt for Amount
fn type_prefix() -> &'static str
fn ser_sat_opt<S>( self, s: S ) -> Result<<S as Serializer>::Ok, <S as Serializer>::Error>where S: Serializer,
fn ser_btc_opt<S>( self, s: S ) -> Result<<S as Serializer>::Ok, <S as Serializer>::Error>where S: Serializer,
§impl SubAssign<Amount> for Amount
impl SubAssign<Amount> for Amount
§fn sub_assign(&mut self, other: Amount)
fn sub_assign(&mut self, other: Amount)
-=
operation. Read moreimpl Copy for Amount
impl Eq for Amount
impl StructuralEq for Amount
impl StructuralPartialEq for Amount
Auto Trait Implementations§
impl RefUnwindSafe for Amount
impl Send for Amount
impl Sync for Amount
impl Unpin for Amount
impl UnwindSafe for Amount
Blanket Implementations§
§impl<T> CheckedSum<Amount> for Twhere
T: Iterator<Item = Amount>,
impl<T> CheckedSum<Amount> for Twhere T: Iterator<Item = Amount>,
§fn checked_sum(self) -> Option<Amount>
fn checked_sum(self) -> Option<Amount>
None
.§impl<T> Conv for T
impl<T> Conv for T
source§impl<Q, K> Equivalent<K> for Qwhere
Q: Eq + ?Sized,
K: Borrow<Q> + ?Sized,
impl<Q, K> Equivalent<K> for Qwhere Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,
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,
source§impl<T> Instrument for T
impl<T> Instrument for T
source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
§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) -> Rwhere
Self: Borrow<B>,
B: 'a + ?Sized,
R: 'a,
fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> Rwhere Self: Borrow<B>, B: 'a + ?Sized, R: 'a,
§fn pipe_borrow_mut<'a, B, R>(
&'a mut self,
func: impl FnOnce(&'a mut B) -> R
) -> Rwhere
Self: BorrowMut<B>,
B: 'a + ?Sized,
R: 'a,
fn pipe_borrow_mut<'a, B, R>( &'a mut self, func: impl FnOnce(&'a mut B) -> R ) -> Rwhere Self: BorrowMut<B>, B: 'a + ?Sized, R: 'a,
§fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> Rwhere
Self: AsRef<U>,
U: 'a + ?Sized,
R: 'a,
fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> Rwhere Self: AsRef<U>, U: 'a + ?Sized, R: 'a,
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) -> Rwhere
Self: AsMut<U>,
U: 'a + ?Sized,
R: 'a,
fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> Rwhere Self: AsMut<U>, U: 'a + ?Sized, R: 'a,
self
, then passes self.as_mut()
into the pipe
function.§impl<T> Tap for T
impl<T> Tap for T
§fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Selfwhere
Self: Borrow<B>,
B: ?Sized,
fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Selfwhere Self: Borrow<B>, B: ?Sized,
Borrow<B>
of a value. Read more§fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Selfwhere
Self: BorrowMut<B>,
B: ?Sized,
fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Selfwhere Self: BorrowMut<B>, B: ?Sized,
BorrowMut<B>
of a value. Read more§fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Selfwhere
Self: AsRef<R>,
R: ?Sized,
fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Selfwhere Self: AsRef<R>, R: ?Sized,
AsRef<R>
view of a value. Read more§fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Selfwhere
Self: AsMut<R>,
R: ?Sized,
fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Selfwhere Self: AsMut<R>, R: ?Sized,
AsMut<R>
view of a value. Read more§fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Selfwhere
Self: Deref<Target = T>,
T: ?Sized,
fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Selfwhere Self: Deref<Target = T>, T: ?Sized,
Deref::Target
of a value. Read more§fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Selfwhere
Self: DerefMut<Target = T> + Deref,
T: ?Sized,
fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Selfwhere Self: DerefMut<Target = T> + Deref, T: ?Sized,
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)) -> Selfwhere
Self: Borrow<B>,
B: ?Sized,
fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Selfwhere Self: Borrow<B>, B: ?Sized,
.tap_borrow()
only in debug builds, and is erased in release
builds.§fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Selfwhere
Self: BorrowMut<B>,
B: ?Sized,
fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Selfwhere Self: BorrowMut<B>, B: ?Sized,
.tap_borrow_mut()
only in debug builds, and is erased in release
builds.§fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Selfwhere
Self: AsRef<R>,
R: ?Sized,
fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Selfwhere Self: AsRef<R>, R: ?Sized,
.tap_ref()
only in debug builds, and is erased in release
builds.§fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Selfwhere
Self: AsMut<R>,
R: ?Sized,
fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Selfwhere Self: AsMut<R>, R: ?Sized,
.tap_ref_mut()
only in debug builds, and is erased in release
builds.§impl<T> TryConv for T
impl<T> TryConv for T
§impl<S, T> UncheckedInto<T> for Swhere
T: UncheckedFrom<S>,
impl<S, T> UncheckedInto<T> for Swhere T: UncheckedFrom<S>,
§fn unchecked_into(self) -> T
fn unchecked_into(self) -> T
unchecked_from
.