]> Untitled Git - bdk/commitdiff
Make constructor functions on `FeeRate` const
authorThomas Eizinger <thomas@eizinger.io>
Fri, 26 Feb 2021 03:15:46 +0000 (14:15 +1100)
committerThomas Eizinger <thomas@eizinger.io>
Mon, 1 Mar 2021 00:04:39 +0000 (11:04 +1100)
This allows `FeeRate`s to be stored inside `const`s.

For example:

const MY_FEE_RATE: FeeRate = FeeRate::from_sat_per_vb(10.0);

Unfortunately, floating point maths inside const expressions is
still unstable, hence we cannot make `from_btc_per_kvb` const.

CHANGELOG.md
src/types.rs

index 98cf3dba2c09c42be31fb7531958e99b10495950..96d32ff98c34a31ff856ad529032ffae71080e15 100644 (file)
@@ -10,6 +10,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
 #### Changed
 - Updated `electrum-client` to version `0.7`
 
+### Wallet
+#### Changed
+- `FeeRate` constructors `from_sat_per_vb` and `default_min_relay_fee` are now `const` functions
+
 ## [v0.4.0] - [v0.3.0]
 
 ### Keys
index 9873150a84016db9c932a28def280f66abc61193..3ebce11770c020a9ff7a4f4b78a0a3943062d8ce 100644 (file)
@@ -69,12 +69,12 @@ impl FeeRate {
     }
 
     /// Create a new instance of [`FeeRate`] given a float fee rate in satoshi/vbyte
-    pub fn from_sat_per_vb(sat_per_vb: f32) -> Self {
+    pub const fn from_sat_per_vb(sat_per_vb: f32) -> Self {
         FeeRate(sat_per_vb)
     }
 
     /// Create a new [`FeeRate`] with the default min relay fee value
-    pub fn default_min_relay_fee() -> Self {
+    pub const fn default_min_relay_fee() -> Self {
         FeeRate(1.0)
     }
 
@@ -179,3 +179,14 @@ pub struct TransactionDetails {
     /// Confirmed in block height, `None` means unconfirmed
     pub height: Option<u32>,
 }
+
+#[cfg(test)]
+mod tests {
+    use super::*;
+
+    #[test]
+    fn can_store_feerate_in_const() {
+        const _MY_RATE: FeeRate = FeeRate::from_sat_per_vb(10.0);
+        const _MIN_RELAY: FeeRate = FeeRate::default_min_relay_fee();
+    }
+}