Skip to content

Commit 5feff17

Browse files
committed
Add into_inner method to Amount.
Signed-off-by: Jean Pierre Dudey <jeandudey@hotmail.com>
1 parent 09f540c commit 5feff17

File tree

1 file changed

+17
-9
lines changed

1 file changed

+17
-9
lines changed

src/lib.rs

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,23 @@ use std::ops::{Add, Div, Mul, Sub};
1616
use std::num::ParseFloatError;
1717
use std::str::FromStr;
1818

19+
/// The primitive type that holds the satoshis.
20+
type Inner = i64;
21+
1922
/// The amount of satoshis in a BTC.
2023
pub const SAT_PER_BTC: i64 = 100_000_000;
2124

2225
/// The amount of satoshis in a BTC.
2326
pub const SAT_PER_BTC_FP: f64 = 100_000_000.0;
2427

2528
/// Maximum value in an `Amount`.
26-
pub const MAX: Amount = Amount(i64::max_value());
29+
pub const MAX: Amount = Amount(Inner::max_value());
2730
/// Minimum value in an `Amount`.
28-
pub const MIN: Amount = Amount(i64::min_value());
31+
pub const MIN: Amount = Amount(Inner::min_value());
2932

3033
/// A bitcoin amount integer type.
3134
#[derive(Debug, Clone, Copy, Eq, PartialEq, Ord, PartialOrd)]
32-
pub struct Amount(i64);
35+
pub struct Amount(Inner);
3336

3437
impl Amount {
3538
/// Creates a new `Amount` from a satoshi amount.
@@ -45,7 +48,7 @@ impl Amount {
4548
}
4649

4750
/// Creates a new `Amount` from a satoshi amount.
48-
pub fn from_sat(sat: i64) -> Amount {
51+
pub fn from_sat(sat: Inner) -> Amount {
4952
Amount(sat)
5053
}
5154

@@ -72,6 +75,11 @@ impl Amount {
7275

7376
/// Minimum value that can fit in an `Amount`.
7477
pub fn min_value() -> Amount { MIN }
78+
79+
/// Converts this `Amount` to the inner satoshis.
80+
pub fn into_inner(self) -> Inner {
81+
self.0
82+
}
7583
}
7684

7785
impl Add for Amount {
@@ -112,7 +120,7 @@ impl<'de> serde::Deserialize<'de> for Amount {
112120
where
113121
D: serde::de::Deserializer<'de>
114122
{
115-
i64::deserialize(deserializer).map(Amount)
123+
Inner::deserialize(deserializer).map(Amount)
116124
}
117125
}
118126

@@ -122,7 +130,7 @@ impl serde::Serialize for Amount {
122130
where
123131
S: serde::ser::Serializer
124132
{
125-
i64::serialize(&self.0, serializer)
133+
Inner::serialize(&self.0, serializer)
126134
}
127135
}
128136

@@ -156,11 +164,11 @@ impl error::Error for ParseAmountError {
156164
}
157165
}
158166

159-
fn round_and_to_sat(v: f64) -> i64 {
167+
fn round_and_to_sat(v: f64) -> Inner {
160168
if v < 0.0 {
161-
((v * SAT_PER_BTC_FP) - 0.5) as i64
169+
((v * SAT_PER_BTC_FP) - 0.5) as Inner
162170
} else {
163-
((v * SAT_PER_BTC_FP) + 0.5) as i64
171+
((v * SAT_PER_BTC_FP) + 0.5) as Inner
164172
}
165173
}
166174

0 commit comments

Comments
 (0)