From fdde0e691ecfb415021ee3a79ba5c7e3153a8be8 Mon Sep 17 00:00:00 2001 From: Thomas Eizinger Date: Fri, 26 Feb 2021 14:15:46 +1100 Subject: [PATCH] Make constructor functions on `FeeRate` const 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 | 4 ++++ src/types.rs | 15 +++++++++++++-- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 98cf3dba..96d32ff9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/src/types.rs b/src/types.rs index 9873150a..3ebce117 100644 --- a/src/types.rs +++ b/src/types.rs @@ -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, } + +#[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(); + } +}