Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add PartialOrd and Ord for PeerId #1594

Merged
merged 2 commits into from
Jun 15, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion core/src/peer_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ use bs58;
use thiserror::Error;
use multihash::{self, Code, Sha2_256};
use rand::Rng;
use std::{convert::TryFrom, borrow::Borrow, fmt, hash, str::FromStr};
use std::{convert::TryFrom, borrow::Borrow, fmt, hash, str::FromStr, cmp};

/// Public keys with byte-lengths smaller than `MAX_INLINE_KEY_LENGTH` will be
/// automatically used as the peer id using an identity multihash.
Expand Down Expand Up @@ -57,6 +57,21 @@ impl fmt::Display for PeerId {
}
}

impl cmp::PartialOrd for PeerId {
fn partial_cmp(&self, other: &Self) -> Option<cmp::Ordering> {
Some(Ord::cmp(self, other))
}
}

impl cmp::Ord for PeerId {
fn cmp(&self, other: &Self) -> cmp::Ordering {
// must use borrow, because as_bytes is not consistent with equality
let lhs: &[u8] = self.borrow();
let rhs: &[u8] = other.borrow();
lhs.cmp(rhs)
}
}

impl PeerId {
/// Builds a `PeerId` from a public key.
pub fn from_public_key(key: PublicKey) -> PeerId {
Expand Down