Change paramater type used for whole bitcoin
We have multiple constructors for `Amount`, one of which accepts an integer representing whole bitcoin. In an attempt to make the API crystal clear change the parameter type used for whole bitcoin to either `Into<u64>` or `u32` instead of a `u64` which may well be mapped to sats in the mind of developers (it is in mine anyway).
This commit is contained in:
parent
4d0f80f1fc
commit
99b32c95b8
|
@ -80,8 +80,8 @@ impl SignedAmount {
|
|||
///
|
||||
/// The function errors if the argument multiplied by the number of sats
|
||||
/// per bitcoin overflows an `i64` type.
|
||||
pub fn from_int_btc(btc: i64) -> Result<SignedAmount, OutOfRangeError> {
|
||||
match btc.checked_mul(100_000_000) {
|
||||
pub fn from_int_btc<T: Into<i64>>(whole_bitcoin: T) -> Result<SignedAmount, OutOfRangeError> {
|
||||
match whole_bitcoin.into().checked_mul(100_000_000) {
|
||||
Some(amount) => Ok(SignedAmount::from_sat(amount)),
|
||||
None => Err(OutOfRangeError { is_signed: true, is_greater_than_max: true }),
|
||||
}
|
||||
|
@ -94,8 +94,8 @@ impl SignedAmount {
|
|||
///
|
||||
/// The function panics if the argument multiplied by the number of sats
|
||||
/// per bitcoin overflows an `i64` type.
|
||||
pub const fn from_int_btc_const(btc: i64) -> SignedAmount {
|
||||
match btc.checked_mul(100_000_000) {
|
||||
pub const fn from_int_btc_const(whole_bitcoin: i64) -> SignedAmount {
|
||||
match whole_bitcoin.checked_mul(100_000_000) {
|
||||
Some(amount) => SignedAmount::from_sat(amount),
|
||||
None => panic!("checked_mul overflowed"),
|
||||
}
|
||||
|
|
|
@ -71,10 +71,6 @@ fn from_int_btc() {
|
|||
assert_eq!(Amount::from_sat(200_000_000), amt);
|
||||
}
|
||||
|
||||
#[should_panic]
|
||||
#[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);
|
||||
|
|
|
@ -101,8 +101,8 @@ impl Amount {
|
|||
///
|
||||
/// The function errors if the argument multiplied by the number of sats
|
||||
/// per bitcoin overflows a `u64` type.
|
||||
pub fn from_int_btc(btc: u64) -> Result<Amount, OutOfRangeError> {
|
||||
match btc.checked_mul(100_000_000) {
|
||||
pub fn from_int_btc<T: Into<u64>>(whole_bitcoin: T) -> Result<Amount, OutOfRangeError> {
|
||||
match whole_bitcoin.into().checked_mul(100_000_000) {
|
||||
Some(amount) => Ok(Amount::from_sat(amount)),
|
||||
None => Err(OutOfRangeError { is_signed: false, is_greater_than_max: true }),
|
||||
}
|
||||
|
@ -115,7 +115,8 @@ impl Amount {
|
|||
///
|
||||
/// The function panics if the argument multiplied by the number of sats
|
||||
/// per bitcoin overflows a `u64` type.
|
||||
pub const fn from_int_btc_const(btc: u64) -> Amount {
|
||||
pub const fn from_int_btc_const(whole_bitcoin: u32) -> Amount {
|
||||
let btc = whole_bitcoin as u64; // Can't call u64::from in const context.
|
||||
match btc.checked_mul(100_000_000) {
|
||||
Some(amount) => Amount::from_sat(amount),
|
||||
None => panic!("checked_mul overflowed"),
|
||||
|
|
Loading…
Reference in New Issue