Explicitly define Ord for NodeInfo

This commit is contained in:
Shing Him Ng 2024-11-27 07:06:32 -06:00
parent dde5f47ce4
commit 428e9787d1
1 changed files with 20 additions and 2 deletions

View File

@ -7,7 +7,7 @@
pub mod merkle_branch; pub mod merkle_branch;
pub mod serialized_signature; pub mod serialized_signature;
use core::cmp::Reverse; use core::cmp::{Ordering, Reverse};
use core::fmt; use core::fmt;
use core::iter::FusedIterator; use core::iter::FusedIterator;
@ -789,7 +789,7 @@ impl DoubleEndedIterator for LeafNodes<'_> {
/// ///
/// You can use [`TaprootSpendInfo::from_node_info`] to a get a [`TaprootSpendInfo`] from the Merkle /// You can use [`TaprootSpendInfo::from_node_info`] to a get a [`TaprootSpendInfo`] from the Merkle
/// root [`NodeInfo`]. /// root [`NodeInfo`].
#[derive(Debug, Clone, PartialOrd, Ord)] #[derive(Debug, Clone)]
pub struct NodeInfo { pub struct NodeInfo {
/// Merkle hash for this node. /// Merkle hash for this node.
pub(crate) hash: TapNodeHash, pub(crate) hash: TapNodeHash,
@ -799,6 +799,24 @@ pub struct NodeInfo {
pub(crate) has_hidden_nodes: bool, pub(crate) has_hidden_nodes: bool,
} }
/// Explicitly implement Ord so future changes to NodeInfo (e.g. adding a new field) won't result in
/// potentially changing addresses out from under users
impl Ord for NodeInfo {
fn cmp(&self, other: &Self) -> Ordering {
match self.hash.cmp(&other.hash) {
Ordering::Equal => match self.leaves.cmp(&other.leaves) {
Ordering::Equal => self.has_hidden_nodes.cmp(&other.has_hidden_nodes),
other => other,
},
other => other,
}
}
}
impl PartialOrd for NodeInfo {
fn partial_cmp(&self, other: &NodeInfo) -> Option<Ordering> { Some(self.cmp(other)) }
}
impl PartialEq for NodeInfo { impl PartialEq for NodeInfo {
fn eq(&self, other: &Self) -> bool { self.hash.eq(&other.hash) } fn eq(&self, other: &Self) -> bool { self.hash.eq(&other.hash) }
} }