diff --git a/prdoc/pr_7612.prdoc b/prdoc/pr_7612.prdoc new file mode 100644 index 0000000000000000000000000000000000000000..c53dcb5a2a750c0feee480bdba69afbdae726b03 --- /dev/null +++ b/prdoc/pr_7612.prdoc @@ -0,0 +1,8 @@ +title: 'HashAndNumber: Ord, Eq, PartialOrd, PartialEq implemented' +doc: +- audience: Node Dev + description: This PR adds implementation of `Ord, Eq, PartialOrd, PartialEq` traits + for `HashAndNumber` struct. +crates: +- name: sp-blockchain + bump: minor diff --git a/substrate/primitives/blockchain/src/header_metadata.rs b/substrate/primitives/blockchain/src/header_metadata.rs index b2910a32e99546f9cb74803f124ba963ed21700f..c6782a721ec1ad0a508398e68307c1f41619bb5a 100644 --- a/substrate/primitives/blockchain/src/header_metadata.rs +++ b/substrate/primitives/blockchain/src/header_metadata.rs @@ -153,6 +153,29 @@ pub struct HashAndNumber<Block: BlockT> { pub hash: Block::Hash, } +impl<Block: BlockT> Eq for HashAndNumber<Block> {} + +impl<Block: BlockT> PartialEq for HashAndNumber<Block> { + fn eq(&self, other: &Self) -> bool { + self.number.eq(&other.number) && self.hash.eq(&other.hash) + } +} + +impl<Block: BlockT> Ord for HashAndNumber<Block> { + fn cmp(&self, other: &Self) -> std::cmp::Ordering { + match self.number.cmp(&other.number) { + std::cmp::Ordering::Equal => self.hash.cmp(&other.hash), + result => result, + } + } +} + +impl<Block: BlockT> PartialOrd for HashAndNumber<Block> { + fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> { + Some(self.cmp(&other)) + } +} + /// A tree-route from one block to another in the chain. /// /// All blocks prior to the pivot in the vector is the reverse-order unique ancestry