diff --git a/api/units/all-features.txt b/api/units/all-features.txt index d75534c5d0..f16214db3d 100644 --- a/api/units/all-features.txt +++ b/api/units/all-features.txt @@ -202,12 +202,12 @@ impl core::convert::TryFrom for bitcoin_units::locktime:: impl core::convert::TryFrom for bitcoin_units::locktime::relative::Height impl core::convert::TryFrom for bitcoin_units::locktime::relative::Time impl core::convert::TryFrom for bitcoin_units::weight::Weight -impl core::convert::TryFrom for bitcoin_units::SignedAmount impl core::convert::TryFrom for bitcoin_units::Amount impl core::convert::TryFrom for bitcoin_units::locktime::absolute::Height impl core::convert::TryFrom for bitcoin_units::locktime::relative::Height impl core::default::Default for bitcoin_units::Amount impl core::default::Default for bitcoin_units::SignedAmount +impl core::default::Default for bitcoin_units::block::BlockInterval impl core::default::Default for bitcoin_units::locktime::relative::Height impl core::default::Default for bitcoin_units::locktime::relative::Time impl core::error::Error for bitcoin_units::amount::error::InputTooLargeError @@ -737,7 +737,6 @@ pub const fn bitcoin_units::weight::Weight::from_vb_unchecked(vb: u64) -> Self pub const fn bitcoin_units::weight::Weight::from_vb_unwrap(vb: u64) -> bitcoin_units::weight::Weight pub const fn bitcoin_units::weight::Weight::from_witness_data_size(witness_size: u64) -> Self pub const fn bitcoin_units::weight::Weight::from_wu(wu: u64) -> Self -pub const fn bitcoin_units::weight::Weight::from_wu_usize(wu: usize) -> Self pub const fn bitcoin_units::weight::Weight::to_kwu_floor(self) -> u64 pub const fn bitcoin_units::weight::Weight::to_vbytes_ceil(self) -> u64 pub const fn bitcoin_units::weight::Weight::to_vbytes_floor(self) -> u64 @@ -788,7 +787,7 @@ pub fn bitcoin_units::Amount::sum(iter: I) -> Self where I: core::iter::traits::iterator::Iterator pub fn bitcoin_units::Amount::to_btc(self) -> f64 pub fn bitcoin_units::Amount::to_float_in(self, denom: bitcoin_units::amount::Denomination) -> f64 -pub fn bitcoin_units::Amount::to_signed(self) -> core::result::Result +pub fn bitcoin_units::Amount::to_signed(self) -> bitcoin_units::SignedAmount pub fn bitcoin_units::Amount::to_string_in(self, denom: bitcoin_units::amount::Denomination) -> alloc::string::String pub fn bitcoin_units::Amount::to_string_with_denomination(self, denom: bitcoin_units::amount::Denomination) -> alloc::string::String pub fn bitcoin_units::Amount::try_from(value: bitcoin_units::SignedAmount) -> core::result::Result @@ -843,7 +842,6 @@ pub fn bitcoin_units::SignedAmount::to_float_in(self, denom: bitcoin_units::amou pub fn bitcoin_units::SignedAmount::to_string_in(self, denom: bitcoin_units::amount::Denomination) -> alloc::string::String pub fn bitcoin_units::SignedAmount::to_string_with_denomination(self, denom: bitcoin_units::amount::Denomination) -> alloc::string::String pub fn bitcoin_units::SignedAmount::to_unsigned(self) -> core::result::Result -pub fn bitcoin_units::SignedAmount::try_from(value: bitcoin_units::Amount) -> core::result::Result pub fn bitcoin_units::SignedAmount::type_prefix(_: private::Token) -> &'static str pub fn bitcoin_units::SignedAmount::unchecked_add(self, rhs: bitcoin_units::SignedAmount) -> bitcoin_units::SignedAmount pub fn bitcoin_units::SignedAmount::unchecked_sub(self, rhs: bitcoin_units::SignedAmount) -> bitcoin_units::SignedAmount @@ -956,6 +954,7 @@ pub fn bitcoin_units::block::BlockInterval::add(self, rhs: bitcoin_units::block: pub fn bitcoin_units::block::BlockInterval::add_assign(&mut self, rhs: bitcoin_units::block::BlockInterval) pub fn bitcoin_units::block::BlockInterval::clone(&self) -> bitcoin_units::block::BlockInterval pub fn bitcoin_units::block::BlockInterval::cmp(&self, other: &bitcoin_units::block::BlockInterval) -> core::cmp::Ordering +pub fn bitcoin_units::block::BlockInterval::default() -> bitcoin_units::block::BlockInterval pub fn bitcoin_units::block::BlockInterval::deserialize<__D>(__deserializer: __D) -> core::result::Result::Error> where __D: serde::de::Deserializer<'de> pub fn bitcoin_units::block::BlockInterval::eq(&self, other: &bitcoin_units::block::BlockInterval) -> bool pub fn bitcoin_units::block::BlockInterval::fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result @@ -1191,17 +1190,16 @@ pub struct bitcoin_units::locktime::relative::TimeOverflowError pub struct bitcoin_units::parse::PrefixedHexError(_) pub struct bitcoin_units::parse::UnprefixedHexError(_) pub struct bitcoin_units::weight::Weight(_) -pub trait bitcoin_units::amount::CheckedSum: private::SumSeal +pub trait bitcoin_units::amount::CheckedSum: sealed::Sealed pub trait bitcoin_units::amount::serde::SerdeAmount: core::marker::Copy + core::marker::Sized pub trait bitcoin_units::amount::serde::SerdeAmountForOpt: core::marker::Copy + core::marker::Sized + bitcoin_units::amount::serde::SerdeAmount -pub trait bitcoin_units::parse::Integer: core::str::traits::FromStr + core::convert::TryFrom + core::marker::Sized +pub trait bitcoin_units::parse::Integer: core::str::traits::FromStr + core::convert::TryFrom + core::marker::Sized + sealed::Sealed pub type &bitcoin_units::fee_rate::FeeRate::Output = bitcoin_units::fee_rate::FeeRate pub type bitcoin_units::Amount::Err = bitcoin_units::amount::error::ParseError pub type bitcoin_units::Amount::Error = bitcoin_units::amount::error::OutOfRangeError pub type bitcoin_units::Amount::Output = bitcoin_units::Amount pub type bitcoin_units::Amount::Output = bitcoin_units::fee_rate::FeeRate pub type bitcoin_units::SignedAmount::Err = bitcoin_units::amount::error::ParseError -pub type bitcoin_units::SignedAmount::Error = bitcoin_units::amount::error::OutOfRangeError pub type bitcoin_units::SignedAmount::Output = bitcoin_units::SignedAmount pub type bitcoin_units::amount::Denomination::Err = bitcoin_units::amount::error::ParseDenominationError pub type bitcoin_units::block::BlockHeight::Err = bitcoin_units::parse::ParseIntError diff --git a/api/units/alloc-only.txt b/api/units/alloc-only.txt index 21cc554a19..66e2c71663 100644 --- a/api/units/alloc-only.txt +++ b/api/units/alloc-only.txt @@ -198,12 +198,12 @@ impl core::convert::TryFrom for bitcoin_units::locktime:: impl core::convert::TryFrom for bitcoin_units::locktime::relative::Height impl core::convert::TryFrom for bitcoin_units::locktime::relative::Time impl core::convert::TryFrom for bitcoin_units::weight::Weight -impl core::convert::TryFrom for bitcoin_units::SignedAmount impl core::convert::TryFrom for bitcoin_units::Amount impl core::convert::TryFrom for bitcoin_units::locktime::absolute::Height impl core::convert::TryFrom for bitcoin_units::locktime::relative::Height impl core::default::Default for bitcoin_units::Amount impl core::default::Default for bitcoin_units::SignedAmount +impl core::default::Default for bitcoin_units::block::BlockInterval impl core::default::Default for bitcoin_units::locktime::relative::Height impl core::default::Default for bitcoin_units::locktime::relative::Time impl core::fmt::Debug for bitcoin_units::Amount @@ -695,7 +695,6 @@ pub const fn bitcoin_units::weight::Weight::from_vb_unchecked(vb: u64) -> Self pub const fn bitcoin_units::weight::Weight::from_vb_unwrap(vb: u64) -> bitcoin_units::weight::Weight pub const fn bitcoin_units::weight::Weight::from_witness_data_size(witness_size: u64) -> Self pub const fn bitcoin_units::weight::Weight::from_wu(wu: u64) -> Self -pub const fn bitcoin_units::weight::Weight::from_wu_usize(wu: usize) -> Self pub const fn bitcoin_units::weight::Weight::to_kwu_floor(self) -> u64 pub const fn bitcoin_units::weight::Weight::to_vbytes_ceil(self) -> u64 pub const fn bitcoin_units::weight::Weight::to_vbytes_floor(self) -> u64 @@ -736,7 +735,7 @@ pub fn bitcoin_units::Amount::sum(iter: I) -> Self where I: core::iter::traits::iterator::Iterator pub fn bitcoin_units::Amount::to_btc(self) -> f64 pub fn bitcoin_units::Amount::to_float_in(self, denom: bitcoin_units::amount::Denomination) -> f64 -pub fn bitcoin_units::Amount::to_signed(self) -> core::result::Result +pub fn bitcoin_units::Amount::to_signed(self) -> bitcoin_units::SignedAmount pub fn bitcoin_units::Amount::to_string_in(self, denom: bitcoin_units::amount::Denomination) -> alloc::string::String pub fn bitcoin_units::Amount::to_string_with_denomination(self, denom: bitcoin_units::amount::Denomination) -> alloc::string::String pub fn bitcoin_units::Amount::try_from(value: bitcoin_units::SignedAmount) -> core::result::Result @@ -780,7 +779,6 @@ pub fn bitcoin_units::SignedAmount::to_float_in(self, denom: bitcoin_units::amou pub fn bitcoin_units::SignedAmount::to_string_in(self, denom: bitcoin_units::amount::Denomination) -> alloc::string::String pub fn bitcoin_units::SignedAmount::to_string_with_denomination(self, denom: bitcoin_units::amount::Denomination) -> alloc::string::String pub fn bitcoin_units::SignedAmount::to_unsigned(self) -> core::result::Result -pub fn bitcoin_units::SignedAmount::try_from(value: bitcoin_units::Amount) -> core::result::Result pub fn bitcoin_units::SignedAmount::unchecked_add(self, rhs: bitcoin_units::SignedAmount) -> bitcoin_units::SignedAmount pub fn bitcoin_units::SignedAmount::unchecked_sub(self, rhs: bitcoin_units::SignedAmount) -> bitcoin_units::SignedAmount pub fn bitcoin_units::SignedAmount::unsigned_abs(self) -> bitcoin_units::Amount @@ -863,6 +861,7 @@ pub fn bitcoin_units::block::BlockInterval::add(self, rhs: bitcoin_units::block: pub fn bitcoin_units::block::BlockInterval::add_assign(&mut self, rhs: bitcoin_units::block::BlockInterval) pub fn bitcoin_units::block::BlockInterval::clone(&self) -> bitcoin_units::block::BlockInterval pub fn bitcoin_units::block::BlockInterval::cmp(&self, other: &bitcoin_units::block::BlockInterval) -> core::cmp::Ordering +pub fn bitcoin_units::block::BlockInterval::default() -> bitcoin_units::block::BlockInterval pub fn bitcoin_units::block::BlockInterval::eq(&self, other: &bitcoin_units::block::BlockInterval) -> bool pub fn bitcoin_units::block::BlockInterval::fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result pub fn bitcoin_units::block::BlockInterval::from(h: bitcoin_units::locktime::relative::Height) -> Self @@ -1069,15 +1068,14 @@ pub struct bitcoin_units::locktime::relative::TimeOverflowError pub struct bitcoin_units::parse::PrefixedHexError(_) pub struct bitcoin_units::parse::UnprefixedHexError(_) pub struct bitcoin_units::weight::Weight(_) -pub trait bitcoin_units::amount::CheckedSum: private::SumSeal -pub trait bitcoin_units::parse::Integer: core::str::traits::FromStr + core::convert::TryFrom + core::marker::Sized +pub trait bitcoin_units::amount::CheckedSum: sealed::Sealed +pub trait bitcoin_units::parse::Integer: core::str::traits::FromStr + core::convert::TryFrom + core::marker::Sized + sealed::Sealed pub type &bitcoin_units::fee_rate::FeeRate::Output = bitcoin_units::fee_rate::FeeRate pub type bitcoin_units::Amount::Err = bitcoin_units::amount::error::ParseError pub type bitcoin_units::Amount::Error = bitcoin_units::amount::error::OutOfRangeError pub type bitcoin_units::Amount::Output = bitcoin_units::Amount pub type bitcoin_units::Amount::Output = bitcoin_units::fee_rate::FeeRate pub type bitcoin_units::SignedAmount::Err = bitcoin_units::amount::error::ParseError -pub type bitcoin_units::SignedAmount::Error = bitcoin_units::amount::error::OutOfRangeError pub type bitcoin_units::SignedAmount::Output = bitcoin_units::SignedAmount pub type bitcoin_units::amount::Denomination::Err = bitcoin_units::amount::error::ParseDenominationError pub type bitcoin_units::block::BlockHeight::Err = bitcoin_units::parse::ParseIntError diff --git a/api/units/no-features.txt b/api/units/no-features.txt index 638a07add3..8b2044b3a9 100644 --- a/api/units/no-features.txt +++ b/api/units/no-features.txt @@ -182,12 +182,12 @@ impl core::convert::TryFrom<&str> for bitcoin_units::locktime::absolute::Time impl core::convert::TryFrom<&str> for bitcoin_units::locktime::relative::Height impl core::convert::TryFrom<&str> for bitcoin_units::locktime::relative::Time impl core::convert::TryFrom<&str> for bitcoin_units::weight::Weight -impl core::convert::TryFrom for bitcoin_units::SignedAmount impl core::convert::TryFrom for bitcoin_units::Amount impl core::convert::TryFrom for bitcoin_units::locktime::absolute::Height impl core::convert::TryFrom for bitcoin_units::locktime::relative::Height impl core::default::Default for bitcoin_units::Amount impl core::default::Default for bitcoin_units::SignedAmount +impl core::default::Default for bitcoin_units::block::BlockInterval impl core::default::Default for bitcoin_units::locktime::relative::Height impl core::default::Default for bitcoin_units::locktime::relative::Time impl core::fmt::Debug for bitcoin_units::Amount @@ -677,7 +677,6 @@ pub const fn bitcoin_units::weight::Weight::from_vb_unchecked(vb: u64) -> Self pub const fn bitcoin_units::weight::Weight::from_vb_unwrap(vb: u64) -> bitcoin_units::weight::Weight pub const fn bitcoin_units::weight::Weight::from_witness_data_size(witness_size: u64) -> Self pub const fn bitcoin_units::weight::Weight::from_wu(wu: u64) -> Self -pub const fn bitcoin_units::weight::Weight::from_wu_usize(wu: usize) -> Self pub const fn bitcoin_units::weight::Weight::to_kwu_floor(self) -> u64 pub const fn bitcoin_units::weight::Weight::to_vbytes_ceil(self) -> u64 pub const fn bitcoin_units::weight::Weight::to_vbytes_floor(self) -> u64 @@ -714,7 +713,7 @@ pub fn bitcoin_units::Amount::sub(self, rhs: bitcoin_units::Amount) -> Self::Out pub fn bitcoin_units::Amount::sub_assign(&mut self, other: bitcoin_units::Amount) pub fn bitcoin_units::Amount::sum>(iter: I) -> Self pub fn bitcoin_units::Amount::sum(iter: I) -> Self where I: core::iter::traits::iterator::Iterator -pub fn bitcoin_units::Amount::to_signed(self) -> core::result::Result +pub fn bitcoin_units::Amount::to_signed(self) -> bitcoin_units::SignedAmount pub fn bitcoin_units::Amount::try_from(value: bitcoin_units::SignedAmount) -> core::result::Result pub fn bitcoin_units::Amount::unchecked_add(self, rhs: bitcoin_units::Amount) -> bitcoin_units::Amount pub fn bitcoin_units::Amount::unchecked_sub(self, rhs: bitcoin_units::Amount) -> bitcoin_units::Amount @@ -750,7 +749,6 @@ pub fn bitcoin_units::SignedAmount::sub_assign(&mut self, other: bitcoin_units:: pub fn bitcoin_units::SignedAmount::sum>(iter: I) -> Self pub fn bitcoin_units::SignedAmount::sum(iter: I) -> Self where I: core::iter::traits::iterator::Iterator pub fn bitcoin_units::SignedAmount::to_unsigned(self) -> core::result::Result -pub fn bitcoin_units::SignedAmount::try_from(value: bitcoin_units::Amount) -> core::result::Result pub fn bitcoin_units::SignedAmount::unchecked_add(self, rhs: bitcoin_units::SignedAmount) -> bitcoin_units::SignedAmount pub fn bitcoin_units::SignedAmount::unchecked_sub(self, rhs: bitcoin_units::SignedAmount) -> bitcoin_units::SignedAmount pub fn bitcoin_units::SignedAmount::unsigned_abs(self) -> bitcoin_units::Amount @@ -831,6 +829,7 @@ pub fn bitcoin_units::block::BlockInterval::add(self, rhs: bitcoin_units::block: pub fn bitcoin_units::block::BlockInterval::add_assign(&mut self, rhs: bitcoin_units::block::BlockInterval) pub fn bitcoin_units::block::BlockInterval::clone(&self) -> bitcoin_units::block::BlockInterval pub fn bitcoin_units::block::BlockInterval::cmp(&self, other: &bitcoin_units::block::BlockInterval) -> core::cmp::Ordering +pub fn bitcoin_units::block::BlockInterval::default() -> bitcoin_units::block::BlockInterval pub fn bitcoin_units::block::BlockInterval::eq(&self, other: &bitcoin_units::block::BlockInterval) -> bool pub fn bitcoin_units::block::BlockInterval::fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result pub fn bitcoin_units::block::BlockInterval::from(h: bitcoin_units::locktime::relative::Height) -> Self @@ -1023,15 +1022,14 @@ pub struct bitcoin_units::locktime::relative::TimeOverflowError pub struct bitcoin_units::parse::PrefixedHexError(_) pub struct bitcoin_units::parse::UnprefixedHexError(_) pub struct bitcoin_units::weight::Weight(_) -pub trait bitcoin_units::amount::CheckedSum: private::SumSeal -pub trait bitcoin_units::parse::Integer: core::str::traits::FromStr + core::convert::TryFrom + core::marker::Sized +pub trait bitcoin_units::amount::CheckedSum: sealed::Sealed +pub trait bitcoin_units::parse::Integer: core::str::traits::FromStr + core::convert::TryFrom + core::marker::Sized + sealed::Sealed pub type &bitcoin_units::fee_rate::FeeRate::Output = bitcoin_units::fee_rate::FeeRate pub type bitcoin_units::Amount::Err = bitcoin_units::amount::error::ParseError pub type bitcoin_units::Amount::Error = bitcoin_units::amount::error::OutOfRangeError pub type bitcoin_units::Amount::Output = bitcoin_units::Amount pub type bitcoin_units::Amount::Output = bitcoin_units::fee_rate::FeeRate pub type bitcoin_units::SignedAmount::Err = bitcoin_units::amount::error::ParseError -pub type bitcoin_units::SignedAmount::Error = bitcoin_units::amount::error::OutOfRangeError pub type bitcoin_units::SignedAmount::Output = bitcoin_units::SignedAmount pub type bitcoin_units::amount::Denomination::Err = bitcoin_units::amount::error::ParseDenominationError pub type bitcoin_units::block::BlockHeight::Err = bitcoin_units::parse::ParseIntError diff --git a/bitcoin/src/blockdata/transaction.rs b/bitcoin/src/blockdata/transaction.rs index a333c21975..67858962ee 100644 --- a/bitcoin/src/blockdata/transaction.rs +++ b/bitcoin/src/blockdata/transaction.rs @@ -790,8 +790,8 @@ pub fn effective_value( value: Amount, ) -> Option { let weight = satisfaction_weight.checked_add(TX_IN_BASE_WEIGHT)?; - let signed_input_fee = fee_rate.checked_mul_by_weight(weight)?.to_signed().ok()?; - value.to_signed().ok()?.checked_sub(signed_input_fee) + let signed_input_fee = fee_rate.checked_mul_by_weight(weight)?.to_signed(); + value.to_signed().checked_sub(signed_input_fee) } /// Predicts the weight of a to-be-constructed transaction. @@ -1673,7 +1673,7 @@ mod tests { // 10 sat/kwu * (204wu + BASE_WEIGHT) = 4 sats let expected_fee = "4 sats".parse::().unwrap(); - let expected_effective_value = value.to_signed().unwrap() - expected_fee; + let expected_effective_value = value.to_signed() - expected_fee; assert_eq!(effective_value, expected_effective_value); } diff --git a/units/src/amount/signed.rs b/units/src/amount/signed.rs index d88905dbae..aa4f993dda 100644 --- a/units/src/amount/signed.rs +++ b/units/src/amount/signed.rs @@ -469,12 +469,6 @@ impl FromStr for SignedAmount { } } -impl TryFrom for SignedAmount { - type Error = OutOfRangeError; - - fn try_from(value: Amount) -> Result { value.to_signed() } -} - impl core::iter::Sum for SignedAmount { fn sum>(iter: I) -> Self { let sats: i64 = iter.map(|amt| amt.0).sum(); diff --git a/units/src/amount/tests.rs b/units/src/amount/tests.rs index ca2774750e..b10bba16e8 100644 --- a/units/src/amount/tests.rs +++ b/units/src/amount/tests.rs @@ -75,13 +75,6 @@ fn from_int_btc() { #[test] fn from_int_btc_panic() { Amount::from_int_btc_const(u64::MAX); } -#[test] -fn test_signed_amount_try_from_amount() { - let ua_positive = Amount::from_sat(123); - let sa_positive = SignedAmount::try_from(ua_positive).unwrap(); - assert_eq!(sa_positive, SignedAmount::from_sat(123)); -} - #[test] fn test_amount_try_from_signed_amount() { let sa_positive = SignedAmount::from_sat(123); @@ -501,12 +494,11 @@ fn test_unsigned_signed_conversion() { let ua = Amount::from_sat; let max_sats: u64 = Amount::MAX.to_sat(); - assert_eq!(ua(max_sats).to_signed(), Ok(sa(max_sats as i64))); - assert_eq!(ua(i64::MAX as u64 + 1).to_signed(), Err(OutOfRangeError::too_big(true))); + assert_eq!(ua(max_sats).to_signed(), sa(max_sats as i64)); assert_eq!(sa(max_sats as i64).to_unsigned(), Ok(ua(max_sats))); - assert_eq!(sa(max_sats as i64).to_unsigned().unwrap().to_signed(), Ok(sa(max_sats as i64))); + assert_eq!(sa(max_sats as i64).to_unsigned().unwrap().to_signed(), sa(max_sats as i64)); } #[test] diff --git a/units/src/amount/unsigned.rs b/units/src/amount/unsigned.rs index f51ae4af35..b24091ac72 100644 --- a/units/src/amount/unsigned.rs +++ b/units/src/amount/unsigned.rs @@ -341,12 +341,8 @@ impl Amount { pub fn unchecked_sub(self, rhs: Amount) -> Amount { Self(self.0 - rhs.0) } /// Converts to a signed amount. - pub fn to_signed(self) -> Result { - if self.to_sat() > SignedAmount::MAX.to_sat() as u64 { - Err(OutOfRangeError::too_big(true)) - } else { - Ok(SignedAmount::from_sat(self.to_sat() as i64)) - } + pub fn to_signed(self) -> SignedAmount { + SignedAmount::from_sat(self.to_sat() as i64) } /// Checks if the amount is below the maximum value. Returns `None` if it is above.