Skip to content

Commit

Permalink
Use Self instead of amount type
Browse files Browse the repository at this point in the history
I claim that if the two amount modules are coded as similarly as
possible it will be easier to ensure that we have the API's uniform and
bug free. To make auditing the modules easier and less error prone use
`Self` instead of the explicit type. This makes it easier to see
differences in the modules and to ensure the differences are correct and
required.

Internal change, no logic changes whatsoever.
  • Loading branch information
tcharding committed Jan 24, 2025
1 parent 0c2dd88 commit fcd379b
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 24 deletions.
24 changes: 12 additions & 12 deletions units/src/amount/signed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ impl SignedAmount {
/// ```
#[cfg(feature = "alloc")]
pub fn from_btc(btc: f64) -> Result<SignedAmount, ParseAmountError> {
SignedAmount::from_float_in(btc, Denomination::Bitcoin)
Self::from_float_in(btc, Denomination::Bitcoin)
}

/// Converts from a value expressing a whole number of bitcoin to a [`SignedAmount`].
Expand All @@ -120,7 +120,7 @@ impl SignedAmount {
/// per bitcoin overflows an `i64` type.
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)),
Some(amount) => Ok(Self::from_sat(amount)),
None => Err(OutOfRangeError { is_signed: true, is_greater_than_max: true }),
}
}
Expand Down Expand Up @@ -187,7 +187,7 @@ impl SignedAmount {
/// ```
pub fn from_str_with_denomination(s: &str) -> Result<SignedAmount, ParseError> {
let (amt, denom) = split_amount_and_denomination(s)?;
SignedAmount::from_str_in(amt, denom).map_err(Into::into)
Self::from_str_in(amt, denom).map_err(Into::into)
}

/// Expresses this [`SignedAmount`] as a floating-point value in the given [`Denomination`].
Expand Down Expand Up @@ -235,7 +235,7 @@ impl SignedAmount {
) -> Result<SignedAmount, ParseAmountError> {
// This is inefficient, but the safest way to deal with this. The parsing logic is safe.
// Any performance-critical application should not be dealing with floats.
SignedAmount::from_str_in(&value.to_string(), denom)
Self::from_str_in(&value.to_string(), denom)
}

/// Constructs a new object that implements [`fmt::Display`] in the given [`Denomination`].
Expand Down Expand Up @@ -555,14 +555,14 @@ impl FromStr for SignedAmount {
///
/// If the returned value would be zero or negative zero, then no denomination is required.
fn from_str(s: &str) -> Result<Self, Self::Err> {
let result = SignedAmount::from_str_with_denomination(s);
let result = Self::from_str_with_denomination(s);

match result {
Err(ParseError(ParseErrorInner::MissingDenomination(_))) => {
let d = SignedAmount::from_str_in(s, Denomination::Satoshi);
let d = Self::from_str_in(s, Denomination::Satoshi);

if d == Ok(SignedAmount::ZERO) {
Ok(SignedAmount::ZERO)
if d == Ok(Self::ZERO) {
Ok(Self::ZERO)
} else {
result
}
Expand All @@ -575,14 +575,14 @@ 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)
Self::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();
SignedAmount::from_sat(sats)
Self::from_sat(sats)
}
}

Expand All @@ -592,14 +592,14 @@ impl<'a> core::iter::Sum<&'a SignedAmount> for SignedAmount {
I: Iterator<Item = &'a SignedAmount>,
{
let sats: i64 = iter.map(|amt| amt.0).sum();
SignedAmount::from_sat(sats)
Self::from_sat(sats)
}
}

#[cfg(feature = "arbitrary")]
impl<'a> Arbitrary<'a> for SignedAmount {
fn arbitrary(u: &mut Unstructured<'a>) -> arbitrary::Result<Self> {
let s = i64::arbitrary(u)?;
Ok(SignedAmount(s))
Ok(Self(s))
}
}
24 changes: 12 additions & 12 deletions units/src/amount/unsigned.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ impl Amount {
/// ```
#[cfg(feature = "alloc")]
pub fn from_btc(btc: f64) -> Result<Amount, ParseAmountError> {
Amount::from_float_in(btc, Denomination::Bitcoin)
Self::from_float_in(btc, Denomination::Bitcoin)
}

/// Converts from a value expressing a whole number of bitcoin to an [`Amount`].
Expand All @@ -123,7 +123,7 @@ impl Amount {
/// per bitcoin overflows a `u64` type.
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)),
Some(amount) => Ok(Self::from_sat(amount)),
None => Err(OutOfRangeError { is_signed: false, is_greater_than_max: true }),
}
}
Expand Down Expand Up @@ -164,7 +164,7 @@ impl Amount {
OutOfRangeError::too_big(false),
)));
}
Ok(Amount::from_sat(sats))
Ok(Self::from_sat(sats))
}

/// Parses amounts with denomination suffix as produced by [`Self::to_string_with_denomination`]
Expand All @@ -186,7 +186,7 @@ impl Amount {
/// ```
pub fn from_str_with_denomination(s: &str) -> Result<Amount, ParseError> {
let (amt, denom) = split_amount_and_denomination(s)?;
Amount::from_str_in(amt, denom).map_err(Into::into)
Self::from_str_in(amt, denom).map_err(Into::into)
}

/// Expresses this [`Amount`] as a floating-point value in the given [`Denomination`].
Expand Down Expand Up @@ -234,7 +234,7 @@ impl Amount {
}
// This is inefficient, but the safest way to deal with this. The parsing logic is safe.
// Any performance-critical application should not be dealing with floats.
Amount::from_str_in(&value.to_string(), denom)
Self::from_str_in(&value.to_string(), denom)
}

/// Constructs a new object that implements [`fmt::Display`] in the given [`Denomination`].
Expand Down Expand Up @@ -483,14 +483,14 @@ impl FromStr for Amount {
///
/// If the returned value would be zero or negative zero, then no denomination is required.
fn from_str(s: &str) -> Result<Self, Self::Err> {
let result = Amount::from_str_with_denomination(s);
let result = Self::from_str_with_denomination(s);

match result {
Err(ParseError(ParseErrorInner::MissingDenomination(_))) => {
let d = Amount::from_str_in(s, Denomination::Satoshi);
let d = Self::from_str_in(s, Denomination::Satoshi);

if d == Ok(Amount::ZERO) {
Ok(Amount::ZERO)
if d == Ok(Self::ZERO) {
Ok(Self::ZERO)
} else {
result
}
Expand All @@ -509,7 +509,7 @@ impl TryFrom<SignedAmount> for Amount {
impl core::iter::Sum for Amount {
fn sum<I: Iterator<Item = Self>>(iter: I) -> Self {
let sats: u64 = iter.map(|amt| amt.0).sum();
Amount::from_sat(sats)
Self::from_sat(sats)
}
}

Expand All @@ -519,14 +519,14 @@ impl<'a> core::iter::Sum<&'a Amount> for Amount {
I: Iterator<Item = &'a Amount>,
{
let sats: u64 = iter.map(|amt| amt.0).sum();
Amount::from_sat(sats)
Self::from_sat(sats)
}
}

#[cfg(feature = "arbitrary")]
impl<'a> Arbitrary<'a> for Amount {
fn arbitrary(u: &mut Unstructured<'a>) -> arbitrary::Result<Self> {
let a = u64::arbitrary(u)?;
Ok(Amount(a))
Ok(Self(a))
}
}

0 comments on commit fcd379b

Please sign in to comment.