Skip to content

Commit e744347

Browse files
committed
Make usage of Self and type uniform across both modules
This commit standardizes the function signatures in the Amount and SignedAmount implementations by consistently using Self as the return type instead of the concrete type names. This makes the code more consistent, easier to maintain, and follows Rust's idiomatic practices. Changes: Replace all occurrences of -> Amount with -> Self in unsigned.rs Replace all occurrences of -> SignedAmount with -> Self in signed.rs Make similar replacements for Option/Result return types Use Self:: instead of the explicit type name for static method calls
1 parent dfb49f0 commit e744347

File tree

2 files changed

+47
-47
lines changed

2 files changed

+47
-47
lines changed

units/src/amount/signed.rs

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,8 @@ mod encapsulate {
6161
///
6262
/// Accepts an `i32` which is guaranteed to be in range for the type, but which can only
6363
/// represent roughly -21.47 to 21.47 BTC.
64-
pub const fn from_sat_i32(satoshi: i32) -> SignedAmount {
65-
SignedAmount(satoshi as i64) // cannot use i64::from in a constfn
64+
pub const fn from_sat_i32(satoshi: i32) -> Self {
65+
Self(satoshi as i64) // cannot use i64::from in a constfn
6666
}
6767

6868
/// Gets the number of satoshis in this [`SignedAmount`].
@@ -90,7 +90,7 @@ mod encapsulate {
9090
/// assert_eq!(amount.to_sat(), sat);
9191
/// # Ok::<_, amount::OutOfRangeError>(())
9292
/// ```
93-
pub const fn from_sat(satoshi: i64) -> Result<SignedAmount, OutOfRangeError> {
93+
pub const fn from_sat(satoshi: i64) -> Result<Self, OutOfRangeError> {
9494
if satoshi < Self::MIN.to_sat() {
9595
Err(OutOfRangeError { is_signed: true, is_greater_than_max: false })
9696
} else if satoshi > Self::MAX_MONEY.to_sat() {
@@ -106,13 +106,13 @@ pub use encapsulate::SignedAmount;
106106

107107
impl SignedAmount {
108108
/// The zero amount.
109-
pub const ZERO: Self = SignedAmount::from_sat_i32(0);
109+
pub const ZERO: Self = Self::from_sat_i32(0);
110110
/// Exactly one satoshi.
111-
pub const ONE_SAT: Self = SignedAmount::from_sat_i32(1);
111+
pub const ONE_SAT: Self = Self::from_sat_i32(1);
112112
/// Exactly one bitcoin.
113-
pub const ONE_BTC: Self = SignedAmount::from_btc_i16(1);
113+
pub const ONE_BTC: Self = Self::from_btc_i16(1);
114114
/// Exactly fifty bitcoin.
115-
pub const FIFTY_BTC: Self = SignedAmount::from_btc_i16(50);
115+
pub const FIFTY_BTC: Self = Self::from_btc_i16(50);
116116
/// The maximum value allowed as an amount. Useful for sanity checking.
117117
pub const MAX_MONEY: Self = Self::MAX;
118118

@@ -133,24 +133,24 @@ impl SignedAmount {
133133
/// # Ok::<_, amount::ParseAmountError>(())
134134
/// ```
135135
#[cfg(feature = "alloc")]
136-
pub fn from_btc(btc: f64) -> Result<SignedAmount, ParseAmountError> {
136+
pub fn from_btc(btc: f64) -> Result<Self, ParseAmountError> {
137137
Self::from_float_in(btc, Denomination::Bitcoin)
138138
}
139139

140140
/// Converts from a value expressing a whole number of bitcoin to a [`SignedAmount`].
141141
#[allow(clippy::missing_panics_doc)]
142-
pub fn from_int_btc<T: Into<i16>>(whole_bitcoin: T) -> SignedAmount {
143-
SignedAmount::from_btc_i16(whole_bitcoin.into())
142+
pub fn from_int_btc<T: Into<i16>>(whole_bitcoin: T) -> Self {
143+
Self::from_btc_i16(whole_bitcoin.into())
144144
}
145145

146146
/// Converts from a value expressing a whole number of bitcoin to a [`SignedAmount`]
147147
/// in const context.
148148
#[allow(clippy::missing_panics_doc)]
149-
pub const fn from_btc_i16(whole_bitcoin: i16) -> SignedAmount {
149+
pub const fn from_btc_i16(whole_bitcoin: i16) -> Self {
150150
let btc = whole_bitcoin as i64; // Can't call `into` in const context.
151151
let sats = btc * 100_000_000;
152152

153-
match SignedAmount::from_sat(sats) {
153+
match Self::from_sat(sats) {
154154
Ok(amount) => amount,
155155
Err(_) => panic!("unreachable - 65536 BTC is within range"),
156156
}
@@ -164,7 +164,7 @@ impl SignedAmount {
164164
/// # Errors
165165
///
166166
/// If the amount is too big (positive or negative) or too precise.
167-
pub fn from_str_in(s: &str, denom: Denomination) -> Result<SignedAmount, ParseAmountError> {
167+
pub fn from_str_in(s: &str, denom: Denomination) -> Result<Self, ParseAmountError> {
168168
parse_signed_to_satoshi(s, denom)
169169
.map(|(_, amount)| amount)
170170
.map_err(|error| error.convert(true))
@@ -187,7 +187,7 @@ impl SignedAmount {
187187
/// assert_eq!(amount, SignedAmount::from_sat(10_000_000)?);
188188
/// # Ok::<_, amount::ParseError>(())
189189
/// ```
190-
pub fn from_str_with_denomination(s: &str) -> Result<SignedAmount, ParseError> {
190+
pub fn from_str_with_denomination(s: &str) -> Result<Self, ParseError> {
191191
let (amt, denom) = split_amount_and_denomination(s)?;
192192
Self::from_str_in(amt, denom).map_err(Into::into)
193193
}
@@ -236,7 +236,7 @@ impl SignedAmount {
236236
pub fn from_float_in(
237237
value: f64,
238238
denom: Denomination,
239-
) -> Result<SignedAmount, ParseAmountError> {
239+
) -> Result<Self, ParseAmountError> {
240240
// This is inefficient, but the safest way to deal with this. The parsing logic is safe.
241241
// Any performance-critical application should not be dealing with floats.
242242
Self::from_str_in(&value.to_string(), denom)
@@ -315,7 +315,7 @@ impl SignedAmount {
315315
///
316316
/// This function never overflows or panics, unlike `i64::abs()`.
317317
#[must_use]
318-
pub const fn abs(self) -> SignedAmount {
318+
pub const fn abs(self) -> Self {
319319
// `i64::abs()` can never overflow because SignedAmount::MIN == -MAX_MONEY.
320320
match Self::from_sat(self.to_sat().abs()) {
321321
Ok(amount) => amount,
@@ -358,16 +358,16 @@ impl SignedAmount {
358358
#[must_use]
359359
#[deprecated(since = "TBD", note = "Never returns none, use `abs()` instead")]
360360
#[allow(clippy::unnecessary_wraps)] // To match stdlib function definition.
361-
pub const fn checked_abs(self) -> Option<SignedAmount> { Some(self.abs()) }
361+
pub const fn checked_abs(self) -> Option<Self> { Some(self.abs()) }
362362

363363
/// Checked addition.
364364
///
365365
/// Returns [`None`] if the sum is above [`SignedAmount::MAX`] or below [`SignedAmount::MIN`].
366366
#[must_use]
367-
pub const fn checked_add(self, rhs: SignedAmount) -> Option<SignedAmount> {
367+
pub const fn checked_add(self, rhs: Self) -> Option<Self> {
368368
// No `map()` in const context.
369369
match self.to_sat().checked_add(rhs.to_sat()) {
370-
Some(res) => match SignedAmount::from_sat(res) {
370+
Some(res) => match Self::from_sat(res) {
371371
Ok(amount) => Some(amount),
372372
Err(_) => None,
373373
},
@@ -380,7 +380,7 @@ impl SignedAmount {
380380
/// Returns [`None`] if the difference is above [`SignedAmount::MAX`] or below
381381
/// [`SignedAmount::MIN`].
382382
#[must_use]
383-
pub const fn checked_sub(self, rhs: SignedAmount) -> Option<SignedAmount> {
383+
pub const fn checked_sub(self, rhs: Self) -> Option<Self> {
384384
// No `map()` in const context.
385385
match self.to_sat().checked_sub(rhs.to_sat()) {
386386
Some(res) => match Self::from_sat(res) {
@@ -396,7 +396,7 @@ impl SignedAmount {
396396
/// Returns [`None`] if the product is above [`SignedAmount::MAX`] or below
397397
/// [`SignedAmount::MIN`].
398398
#[must_use]
399-
pub const fn checked_mul(self, rhs: i64) -> Option<SignedAmount> {
399+
pub const fn checked_mul(self, rhs: i64) -> Option<Self> {
400400
// No `map()` in const context.
401401
match self.to_sat().checked_mul(rhs) {
402402
Some(res) => match Self::from_sat(res) {
@@ -413,7 +413,7 @@ impl SignedAmount {
413413
///
414414
/// Returns [`None`] if overflow occurred.
415415
#[must_use]
416-
pub const fn checked_div(self, rhs: i64) -> Option<SignedAmount> {
416+
pub const fn checked_div(self, rhs: i64) -> Option<Self> {
417417
// No `map()` in const context.
418418
match self.to_sat().checked_div(rhs) {
419419
Some(res) => match Self::from_sat(res) {
@@ -428,7 +428,7 @@ impl SignedAmount {
428428
///
429429
/// Returns [`None`] if overflow occurred.
430430
#[must_use]
431-
pub const fn checked_rem(self, rhs: i64) -> Option<SignedAmount> {
431+
pub const fn checked_rem(self, rhs: i64) -> Option<Self> {
432432
// No `map()` in const context.
433433
match self.to_sat().checked_rem(rhs) {
434434
Some(res) => match Self::from_sat(res) {
@@ -443,7 +443,7 @@ impl SignedAmount {
443443
///
444444
/// Returns [`None`] if either `self`, `rhs` or the result is strictly negative.
445445
#[must_use]
446-
pub fn positive_sub(self, rhs: SignedAmount) -> Option<SignedAmount> {
446+
pub fn positive_sub(self, rhs: Self) -> Option<Self> {
447447
if self.is_negative() || rhs.is_negative() || rhs > self {
448448
None
449449
} else {
@@ -469,7 +469,7 @@ impl SignedAmount {
469469
}
470470

471471
impl default::Default for SignedAmount {
472-
fn default() -> Self { SignedAmount::ZERO }
472+
fn default() -> Self { Self::ZERO }
473473
}
474474

475475
impl fmt::Debug for SignedAmount {

units/src/amount/unsigned.rs

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,8 @@ mod encapsulate {
6161
///
6262
/// Accepts an `u32` which is guaranteed to be in range for the type, but which can only
6363
/// represent roughly 0 to 42.95 BTC.
64-
pub const fn from_sat_u32(satoshi: u32) -> Amount {
65-
Amount(satoshi as u64) // cannot use u64::from in a constfn
64+
pub const fn from_sat_u32(satoshi: u32) -> Self {
65+
Self(satoshi as u64) // cannot use u64::from in a constfn
6666
}
6767

6868
/// Gets the number of satoshis in this [`Amount`].
@@ -90,7 +90,7 @@ mod encapsulate {
9090
/// assert_eq!(amount.to_sat(), sat);
9191
/// # Ok::<_, amount::OutOfRangeError>(())
9292
/// ```
93-
pub const fn from_sat(satoshi: u64) -> Result<Amount, OutOfRangeError> {
93+
pub const fn from_sat(satoshi: u64) -> Result<Self, OutOfRangeError> {
9494
if satoshi > Self::MAX_MONEY.to_sat() {
9595
Err(OutOfRangeError { is_signed: false, is_greater_than_max: true })
9696
} else {
@@ -104,15 +104,15 @@ pub use encapsulate::Amount;
104104

105105
impl Amount {
106106
/// The zero amount.
107-
pub const ZERO: Self = Amount::from_sat_u32(0);
107+
pub const ZERO: Self = Self::from_sat_u32(0);
108108
/// Exactly one satoshi.
109-
pub const ONE_SAT: Self = Amount::from_sat_u32(1);
109+
pub const ONE_SAT: Self = Self::from_sat_u32(1);
110110
/// Exactly one bitcoin.
111-
pub const ONE_BTC: Self = Amount::from_btc_u16(1);
111+
pub const ONE_BTC: Self = Self::from_btc_u16(1);
112112
/// Exactly fifty bitcoin.
113-
pub const FIFTY_BTC: Self = Amount::from_btc_u16(50);
113+
pub const FIFTY_BTC: Self = Self::from_btc_u16(50);
114114
/// The maximum value allowed as an amount. Useful for sanity checking.
115-
pub const MAX_MONEY: Self = Amount::MAX;
115+
pub const MAX_MONEY: Self = Self::MAX;
116116
/// The number of bytes that an amount contributes to the size of a transaction.
117117
pub const SIZE: usize = 8; // Serialized length of a u64.
118118

@@ -133,24 +133,24 @@ impl Amount {
133133
/// # Ok::<_, amount::ParseAmountError>(())
134134
/// ```
135135
#[cfg(feature = "alloc")]
136-
pub fn from_btc(btc: f64) -> Result<Amount, ParseAmountError> {
136+
pub fn from_btc(btc: f64) -> Result<Self, ParseAmountError> {
137137
Self::from_float_in(btc, Denomination::Bitcoin)
138138
}
139139

140140
/// Converts from a value expressing a whole number of bitcoin to an [`Amount`].
141141
#[allow(clippy::missing_panics_doc)]
142-
pub fn from_int_btc<T: Into<u16>>(whole_bitcoin: T) -> Amount {
143-
Amount::from_btc_u16(whole_bitcoin.into())
142+
pub fn from_int_btc<T: Into<u16>>(whole_bitcoin: T) -> Self {
143+
Self::from_btc_u16(whole_bitcoin.into())
144144
}
145145

146146
/// Converts from a value expressing a whole number of bitcoin to an [`Amount`]
147147
/// in const context.
148148
#[allow(clippy::missing_panics_doc)]
149-
pub const fn from_btc_u16(whole_bitcoin: u16) -> Amount {
149+
pub const fn from_btc_u16(whole_bitcoin: u16) -> Self {
150150
let btc = whole_bitcoin as u64; // Can't call `into` in const context.
151151
let sats = btc * 100_000_000;
152152

153-
match Amount::from_sat(sats) {
153+
match Self::from_sat(sats) {
154154
Ok(amount) => amount,
155155
Err(_) => panic!("unreachable - 65536 BTC is within range"),
156156
}
@@ -164,7 +164,7 @@ impl Amount {
164164
/// # Errors
165165
///
166166
/// If the amount is too precise, negative, or greater than 21,000,000.
167-
pub fn from_str_in(s: &str, denom: Denomination) -> Result<Amount, ParseAmountError> {
167+
pub fn from_str_in(s: &str, denom: Denomination) -> Result<Self, ParseAmountError> {
168168
let (is_neg, amount) =
169169
parse_signed_to_satoshi(s, denom).map_err(|error| error.convert(false))?;
170170
if is_neg {
@@ -192,7 +192,7 @@ impl Amount {
192192
/// assert_eq!(amount, Amount::from_sat(10_000_000)?);
193193
/// # Ok::<_, amount::ParseError>(())
194194
/// ```
195-
pub fn from_str_with_denomination(s: &str) -> Result<Amount, ParseError> {
195+
pub fn from_str_with_denomination(s: &str) -> Result<Self, ParseError> {
196196
let (amt, denom) = split_amount_and_denomination(s)?;
197197
Self::from_str_in(amt, denom).map_err(Into::into)
198198
}
@@ -238,7 +238,7 @@ impl Amount {
238238
///
239239
/// Please be aware of the risk of using floating-point numbers.
240240
#[cfg(feature = "alloc")]
241-
pub fn from_float_in(value: f64, denom: Denomination) -> Result<Amount, ParseAmountError> {
241+
pub fn from_float_in(value: f64, denom: Denomination) -> Result<Self, ParseAmountError> {
242242
if value < 0.0 {
243243
return Err(OutOfRangeError::negative().into());
244244
}
@@ -320,7 +320,7 @@ impl Amount {
320320
///
321321
/// Returns [`None`] if the sum is larger than [`Amount::MAX`].
322322
#[must_use]
323-
pub const fn checked_add(self, rhs: Amount) -> Option<Amount> {
323+
pub const fn checked_add(self, rhs: Self) -> Option<Self> {
324324
// No `map()` in const context.
325325
// Unchecked add ok, adding two values less than `MAX_MONEY` cannot overflow an `i64`.
326326
match Self::from_sat(self.to_sat() + rhs.to_sat()) {
@@ -333,7 +333,7 @@ impl Amount {
333333
///
334334
/// Returns [`None`] if overflow occurred.
335335
#[must_use]
336-
pub const fn checked_sub(self, rhs: Amount) -> Option<Amount> {
336+
pub const fn checked_sub(self, rhs: Self) -> Option<Self> {
337337
// No `map()` in const context.
338338
match self.to_sat().checked_sub(rhs.to_sat()) {
339339
Some(res) => match Self::from_sat(res) {
@@ -348,7 +348,7 @@ impl Amount {
348348
///
349349
/// Returns [`None`] if the product is larger than [`Amount::MAX`].
350350
#[must_use]
351-
pub const fn checked_mul(self, rhs: u64) -> Option<Amount> {
351+
pub const fn checked_mul(self, rhs: u64) -> Option<Self> {
352352
// No `map()` in const context.
353353
match self.to_sat().checked_mul(rhs) {
354354
Some(res) => match Self::from_sat(res) {
@@ -365,7 +365,7 @@ impl Amount {
365365
///
366366
/// Returns [`None`] if overflow occurred.
367367
#[must_use]
368-
pub const fn checked_div(self, rhs: u64) -> Option<Amount> {
368+
pub const fn checked_div(self, rhs: u64) -> Option<Self> {
369369
// No `map()` in const context.
370370
match self.to_sat().checked_div(rhs) {
371371
Some(res) => match Self::from_sat(res) {
@@ -380,7 +380,7 @@ impl Amount {
380380
///
381381
/// Returns [`None`] if overflow occurred.
382382
#[must_use]
383-
pub const fn checked_rem(self, rhs: u64) -> Option<Amount> {
383+
pub const fn checked_rem(self, rhs: u64) -> Option<Self> {
384384
// No `map()` in const context.
385385
match self.to_sat().checked_rem(rhs) {
386386
Some(res) => match Self::from_sat(res) {
@@ -401,7 +401,7 @@ impl Amount {
401401
}
402402

403403
impl default::Default for Amount {
404-
fn default() -> Self { Amount::ZERO }
404+
fn default() -> Self { Self::ZERO }
405405
}
406406

407407
impl fmt::Debug for Amount {

0 commit comments

Comments
 (0)