From 917470b805cfc7d522a208abfbe7f55994fd6c9e Mon Sep 17 00:00:00 2001 From: Michal Kucharczyk <1728078+michalkucharczyk@users.noreply.github.com> Date: Tue, 18 Feb 2025 18:44:51 +0100 Subject: [PATCH] HashAndNumber: Ord, Eq, PartialOrd, PartialEq implemented (#7612) This PR adds implementation of `Ord, Eq, PartialOrd, PartialEq` traits for [`HashAndNumber` ](https://github.com/paritytech/polkadot-sdk/blob/6e645915639ee0bf682de06a0306a4baf712c1d2/substrate/primitives/blockchain/src/header_metadata.rs#L149-L154) struct. --------- Co-authored-by: cmd[bot] <41898282+github-actions[bot]@users.noreply.github.com> --- prdoc/pr_7612.prdoc | 8 +++++++ .../blockchain/src/header_metadata.rs | 23 +++++++++++++++++++ 2 files changed, 31 insertions(+) create mode 100644 prdoc/pr_7612.prdoc diff --git a/prdoc/pr_7612.prdoc b/prdoc/pr_7612.prdoc new file mode 100644 index 00000000000..c53dcb5a2a7 --- /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 b2910a32e99..c6782a721ec 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 -- GitLab