Skip to content

Commit 7a69337

Browse files
authored
feat : add ordering for Multihash and MultihashRef (#50)
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.
1 parent 0ec803a commit 7a69337

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

src/lib.rs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ use unsigned_varint::{decode, encode};
2222

2323
pub use errors::{DecodeError, DecodeOwnedError, EncodeError};
2424
pub use hashes::Hash;
25-
use std::fmt;
25+
use std::{cmp, fmt};
2626
use storage::Storage;
2727

2828
// Helper macro for encoding input into output using sha1, sha2, tiny_keccak, or blake2
@@ -246,8 +246,20 @@ impl TryFrom<Vec<u8>> for Multihash {
246246
}
247247
}
248248

249+
impl PartialOrd for Multihash {
250+
fn partial_cmp(&self, other: &Self) -> Option<cmp::Ordering> {
251+
Some(self.cmp(other))
252+
}
253+
}
254+
255+
impl Ord for Multihash {
256+
fn cmp(&self, other: &Self) -> cmp::Ordering {
257+
self.as_ref().cmp(&other.as_ref())
258+
}
259+
}
260+
249261
/// Represents a valid multihash.
250-
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
262+
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
251263
pub struct MultihashRef<'a> {
252264
bytes: &'a [u8],
253265
}

0 commit comments

Comments
 (0)