27 lines
541 B
Rust
27 lines
541 B
Rust
use std::fmt;
|
|
|
|
const MAX_PEER_ID_LENGTH: usize = 20;
|
|
|
|
#[derive(Debug)]
|
|
pub struct PeerId {
|
|
pub bytes: [u8; MAX_PEER_ID_LENGTH],
|
|
}
|
|
|
|
impl PeerId {
|
|
pub fn from_bytes(buf: &[u8]) -> Option<PeerId> {
|
|
Some(PeerId {
|
|
bytes: buf.try_into().ok()?,
|
|
})
|
|
}
|
|
|
|
pub fn to_string_lossy(&self) -> String {
|
|
String::from_utf8_lossy(&self.bytes).into()
|
|
}
|
|
}
|
|
|
|
impl fmt::Display for PeerId {
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
write!(f, "{}", self.to_string_lossy())
|
|
}
|
|
}
|