From f5c2248a31ef6c75319b665e43530cb2cca6c4c4 Mon Sep 17 00:00:00 2001 From: "Tobin C. Harding" Date: Wed, 11 Dec 2024 13:34:27 +1100 Subject: [PATCH 1/2] units: Derive Default for BlockInterval A block interval is a relative thing so it makes sense to default to zero. This is the same as how we derive `Debug` for `relative::Height` but not `absolute::Height`. --- units/src/block.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/units/src/block.rs b/units/src/block.rs index 6afa29453..bdbf886b0 100644 --- a/units/src/block.rs +++ b/units/src/block.rs @@ -93,7 +93,7 @@ impl TryFrom for absolute::Height { /// /// This type is not meant for constructing relative height based timelocks, this is a general /// purpose block interval abstraction. For locktimes please see [`locktime::relative::Height`]. -#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] +#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] // Public to try and make it really clear that there are no invariants. pub struct BlockInterval(pub u32); From a4a0f2921cd2425fa46a8ade42f888d5268e6709 Mon Sep 17 00:00:00 2001 From: "Tobin C. Harding" Date: Wed, 11 Dec 2024 13:35:34 +1100 Subject: [PATCH 2/2] units: Add regression tests for Default impls Add a regression test to the `api` tests to check the value returned by `Default` for all types that implement it. --- units/tests/api.rs | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/units/tests/api.rs b/units/tests/api.rs index eb6a9530e..20746a3a8 100644 --- a/units/tests/api.rs +++ b/units/tests/api.rs @@ -98,12 +98,13 @@ struct CommonTraits { } /// A struct that includes all types that implement `Default`. -#[derive(Default)] // C-COMMON-TRAITS: `Default` +#[derive(Debug, Default, PartialEq, Eq)] // C-COMMON-TRAITS: `Default` struct Default { a: Amount, b: SignedAmount, - c: relative::Height, - d: relative::Time, + c: BlockInterval, + d: relative::Height, + e: relative::Time, } /// A struct that includes all public error types. @@ -249,3 +250,16 @@ fn test_sync() { assert_sync::(); assert_sync::(); } + +#[test] +fn regression_default() { + let got: Default = Default::default(); + let want = Default { + a: Amount::ZERO, + b: SignedAmount::ZERO, + c: BlockInterval::ZERO, + d: relative::Height::ZERO, + e: relative::Time::ZERO, + }; + assert_eq!(got, want); +}