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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
pub use minibytes::Bytes;
use serde::{Deserialize, Serialize};
use std::fmt;
use std::ops;
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Id(pub u64);
#[derive(Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Serialize, Deserialize)]
#[serde(transparent)]
pub struct VertexName(pub Bytes);
impl AsRef<[u8]> for VertexName {
fn as_ref(&self) -> &[u8] {
&self.0
}
}
impl VertexName {
pub fn to_hex(&self) -> String {
const HEX_CHARS: &[u8] = b"0123456789abcdef";
let mut v = Vec::with_capacity(self.0.len() * 2);
for &byte in self.as_ref() {
v.push(HEX_CHARS[(byte >> 4) as usize]);
v.push(HEX_CHARS[(byte & 0xf) as usize]);
}
unsafe { String::from_utf8_unchecked(v) }
}
pub fn copy_from(value: &[u8]) -> Self {
Self(value.to_vec().into())
}
}
impl<T> From<T> for VertexName
where
Bytes: From<T>,
{
fn from(value: T) -> Self {
Self(Bytes::from(value))
}
}
impl fmt::Debug for VertexName {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
if self.0.len() >= 2 {
write!(f, "{}", self.to_hex())
} else {
match std::str::from_utf8(self.as_ref()) {
Ok(s) => write!(f, "{}", s),
Err(_) => write!(f, "{}", self.to_hex()),
}
}
}
}
#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Group(pub(crate) usize);
impl Group {
pub const MASTER: Self = Self(0);
pub const NON_MASTER: Self = Self(1);
pub const ALL: [Self; 2] = [Self::MASTER, Self::NON_MASTER];
pub(crate) const COUNT: usize = Self::ALL.len();
pub(crate) const BITS: u32 = 8;
pub const fn min_id(self) -> Id {
Id((self.0 as u64) << (64 - Self::BITS))
}
pub const fn max_id(self) -> Id {
Id(self.min_id().0 + ((1u64 << (64 - Self::BITS)) - 1))
}
}
impl Id {
pub fn group(self) -> Group {
let group = (self.0 >> (64 - Group::BITS)) as usize;
debug_assert!(group < Group::COUNT);
Group(group)
}
pub fn to(self, other: Id) -> IdIter {
IdIter {
current: self,
end: other,
}
}
pub fn to_bytearray(self) -> [u8; 8] {
unsafe { std::mem::transmute(self.0.to_be()) }
}
pub(crate) fn to_prefixed_bytearray(self, prefix: u8) -> [u8; 9] {
let a = self.to_bytearray();
[prefix, a[0], a[1], a[2], a[3], a[4], a[5], a[6], a[7]]
}
pub const MAX: Self = Group::ALL[Group::COUNT - 1].max_id();
pub const MIN: Self = Group::ALL[0].min_id();
}
impl fmt::Display for Id {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let group = self.group();
if group == Group::NON_MASTER {
write!(f, "N")?;
}
write!(f, "{}", self.0 - group.min_id().0)
}
}
impl fmt::Debug for Id {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self)
}
}
impl fmt::Display for Group {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self.0)
}
}
impl ops::Add<u64> for Id {
type Output = Id;
fn add(self, other: u64) -> Self {
Self(self.0 + other)
}
}
impl ops::Sub<u64> for Id {
type Output = Id;
fn sub(self, other: u64) -> Self {
Self(self.0 - other)
}
}
pub struct IdIter {
current: Id,
end: Id,
}
impl Iterator for IdIter {
type Item = Id;
fn next(&mut self) -> Option<Id> {
if self.current > self.end {
None
} else {
let result = self.current;
self.current = self.current + 1;
Some(result)
}
}
}