1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
use std::fmt::{self, Debug};
use std::ops::Index;
use base::crypto::Crypto;
#[repr(C)]
#[derive(PartialEq, PartialOrd, Ord, Eq, Hash, Default, Clone, Deserialize,
Serialize)]
pub struct Eid([u8; Eid::EID_SIZE]);
impl Eid {
pub(crate) const EID_SIZE: usize = 32;
#[inline]
pub(crate) fn new_empty() -> Self {
Eid::default()
}
pub(crate) fn new() -> Self {
let mut eid = Eid::new_empty();
Crypto::random_buf(&mut eid.0);
eid
}
#[inline]
pub(crate) fn is_empty(&self) -> bool {
*self == Eid::default()
}
pub(crate) fn from_slice(buf: &[u8]) -> Self {
assert_eq!(buf.len(), Eid::EID_SIZE);
let mut ret = Eid::new_empty();
ret.0.copy_from_slice(buf);
ret
}
}
impl AsRef<[u8]> for Eid {
#[inline]
fn as_ref(&self) -> &[u8] {
&self.0
}
}
impl Index<usize> for Eid {
type Output = u8;
#[inline]
fn index(&self, idx: usize) -> &u8 {
&self.0[idx]
}
}
impl Debug for Eid {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "Eid({})", &self.to_string()[..6])
}
}
impl ToString for Eid {
fn to_string(&self) -> String {
let strs: Vec<String> =
self.0.iter().map(|b| format!("{:02x}", b)).collect();
strs.join("")
}
}
pub trait Id {
fn id(&self) -> &Eid;
fn id_mut(&mut self) -> &mut Eid;
}