Skip to content

Commit 279986f

Browse files
committed
Use Arc<Vec<u8>> to replace Bytes in PeerId
For avoid cargo clippy raise `mutable_key_type error when use PeerId as HashSet/HashMap 's key.
1 parent 505a17d commit 279986f

File tree

1 file changed

+9
-9
lines changed

1 file changed

+9
-9
lines changed

core/src/peer_id.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@
1919
// DEALINGS IN THE SOFTWARE.
2020

2121
use crate::PublicKey;
22-
use bytes::Bytes;
2322
use thiserror::Error;
2423
use multihash::{Code, Multihash, MultihashDigest};
2524
use rand::Rng;
2625
use std::{convert::TryFrom, borrow::Borrow, fmt, hash, str::FromStr, cmp};
26+
use std::sync::Arc;
2727

2828
/// Public keys with byte-lengths smaller than `MAX_INLINE_KEY_LENGTH` will be
2929
/// automatically used as the peer id using an identity multihash.
@@ -35,7 +35,7 @@ const MAX_INLINE_KEY_LENGTH: usize = 42;
3535
// TODO: maybe keep things in decoded version?
3636
#[derive(Clone, Eq)]
3737
pub struct PeerId {
38-
multihash: Bytes,
38+
multihash: Arc<Vec<u8>>,
3939
}
4040

4141
impl fmt::Debug for PeerId {
@@ -78,9 +78,9 @@ impl PeerId {
7878
Code::Sha2_256
7979
};
8080

81-
let multihash = hash_algorithm.digest(&key_enc).to_bytes().into();
81+
let multihash = hash_algorithm.digest(&key_enc).to_bytes();
8282

83-
PeerId { multihash }
83+
PeerId { multihash: Arc::new(multihash) }
8484
}
8585

8686
/// Checks whether `data` is a valid `PeerId`. If so, returns the `PeerId`. If not, returns
@@ -99,9 +99,9 @@ impl PeerId {
9999
/// peer ID, it is returned as an `Err`.
100100
pub fn from_multihash(multihash: Multihash) -> Result<PeerId, Multihash> {
101101
match Code::try_from(multihash.code()) {
102-
Ok(Code::Sha2_256) => Ok(PeerId { multihash: multihash.to_bytes().into() }),
102+
Ok(Code::Sha2_256) => Ok(PeerId { multihash: Arc::new(multihash.to_bytes()) }),
103103
Ok(Code::Identity) if multihash.digest().len() <= MAX_INLINE_KEY_LENGTH
104-
=> Ok(PeerId { multihash: multihash.to_bytes().into() }),
104+
=> Ok(PeerId { multihash: Arc::new(multihash.to_bytes()) }),
105105
_ => Err(multihash)
106106
}
107107
}
@@ -112,8 +112,8 @@ impl PeerId {
112112
pub fn random() -> PeerId {
113113
let peer_id = rand::thread_rng().gen::<[u8; 32]>();
114114
PeerId {
115-
multihash: Multihash::wrap(Code::Identity.into(), &peer_id)
116-
.expect("The digest size is never too large").to_bytes().into()
115+
multihash: Arc::new(Multihash::wrap(Code::Identity.into(), &peer_id)
116+
.expect("The digest size is never too large").to_bytes())
117117
}
118118
}
119119

@@ -123,7 +123,7 @@ impl PeerId {
123123
/// equality of peer IDs. That is, two peer IDs may be considered equal
124124
/// while having a different byte representation as per `into_bytes`.
125125
pub fn into_bytes(self) -> Vec<u8> {
126-
self.multihash.to_vec()
126+
self.multihash.as_ref().clone()
127127
}
128128

129129
/// Returns a raw bytes representation of this `PeerId`.

0 commit comments

Comments
 (0)