@@ -484,7 +484,13 @@ impl Amount {
484
484
}
485
485
486
486
/// Get the number of satoshis in this [Amount].
487
+ #[ deprecated( since = "0.29.0" , note = "use to_sat instead" ) ]
487
488
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 {
488
494
self . 0
489
495
}
490
496
@@ -543,9 +549,22 @@ impl Amount {
543
549
/// Express this [Amount] as a floating-point value in Bitcoin.
544
550
///
545
551
/// 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.
546
558
///
547
559
/// 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 {
549
568
self . to_float_in ( Denomination :: Bitcoin )
550
569
}
551
570
@@ -566,7 +585,7 @@ impl Amount {
566
585
/// Create an object that implements [`fmt::Display`] using specified denomination.
567
586
pub fn display_in ( self , denomination : Denomination ) -> Display {
568
587
Display {
569
- sats_abs : self . as_sat ( ) ,
588
+ sats_abs : self . to_sat ( ) ,
570
589
is_negative : false ,
571
590
style : DisplayStyle :: FixedDenomination { denomination, show_denomination : false , } ,
572
591
}
@@ -578,7 +597,7 @@ impl Amount {
578
597
/// avoid confusion the denomination is always shown.
579
598
pub fn display_dynamic ( self ) -> Display {
580
599
Display {
581
- sats_abs : self . as_sat ( ) ,
600
+ sats_abs : self . to_sat ( ) ,
582
601
is_negative : false ,
583
602
style : DisplayStyle :: DynamicDenomination ,
584
603
}
@@ -588,7 +607,7 @@ impl Amount {
588
607
///
589
608
/// Does not include the denomination.
590
609
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 ( ) )
592
611
}
593
612
594
613
/// Get a string number of this [Amount] in the given denomination.
@@ -645,10 +664,10 @@ impl Amount {
645
664
646
665
/// Convert to a signed amount.
647
666
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 {
649
668
Err ( ParseAmountError :: TooBig )
650
669
} else {
651
- Ok ( SignedAmount :: from_sat ( self . as_sat ( ) as i64 ) )
670
+ Ok ( SignedAmount :: from_sat ( self . to_sat ( ) as i64 ) )
652
671
}
653
672
}
654
673
}
@@ -661,7 +680,7 @@ impl default::Default for Amount {
661
680
662
681
impl fmt:: Debug for Amount {
663
682
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 ( ) )
665
684
}
666
685
}
667
686
@@ -799,7 +818,7 @@ impl fmt::Display for Display {
799
818
let format_options = FormatOptions :: from_formatter ( f) ;
800
819
match & self . style {
801
820
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 ( ) => {
803
822
fmt_satoshi_in ( self . sats_abs , self . is_negative , f, Denomination :: Bitcoin , true , format_options)
804
823
} ,
805
824
DisplayStyle :: DynamicDenomination => {
@@ -848,7 +867,13 @@ impl SignedAmount {
848
867
}
849
868
850
869
/// Get the number of satoshis in this [SignedAmount].
870
+ #[ deprecated( since = "0.29.0" , note = "use to_sat instead" ) ]
851
871
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 {
852
877
self . 0
853
878
}
854
879
@@ -909,7 +934,17 @@ impl SignedAmount {
909
934
/// Equivalent to `to_float_in(Denomination::Bitcoin)`.
910
935
///
911
936
/// Please be aware of the risk of using floating-point numbers.
937
+ #[ deprecated( since = "0.29.0" , note = "use to_btc instead" ) ]
912
938
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 {
913
948
self . to_float_in ( Denomination :: Bitcoin )
914
949
}
915
950
@@ -931,7 +966,7 @@ impl SignedAmount {
931
966
///
932
967
/// This is the implementation of `unsigned_abs()` copied from `core` to support older MSRV.
933
968
fn to_sat_abs ( self ) -> u64 {
934
- self . as_sat ( ) . wrapping_abs ( ) as u64
969
+ self . to_sat ( ) . wrapping_abs ( ) as u64
935
970
}
936
971
937
972
/// Create an object that implements [`fmt::Display`] using specified denomination.
@@ -1063,7 +1098,7 @@ impl SignedAmount {
1063
1098
if self . is_negative ( ) {
1064
1099
Err ( ParseAmountError :: Negative )
1065
1100
} else {
1066
- Ok ( Amount :: from_sat ( self . as_sat ( ) as u64 ) )
1101
+ Ok ( Amount :: from_sat ( self . to_sat ( ) as u64 ) )
1067
1102
}
1068
1103
}
1069
1104
}
@@ -1076,7 +1111,7 @@ impl default::Default for SignedAmount {
1076
1111
1077
1112
impl fmt:: Debug for SignedAmount {
1078
1113
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 ( ) )
1080
1115
}
1081
1116
}
1082
1117
@@ -1263,7 +1298,7 @@ pub mod serde {
1263
1298
1264
1299
impl SerdeAmount for Amount {
1265
1300
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)
1267
1302
}
1268
1303
fn des_sat < ' d , D : Deserializer < ' d > > ( d : D ) -> Result < Self , D :: Error > {
1269
1304
Ok ( Amount :: from_sat ( u64:: deserialize ( d) ?) )
@@ -1282,16 +1317,16 @@ pub mod serde {
1282
1317
"u"
1283
1318
}
1284
1319
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 ( ) )
1286
1321
}
1287
1322
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 ( ) )
1289
1324
}
1290
1325
}
1291
1326
1292
1327
impl SerdeAmount for SignedAmount {
1293
1328
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)
1295
1330
}
1296
1331
fn des_sat < ' d , D : Deserializer < ' d > > ( d : D ) -> Result < Self , D :: Error > {
1297
1332
Ok ( SignedAmount :: from_sat ( i64:: deserialize ( d) ?) )
@@ -1310,10 +1345,10 @@ pub mod serde {
1310
1345
"i"
1311
1346
}
1312
1347
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 ( ) )
1314
1349
}
1315
1350
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 ( ) )
1317
1352
}
1318
1353
}
1319
1354
0 commit comments