Skip to content

Commit

Permalink
Implement ArbitraryOrd for relative::LockTime
Browse files Browse the repository at this point in the history
TL;DR As we do for `absolute::LockTime` and for the same reasons;
implement `ArbitraryOrd` for `relative::LockTime`.

locktimes do not have a semantic ordering if they differ (blocks, time)
so we do not derive `Ord` however it is useful for downstream to be able
to order structs that contain lock times. This is exactly what the
`ArbitraryOrd` trait is for.

Fix: rust-bitcoin#2566
  • Loading branch information
tcharding committed Mar 13, 2024
1 parent 6f6cc00 commit 2f0859c
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions bitcoin/src/blockdata/locktime/relative.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
//! whether bit 22 of the `u32` consensus value is set.
//!

use core::cmp::Ordering;
use core::fmt;

#[cfg(all(test, mutate))]
Expand Down Expand Up @@ -188,6 +189,20 @@ impl fmt::Display for LockTime {
}
}

#[cfg(feature = "ordered")]
impl ordered::ArbitraryOrd for LockTime {
fn arbitrary_cmp(&self, other: &Self) -> Ordering {
use LockTime::*;

match (self, other) {
(Blocks(_), Time(_)) => Ordering::Less,
(Time(_), Blocks(_)) => Ordering::Greater,
(Blocks(this), Blocks(that)) => this.cmp(that),
(Time(this), Time(that)) => this.cmp(that),
}
}
}

/// A relative lock time lock-by-blockheight value.
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
Expand Down

0 comments on commit 2f0859c

Please sign in to comment.