mirror of
https://github.com/fluencelabs/rust-libp2p
synced 2025-04-30 21:22:16 +00:00
Add PartialOrd and Ord for PeerId (#1594)
for using PeerId in order based collections. Ord uses borrow to be consistent with equality.
This commit is contained in:
parent
e8bf34fcf1
commit
926274e88c
@ -23,7 +23,7 @@ use bs58;
|
|||||||
use thiserror::Error;
|
use thiserror::Error;
|
||||||
use multihash::{self, Code, Sha2_256};
|
use multihash::{self, Code, Sha2_256};
|
||||||
use rand::Rng;
|
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
|
/// Public keys with byte-lengths smaller than `MAX_INLINE_KEY_LENGTH` will be
|
||||||
/// automatically used as the peer id using an identity multihash.
|
/// automatically used as the peer id using an identity multihash.
|
||||||
@ -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 {
|
impl PeerId {
|
||||||
/// Builds a `PeerId` from a public key.
|
/// Builds a `PeerId` from a public key.
|
||||||
pub fn from_public_key(key: PublicKey) -> PeerId {
|
pub fn from_public_key(key: PublicKey) -> PeerId {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user