From 694649543d0c376c0cb1d8d5ff2d34808cb6d0db Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=BCdiger=20Klaehn?= Date: Thu, 20 Feb 2020 09:21:07 +0100 Subject: [PATCH 1/3] Add ordering for Multihash and MultihashRef This is just lexicographical byte ordering, which should be very fast in case of multihashes since they usually differ within the first 8 byte word. Ordering is useful to have if you e.g. want to output a set of multihashes in a deterministic way for debugging/testing purposes. Also, many possible storage engines work using lexicographical byte ordering. --- src/lib.rs | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 9b2fc43c..e683ef6d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -22,7 +22,7 @@ use unsigned_varint::{decode, encode}; pub use errors::{DecodeError, DecodeOwnedError, EncodeError}; pub use hashes::Hash; -use std::fmt; +use std::{cmp, fmt}; use storage::Storage; // Helper macro for encoding input into output using sha1, sha2, tiny_keccak, or blake2 @@ -246,8 +246,14 @@ impl TryFrom> for Multihash { } } +impl PartialOrd for Multihash { + fn partial_cmp(&self, other: &Self) -> Option { + Some(self.as_ref().cmp(&other.as_ref())) + } +} + /// Represents a valid multihash. -#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)] +#[derive(Debug, Copy, Clone, PartialEq, Eq, Ord, Hash)] pub struct MultihashRef<'a> { bytes: &'a [u8], } @@ -325,6 +331,12 @@ impl<'a> PartialEq for MultihashRef<'a> { } } +impl<'a> PartialOrd for MultihashRef<'a> { + fn partial_cmp(&self, other: &Self) -> Option { + Some(self.as_bytes().cmp(other.as_bytes())) + } +} + #[cfg(any(target_pointer_width = "32", target_pointer_width = "64"))] fn as_u64(a: usize) -> u64 { a as u64 From 3016ac142676e9e0a0df1e405afebf1eeb7e4e7d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=BCdiger=20Klaehn?= Date: Fri, 21 Feb 2020 13:34:59 +0100 Subject: [PATCH 2/3] Derive the PartialOrd no need for an explicit instance --- src/lib.rs | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index e683ef6d..616e2ac5 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -253,7 +253,7 @@ impl PartialOrd for Multihash { } /// Represents a valid multihash. -#[derive(Debug, Copy, Clone, PartialEq, Eq, Ord, Hash)] +#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] pub struct MultihashRef<'a> { bytes: &'a [u8], } @@ -331,12 +331,6 @@ impl<'a> PartialEq for MultihashRef<'a> { } } -impl<'a> PartialOrd for MultihashRef<'a> { - fn partial_cmp(&self, other: &Self) -> Option { - Some(self.as_bytes().cmp(other.as_bytes())) - } -} - #[cfg(any(target_pointer_width = "32", target_pointer_width = "64"))] fn as_u64(a: usize) -> u64 { a as u64 From a61db7963b6603d2ee50ed699243a99a33c5036b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=BCdiger=20Klaehn?= Date: Fri, 21 Feb 2020 13:44:53 +0100 Subject: [PATCH 3/3] Actually add Ord for Multihash... --- src/lib.rs | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index 616e2ac5..2a6e7289 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -248,7 +248,13 @@ impl TryFrom> for Multihash { impl PartialOrd for Multihash { fn partial_cmp(&self, other: &Self) -> Option { - Some(self.as_ref().cmp(&other.as_ref())) + Some(self.cmp(other)) + } +} + +impl Ord for Multihash { + fn cmp(&self, other: &Self) -> cmp::Ordering { + self.as_ref().cmp(&other.as_ref()) } }