From 847fc19318cb7b9123733d405a375434c0ffb6d4 Mon Sep 17 00:00:00 2001
From: librelois <elois@ifee.fr>
Date: Sat, 12 May 2018 18:34:21 +0200
Subject: [PATCH] [enh] impl  Ord & PartialOrd for Blockstamp

---
 documents/lib.rs | 23 ++++++++++++++++++++---
 1 file changed, 20 insertions(+), 3 deletions(-)

diff --git a/documents/lib.rs b/documents/lib.rs
index f0104283..f80ced52 100644
--- a/documents/lib.rs
+++ b/documents/lib.rs
@@ -32,6 +32,7 @@ extern crate linked_hash_map;
 extern crate regex;
 extern crate serde;
 
+use std::cmp::Ordering;
 use std::fmt::{Debug, Display, Error, Formatter};
 
 use self::serde::ser::{Serialize, Serializer};
@@ -40,7 +41,7 @@ use duniter_crypto::keys::BaseConvertionError;
 pub mod blockchain;
 
 /// A block Id.
-#[derive(Debug, Copy, Clone, PartialEq, PartialOrd, Eq, Hash)]
+#[derive(Debug, Copy, Clone, Ord, PartialEq, PartialOrd, Eq, Hash)]
 pub struct BlockId(pub u32);
 
 impl Display for BlockId {
@@ -52,7 +53,7 @@ impl Display for BlockId {
 /// A hash wrapper.
 ///
 /// A hash is often provided as string composed of 64 hexadecimal character (0 to 9 then A to F).
-#[derive(Copy, Clone, PartialEq, Eq, Hash)]
+#[derive(Copy, Clone, Eq, Ord, PartialEq, PartialOrd, Hash)]
 pub struct Hash(pub [u8; 32]);
 
 impl Display for Hash {
@@ -121,7 +122,7 @@ impl Hash {
 }
 
 /// Wrapper of a block hash.
-#[derive(Copy, Clone, PartialEq, Eq, Hash)]
+#[derive(Copy, Clone, Eq, Ord, PartialEq, PartialOrd, Hash)]
 pub struct BlockHash(pub Hash);
 
 impl Display for BlockHash {
@@ -197,6 +198,22 @@ impl Serialize for Blockstamp {
     }
 }
 
+impl PartialOrd for Blockstamp {
+    fn partial_cmp(&self, other: &Blockstamp) -> Option<Ordering> {
+        Some(self.cmp(other))
+    }
+}
+
+impl Ord for Blockstamp {
+    fn cmp(&self, other: &Blockstamp) -> Ordering {
+        if self.id == other.id {
+            self.hash.cmp(&other.hash)
+        } else {
+            self.id.cmp(&other.id)
+        }
+    }
+}
+
 impl Blockstamp {
     /// Create a `BlockUId` from a text.
     pub fn from_string(src: &str) -> Result<Blockstamp, BlockUIdParseError> {
-- 
GitLab