Add From impl

SignedAmount and Amount both share the same MAX, therefore the From impl
is added in place of TryFrom.
This commit is contained in:
yancy 2024-12-23 15:48:16 -06:00
parent 364e9ff775
commit e13355318e
4 changed files with 13 additions and 0 deletions

View File

@ -145,6 +145,7 @@ impl core::cmp::PartialOrd for bitcoin_units::locktime::relative::Height
impl core::cmp::PartialOrd for bitcoin_units::locktime::relative::Time
impl core::cmp::PartialOrd for bitcoin_units::weight::Weight
impl core::convert::AsRef<core::num::error::ParseIntError> for bitcoin_units::parse::ParseIntError
impl core::convert::From<bitcoin_units::Amount> for bitcoin_units::SignedAmount
impl core::convert::From<bitcoin_units::amount::InputTooLargeError> for bitcoin_units::amount::ParseAmountError
impl core::convert::From<bitcoin_units::amount::InputTooLargeError> for bitcoin_units::amount::ParseError
impl core::convert::From<bitcoin_units::amount::InvalidCharacterError> for bitcoin_units::amount::ParseAmountError
@ -855,6 +856,7 @@ pub fn bitcoin_units::SignedAmount::div(self, rhs: i64) -> Self::Output
pub fn bitcoin_units::SignedAmount::div_assign(&mut self, rhs: i64)
pub fn bitcoin_units::SignedAmount::eq(&self, other: &bitcoin_units::SignedAmount) -> bool
pub fn bitcoin_units::SignedAmount::fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result
pub fn bitcoin_units::SignedAmount::from(value: bitcoin_units::Amount) -> Self
pub fn bitcoin_units::SignedAmount::from_btc(btc: f64) -> core::result::Result<bitcoin_units::SignedAmount, bitcoin_units::amount::ParseAmountError>
pub fn bitcoin_units::SignedAmount::from_float_in(value: f64, denom: bitcoin_units::amount::Denomination) -> core::result::Result<bitcoin_units::SignedAmount, bitcoin_units::amount::ParseAmountError>
pub fn bitcoin_units::SignedAmount::from_int_btc<T: core::convert::Into<i64>>(whole_bitcoin: T) -> core::result::Result<bitcoin_units::SignedAmount, bitcoin_units::amount::OutOfRangeError>

View File

@ -141,6 +141,7 @@ impl core::cmp::PartialOrd for bitcoin_units::locktime::relative::Height
impl core::cmp::PartialOrd for bitcoin_units::locktime::relative::Time
impl core::cmp::PartialOrd for bitcoin_units::weight::Weight
impl core::convert::AsRef<core::num::error::ParseIntError> for bitcoin_units::parse::ParseIntError
impl core::convert::From<bitcoin_units::Amount> for bitcoin_units::SignedAmount
impl core::convert::From<bitcoin_units::amount::InputTooLargeError> for bitcoin_units::amount::ParseAmountError
impl core::convert::From<bitcoin_units::amount::InputTooLargeError> for bitcoin_units::amount::ParseError
impl core::convert::From<bitcoin_units::amount::InvalidCharacterError> for bitcoin_units::amount::ParseAmountError
@ -791,6 +792,7 @@ pub fn bitcoin_units::SignedAmount::div(self, rhs: i64) -> Self::Output
pub fn bitcoin_units::SignedAmount::div_assign(&mut self, rhs: i64)
pub fn bitcoin_units::SignedAmount::eq(&self, other: &bitcoin_units::SignedAmount) -> bool
pub fn bitcoin_units::SignedAmount::fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result
pub fn bitcoin_units::SignedAmount::from(value: bitcoin_units::Amount) -> Self
pub fn bitcoin_units::SignedAmount::from_btc(btc: f64) -> core::result::Result<bitcoin_units::SignedAmount, bitcoin_units::amount::ParseAmountError>
pub fn bitcoin_units::SignedAmount::from_float_in(value: f64, denom: bitcoin_units::amount::Denomination) -> core::result::Result<bitcoin_units::SignedAmount, bitcoin_units::amount::ParseAmountError>
pub fn bitcoin_units::SignedAmount::from_int_btc<T: core::convert::Into<i64>>(whole_bitcoin: T) -> core::result::Result<bitcoin_units::SignedAmount, bitcoin_units::amount::OutOfRangeError>

View File

@ -141,6 +141,7 @@ impl core::cmp::PartialOrd for bitcoin_units::locktime::relative::Height
impl core::cmp::PartialOrd for bitcoin_units::locktime::relative::Time
impl core::cmp::PartialOrd for bitcoin_units::weight::Weight
impl core::convert::AsRef<core::num::error::ParseIntError> for bitcoin_units::parse::ParseIntError
impl core::convert::From<bitcoin_units::Amount> for bitcoin_units::SignedAmount
impl core::convert::From<bitcoin_units::amount::InputTooLargeError> for bitcoin_units::amount::ParseAmountError
impl core::convert::From<bitcoin_units::amount::InputTooLargeError> for bitcoin_units::amount::ParseError
impl core::convert::From<bitcoin_units::amount::InvalidCharacterError> for bitcoin_units::amount::ParseAmountError
@ -767,6 +768,7 @@ pub fn bitcoin_units::SignedAmount::div(self, rhs: i64) -> Self::Output
pub fn bitcoin_units::SignedAmount::div_assign(&mut self, rhs: i64)
pub fn bitcoin_units::SignedAmount::eq(&self, other: &bitcoin_units::SignedAmount) -> bool
pub fn bitcoin_units::SignedAmount::fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result
pub fn bitcoin_units::SignedAmount::from(value: bitcoin_units::Amount) -> Self
pub fn bitcoin_units::SignedAmount::from_int_btc<T: core::convert::Into<i64>>(whole_bitcoin: T) -> core::result::Result<bitcoin_units::SignedAmount, bitcoin_units::amount::OutOfRangeError>
pub fn bitcoin_units::SignedAmount::from_str(s: &str) -> core::result::Result<Self, Self::Err>
pub fn bitcoin_units::SignedAmount::from_str_in(s: &str, denom: bitcoin_units::amount::Denomination) -> core::result::Result<bitcoin_units::SignedAmount, bitcoin_units::amount::ParseAmountError>

View File

@ -566,6 +566,13 @@ impl FromStr for SignedAmount {
}
}
impl From<Amount> for SignedAmount {
fn from(value: Amount) -> Self {
let v = value.to_sat() as i64; // Cast ok, signed amount and amount share positive range.
SignedAmount::from_sat_unchecked(v)
}
}
impl core::iter::Sum for SignedAmount {
fn sum<I: Iterator<Item = Self>>(iter: I) -> Self {
let sats: i64 = iter.map(|amt| amt.0).sum();