Merge rust-bitcoin/rust-bitcoin#3726: units: Improve `Default`

a4a0f2921c units: Add regression tests for Default impls (Tobin C. Harding)
f5c2248a31 units: Derive Default for BlockInterval (Tobin C. Harding)

Pull request description:

  Done while looking into [C-TOR](https://rust-lang.github.io/api-guidelines/predictability.html#c-ctor)

  - Derive `Default` for `BlockInterval`
  - Add regression test for all types that implement `Default`

ACKs for top commit:
  apoelstra:
    ACK a4a0f2921cd2425fa46a8ade42f888d5268e6709; successfully ran local tests

Tree-SHA512: ce39c2bcb37fc0fa70bd2553dd7843d9ac8f9528631706056ba89fda9623defadf32974091832b7a087121290b3f883bc8a8dcca070f1d90670eeee6b541de01
This commit is contained in:
merge-script 2024-12-12 18:26:46 +00:00
commit bfd35d49e3
No known key found for this signature in database
GPG Key ID: C588D63CE41B97C1
2 changed files with 18 additions and 4 deletions

View File

@ -93,7 +93,7 @@ impl TryFrom<BlockHeight> 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);

View File

@ -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::<Types>();
assert_sync::<Errors>();
}
#[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);
}