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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
use std::{
convert::TryInto,
fmt,
};
mod parse;
pub mod query;
pub mod scoped;
#[doc(inline)]
pub use self::{
query::Query,
scoped::ScopedName,
};
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Name(str);
impl From<&Name> for Box<Name> {
#[inline]
fn from(name: &Name) -> Self {
name.into_boxed()
}
}
impl Clone for Box<Name> {
#[inline]
fn clone(&self) -> Self {
let str_box: &Box<str> = unsafe {
&*(self as *const Self as *const Box<str>)
};
let str_box = str_box.clone();
let raw = Box::into_raw(str_box) as *mut Name;
unsafe { Box::from_raw(raw) }
}
}
macro_rules! valid_name {
($name:expr) => {
{
union Convert<'a> {
s: &'a str,
n: &'a Name,
}
Convert { s: $name }.n
}
};
}
impl AsRef<str> for Name {
#[inline]
fn as_ref(&self) -> &str {
self.as_str()
}
}
impl AsRef<[u8]> for Name {
#[inline]
fn as_ref(&self) -> &[u8] {
self.0.as_bytes()
}
}
impl PartialEq<str> for Name {
#[inline]
fn eq(&self, s: &str) -> bool {
self.0 == *s
}
}
impl PartialEq<[u8]> for Name {
#[inline]
fn eq(&self, b: &[u8]) -> bool {
self.0.as_bytes() == b
}
}
impl PartialEq<Name> for str {
#[inline]
fn eq(&self, n: &Name) -> bool {
*self == n.0
}
}
impl PartialEq<Name> for [u8] {
#[inline]
fn eq(&self, n: &Name) -> bool {
self == n.0.as_bytes()
}
}
impl fmt::Display for Name {
#[inline]
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
self.0.fmt(f)
}
}
impl Name {
pub const CORE: &'static Self = unsafe { valid_name!("core") };
pub const OCEAN: &'static Self = unsafe { valid_name!("ocean") };
pub const SELF: &'static Self = unsafe { valid_name!("self") };
pub const RESERVED_SCOPES: &'static [&'static Self] = &[
Self::CORE,
Self::OCEAN,
Self::SELF,
];
#[inline]
pub fn new<'a, N>(name: N) -> Result<&'a Self, ValidateError>
where N: TryInto<&'a Self, Error = ValidateError>
{
name.try_into()
}
pub unsafe fn new_unchecked<N>(name: &N) -> &Self
where N: ?Sized + AsRef<[u8]>
{
&*(name.as_ref() as *const [u8] as *const Self)
}
#[inline]
pub fn is_valid<N: AsRef<[u8]>>(name: N) -> bool {
fn imp(bytes: &[u8]) -> bool {
match (bytes.first(), bytes.last()) {
(None, _) | (Some(b'-'), _) | (_, Some(b'-')) => false,
_ => bytes.iter().cloned().all(Name::is_valid_ascii),
}
}
imp(name.as_ref())
}
#[inline]
pub fn is_valid_ascii(byte: u8) -> bool {
match byte {
b'0'..=b'9' |
b'a'..=b'z' |
b'-' => true,
_ => false,
}
}
#[inline]
pub fn is_valid_char(ch: char) -> bool {
ch.is_ascii() && Self::is_valid_ascii(ch as u8)
}
#[inline]
pub const fn as_str(&self) -> &str {
&self.0
}
#[inline]
pub fn into_boxed(&self) -> Box<Self> {
let raw = Box::<str>::into_raw(self.0.into()) as *mut Name;
unsafe { Box::from_raw(raw) }
}
#[inline]
pub fn is_reserved_scope(&self) -> bool {
Self::RESERVED_SCOPES.contains(&self)
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct ValidateError(pub(super) ());
impl fmt::Display for ValidateError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "failed to validate drop name")
}
}
#[cfg(test)]
mod tests {
use super::*;
fn outer() -> Vec<char> {
let mut outer = Vec::new();
outer.extend((b'a'..=b'z').map(|byte| byte as char));
outer.extend((b'0'..=b'9').map(|byte| byte as char));
outer
}
#[test]
fn valid_names() {
let outer = outer();
let mut inner = outer.clone();
inner.push('-');
for &c1 in &outer {
let mut name_buf = [0; 4];
let name = c1.encode_utf8(&mut name_buf);
assert!(
Name::is_valid(&name),
"{:?} found to be invalid",
name
);
for &c2 in &inner {
for &c3 in &outer {
let name: String = [c1, c2, c3].iter().collect();
assert!(
Name::is_valid(&name),
"{:?} found to be invalid",
name
);
}
}
}
}
#[test]
fn invalid_names() {
assert!(!Name::is_valid(""));
assert!(!Name::is_valid("-"));
assert!(!Name::is_valid("--"));
assert!(!Name::is_valid("---"));
for &ch in &outer() {
let names: &[&[char]] = &[
&[ch, '-'],
&['-', ch],
&['-', ch, '-'],
];
for name in names {
let name: String = name.iter().cloned().collect();
assert!(
!Name::is_valid(&name),
"{:?} found to to be valid",
name
);
}
}
}
}