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
#[derive(Clone,Copy,Eq,PartialEq,Hash)]
pub struct UUID(u64,u64);
impl std::fmt::Debug for UUID {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let raw = self.format_raw();
f.write_str(unsafe{std::str::from_utf8_unchecked(&raw[..])})
}
}
impl std::fmt::Display for UUID {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let raw = self.format_raw();
f.write_str(unsafe{std::str::from_utf8_unchecked(&raw[..])})
}
}
fn stupid_hex(f: u64) -> u32 {
let t = f & 0x4040_4040_4040_4040;
let mut s = (f & 0xF0F0F0F0F0F0F0F) + ((t >> 3) | (t >> 6));
s = ((s << 4) | (s >> 8)) & 0x00FF00FF00FF00FF;
s |= s << 24;
return ((s & 0xFFFF0000FFFF0000) | (s >> 48)) as u32
}
impl UUID {
pub fn from_bytes(exa: &[u8]) -> Option<UUID> {
let bytes: &[u8;36] = exa.try_into().ok()?;
let mut p0 = (stupid_hex(u64::from_le_bytes(bytes[0..8].try_into().unwrap())) as u64) << 32;
let op = u32::from_le_bytes(bytes[9..13].try_into().unwrap()) as u64
| ((u32::from_le_bytes(bytes[14..18].try_into().unwrap()) as u64) << 32);
p0 |= stupid_hex(op) as u64;
let op = u32::from_le_bytes(bytes[19..23].try_into().unwrap()) as u64
| ((u32::from_le_bytes(bytes[24..28].try_into().unwrap()) as u64) << 32);
let mut p1 = (stupid_hex(op) as u64) << 32;
p1 |= stupid_hex(u64::from_le_bytes(bytes[28..36].try_into().unwrap())) as u64;
Some(UUID(p0,p1))
}
pub fn format_raw(&self) -> [u8;36] {
const ALPHA: [u8;16] = *b"0123456789abcdef";
let mut ai = 16;
let mut bi = 16;
let mut q = || { ai -= 1; ALPHA[((self.0>>(ai*4))&0b1111u64) as usize] };
let mut p = || { bi -= 1; ALPHA[((self.1>>(bi*4))&0b1111u64) as usize] };
[
q(), q(), q(), q(),
q(), q(), q(), q(), b'-',
q(), q(), q(), q(), b'-',
q(), q(), q(), q(), b'-',
p(), p(), p(), p(), b'-',
p(), p(), p(), p(),
p(), p(), p(), p(),
p(), p(), p(), p()
]
}
}
use serde::de::{Deserializer, Unexpected, Visitor};
use serde::{Deserialize, Serialize, Serializer};
struct UUIDVisitor {}
impl<'de> Visitor<'de> for UUIDVisitor {
type Value = UUID;
fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(
formatter,
"Invalid Word Length <=15 length"
)
}
fn visit_str<E>(self, s: &str) -> Result<Self::Value, E>
where
E: serde::de::Error,
{
if let Some(word) = UUID::from_bytes(s.as_bytes()) {
Ok(word)
} else {
Err(serde::de::Error::invalid_value(
Unexpected::Other("Invalid UUID"),
&self,
))
}
}
}
impl Serialize for UUID {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
let buf = self.format_raw();
serializer.serialize_str(unsafe {
std::str::from_utf8_unchecked(&buf[..])
})
}
}
impl<'de> Deserialize<'de> for UUID {
fn deserialize<D>(deserializer: D) -> Result<UUID, D::Error>
where
D: Deserializer<'de>,
{
deserializer.deserialize_any( UUIDVisitor{})
}
}