Skip to content

Commit af63b80

Browse files
committed
Use fn name to_ instead of as_
Rust convention is to use `to_` for conversion methods that convert from an owned type to an owned `Copy` type. `as_` is for borrowed to borrowed types. Re-name and deprecate conversion methods that use `as_` for owned to owned `Copy` types to use `to_`.
1 parent 8393083 commit af63b80

File tree

4 files changed

+60
-19
lines changed

4 files changed

+60
-19
lines changed

src/blockdata/script.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -644,7 +644,7 @@ impl Script {
644644
#[cfg(feature="bitcoinconsensus")]
645645
#[cfg_attr(docsrs, doc(cfg(feature = "bitcoinconsensus")))]
646646
pub fn verify_with_flags<F: Into<u32>>(&self, index: usize, amount: crate::Amount, spending: &[u8], flags: F) -> Result<(), Error> {
647-
Ok(bitcoinconsensus::verify_with_flags (&self.0[..], amount.as_sat(), spending, index, flags.into())?)
647+
Ok(bitcoinconsensus::verify_with_flags (&self.0[..], amount.to_sat(), spending, index, flags.into())?)
648648
}
649649

650650
/// Writes the assembly decoding of the script bytes to the formatter.

src/network/address.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ impl Encodable for AddrV2Message {
267267
fn consensus_encode<W: io::Write>(&self, mut e: W) -> Result<usize, io::Error> {
268268
let mut len = 0;
269269
len += self.time.consensus_encode(&mut e)?;
270-
len += VarInt(self.services.as_u64()).consensus_encode(&mut e)?;
270+
len += VarInt(self.services.to_u64()).consensus_encode(&mut e)?;
271271
len += self.addr.consensus_encode(&mut e)?;
272272

273273
// consensus_encode always encodes in LE, and we want to encode in BE.

src/network/constants.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,13 @@ impl ServiceFlags {
178178
}
179179

180180
/// Get the integer representation of this [ServiceFlags].
181+
#[deprecated(since = "0.29.0", note = "use to_u64 instead")]
181182
pub fn as_u64(self) -> u64 {
183+
self.to_u64()
184+
}
185+
186+
/// Gets the integer representation of this [`ServiceFlags`].
187+
pub fn to_u64(self) -> u64 {
182188
self.0
183189
}
184190
}

src/util/amount.rs

Lines changed: 52 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -484,7 +484,13 @@ impl Amount {
484484
}
485485

486486
/// Get the number of satoshis in this [Amount].
487+
#[deprecated(since = "0.29.0", note = "use to_sat instead")]
487488
pub fn as_sat(self) -> u64 {
489+
self.to_sat()
490+
}
491+
492+
/// Gets the number of satoshis in this [`Amount`].
493+
pub fn to_sat(self) -> u64 {
488494
self.0
489495
}
490496

@@ -543,9 +549,22 @@ impl Amount {
543549
/// Express this [Amount] as a floating-point value in Bitcoin.
544550
///
545551
/// Equivalent to `to_float_in(Denomination::Bitcoin)`.
552+
#[deprecated(since = "0.29.0", note = "use to_btc instead")]
553+
pub fn as_btc(self) -> f64 {
554+
self.to_btc()
555+
}
556+
557+
/// Express this [`Amount`] as a floating-point value in Bitcoin.
546558
///
547559
/// Please be aware of the risk of using floating-point numbers.
548-
pub fn as_btc(self) -> f64 {
560+
///
561+
/// # Examples
562+
/// ```
563+
/// # use bitcoin::{Amount, Denomination};
564+
/// let amount = Amount::from_sat(100_000);
565+
/// assert_eq!(amount.to_btc(), amount.to_float_in(Denomination::Bitcoin))
566+
/// ```
567+
pub fn to_btc(self) -> f64 {
549568
self.to_float_in(Denomination::Bitcoin)
550569
}
551570

@@ -566,7 +585,7 @@ impl Amount {
566585
/// Create an object that implements [`fmt::Display`] using specified denomination.
567586
pub fn display_in(self, denomination: Denomination) -> Display {
568587
Display {
569-
sats_abs: self.as_sat(),
588+
sats_abs: self.to_sat(),
570589
is_negative: false,
571590
style: DisplayStyle::FixedDenomination { denomination, show_denomination: false, },
572591
}
@@ -578,7 +597,7 @@ impl Amount {
578597
/// avoid confusion the denomination is always shown.
579598
pub fn display_dynamic(self) -> Display {
580599
Display {
581-
sats_abs: self.as_sat(),
600+
sats_abs: self.to_sat(),
582601
is_negative: false,
583602
style: DisplayStyle::DynamicDenomination,
584603
}
@@ -588,7 +607,7 @@ impl Amount {
588607
///
589608
/// Does not include the denomination.
590609
pub fn fmt_value_in(self, f: &mut dyn fmt::Write, denom: Denomination) -> fmt::Result {
591-
fmt_satoshi_in(self.as_sat(), false, f, denom, false, FormatOptions::default())
610+
fmt_satoshi_in(self.to_sat(), false, f, denom, false, FormatOptions::default())
592611
}
593612

594613
/// Get a string number of this [Amount] in the given denomination.
@@ -645,10 +664,10 @@ impl Amount {
645664

646665
/// Convert to a signed amount.
647666
pub fn to_signed(self) -> Result<SignedAmount, ParseAmountError> {
648-
if self.as_sat() > SignedAmount::max_value().as_sat() as u64 {
667+
if self.to_sat() > SignedAmount::max_value().to_sat() as u64 {
649668
Err(ParseAmountError::TooBig)
650669
} else {
651-
Ok(SignedAmount::from_sat(self.as_sat() as i64))
670+
Ok(SignedAmount::from_sat(self.to_sat() as i64))
652671
}
653672
}
654673
}
@@ -661,7 +680,7 @@ impl default::Default for Amount {
661680

662681
impl fmt::Debug for Amount {
663682
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
664-
write!(f, "Amount({:.8} BTC)", self.as_btc())
683+
write!(f, "Amount({:.8} BTC)", self.to_btc())
665684
}
666685
}
667686

@@ -799,7 +818,7 @@ impl fmt::Display for Display {
799818
let format_options = FormatOptions::from_formatter(f);
800819
match &self.style {
801820
DisplayStyle::FixedDenomination { show_denomination, denomination } => fmt_satoshi_in(self.sats_abs, self.is_negative, f, *denomination, *show_denomination, format_options),
802-
DisplayStyle::DynamicDenomination if self.sats_abs >= Amount::ONE_BTC.as_sat() => {
821+
DisplayStyle::DynamicDenomination if self.sats_abs >= Amount::ONE_BTC.to_sat() => {
803822
fmt_satoshi_in(self.sats_abs, self.is_negative, f, Denomination::Bitcoin, true, format_options)
804823
},
805824
DisplayStyle::DynamicDenomination => {
@@ -848,7 +867,13 @@ impl SignedAmount {
848867
}
849868

850869
/// Get the number of satoshis in this [SignedAmount].
870+
#[deprecated(since = "0.29.0", note = "use to_sat instead")]
851871
pub fn as_sat(self) -> i64 {
872+
self.to_sat()
873+
}
874+
875+
/// Gets the number of satoshis in this [`SignedAmount`].
876+
pub fn to_sat(self) -> i64 {
852877
self.0
853878
}
854879

@@ -909,7 +934,17 @@ impl SignedAmount {
909934
/// Equivalent to `to_float_in(Denomination::Bitcoin)`.
910935
///
911936
/// Please be aware of the risk of using floating-point numbers.
937+
#[deprecated(since = "0.29.0", note = "use to_btc instead")]
912938
pub fn as_btc(self) -> f64 {
939+
self.to_btc()
940+
}
941+
942+
/// Express this [`SignedAmount`] as a floating-point value in Bitcoin.
943+
///
944+
/// Equivalent to `to_float_in(Denomination::Bitcoin)`.
945+
///
946+
/// Please be aware of the risk of using floating-point numbers.
947+
pub fn to_btc(self) -> f64 {
913948
self.to_float_in(Denomination::Bitcoin)
914949
}
915950

@@ -931,7 +966,7 @@ impl SignedAmount {
931966
///
932967
/// This is the implementation of `unsigned_abs()` copied from `core` to support older MSRV.
933968
fn to_sat_abs(self) -> u64 {
934-
self.as_sat().wrapping_abs() as u64
969+
self.to_sat().wrapping_abs() as u64
935970
}
936971

937972
/// Create an object that implements [`fmt::Display`] using specified denomination.
@@ -1063,7 +1098,7 @@ impl SignedAmount {
10631098
if self.is_negative() {
10641099
Err(ParseAmountError::Negative)
10651100
} else {
1066-
Ok(Amount::from_sat(self.as_sat() as u64))
1101+
Ok(Amount::from_sat(self.to_sat() as u64))
10671102
}
10681103
}
10691104
}
@@ -1076,7 +1111,7 @@ impl default::Default for SignedAmount {
10761111

10771112
impl fmt::Debug for SignedAmount {
10781113
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1079-
write!(f, "SignedAmount({:.8} BTC)", self.as_btc())
1114+
write!(f, "SignedAmount({:.8} BTC)", self.to_btc())
10801115
}
10811116
}
10821117

@@ -1263,7 +1298,7 @@ pub mod serde {
12631298

12641299
impl SerdeAmount for Amount {
12651300
fn ser_sat<S: Serializer>(self, s: S) -> Result<S::Ok, S::Error> {
1266-
u64::serialize(&self.as_sat(), s)
1301+
u64::serialize(&self.to_sat(), s)
12671302
}
12681303
fn des_sat<'d, D: Deserializer<'d>>(d: D) -> Result<Self, D::Error> {
12691304
Ok(Amount::from_sat(u64::deserialize(d)?))
@@ -1282,16 +1317,16 @@ pub mod serde {
12821317
"u"
12831318
}
12841319
fn ser_sat_opt<S: Serializer>(self, s: S) -> Result<S::Ok, S::Error> {
1285-
s.serialize_some(&self.as_sat())
1320+
s.serialize_some(&self.to_sat())
12861321
}
12871322
fn ser_btc_opt<S: Serializer>(self, s: S) -> Result<S::Ok, S::Error> {
1288-
s.serialize_some(&self.as_btc())
1323+
s.serialize_some(&self.to_btc())
12891324
}
12901325
}
12911326

12921327
impl SerdeAmount for SignedAmount {
12931328
fn ser_sat<S: Serializer>(self, s: S) -> Result<S::Ok, S::Error> {
1294-
i64::serialize(&self.as_sat(), s)
1329+
i64::serialize(&self.to_sat(), s)
12951330
}
12961331
fn des_sat<'d, D: Deserializer<'d>>(d: D) -> Result<Self, D::Error> {
12971332
Ok(SignedAmount::from_sat(i64::deserialize(d)?))
@@ -1310,10 +1345,10 @@ pub mod serde {
13101345
"i"
13111346
}
13121347
fn ser_sat_opt<S: Serializer>(self, s: S) -> Result<S::Ok, S::Error> {
1313-
s.serialize_some(&self.as_sat())
1348+
s.serialize_some(&self.to_sat())
13141349
}
13151350
fn ser_btc_opt<S: Serializer>(self, s: S) -> Result<S::Ok, S::Error> {
1316-
s.serialize_some(&self.as_btc())
1351+
s.serialize_some(&self.to_btc())
13171352
}
13181353
}
13191354

0 commit comments

Comments
 (0)