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
use {
crate::{bytes::*, Error},
byteorder::{BigEndian, ByteOrder},
core::fmt,
};
pub use uuid::Uuid;
const BASE_UUID: [u8; 16] = [
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 00, 0x80, 0x00,
0x00, 0x80, 0x5F, 0x9B, 0x34, 0xFB,
];
#[derive(PartialEq, Eq, Copy, Clone)]
pub struct Uuid16(pub u16);
#[derive(PartialEq, Eq, Copy, Clone)]
pub struct Uuid32(pub u32);
impl From<Uuid16> for Uuid32 {
fn from(smol: Uuid16) -> Self {
Uuid32(smol.0.into())
}
}
impl Into<Uuid> for Uuid16 {
fn into(self) -> Uuid {
Uuid32::from(self).into()
}
}
impl Into<Uuid> for Uuid32 {
fn into(self) -> Uuid {
let mut buf = BASE_UUID;
BigEndian::write_u32(&mut buf, self.0);
Uuid::from_bytes(buf)
}
}
impl ToBytes for Uuid16 {
fn to_bytes(&self, buffer: &mut ByteWriter) -> Result<(), Error> {
buffer.write_slice(&self.0.to_le_bytes())
}
}
impl ToBytes for Uuid32 {
fn to_bytes(&self, buffer: &mut ByteWriter) -> Result<(), Error> {
buffer.write_slice(&self.0.to_le_bytes())
}
}
impl ToBytes for Uuid {
fn to_bytes(&self, buffer: &mut ByteWriter) -> Result<(), Error> {
buffer.write_slice(self.as_bytes())
}
}
impl FromBytes<'_> for Uuid16 {
fn from_bytes(bytes: &mut ByteReader) -> Result<Self, Error> {
let array = bytes.read_array()?;
Ok(Uuid16(u16::from_le_bytes(array)))
}
}
impl FromBytes<'_> for Uuid32 {
fn from_bytes(bytes: &mut ByteReader) -> Result<Self, Error> {
let array = bytes.read_array()?;
Ok(Uuid32(u32::from_le_bytes(array)))
}
}
impl FromBytes<'_> for Uuid {
fn from_bytes(bytes: &mut ByteReader) -> Result<Self, Error> {
let array = bytes.read_array()?;
Ok(Uuid::from_bytes(array))
}
}
impl fmt::Debug for Uuid16 {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "Uuid16({:04x})", self.0)
}
}
impl fmt::Debug for Uuid32 {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "Uuid32({:08x})", self.0)
}
}
pub enum UuidKind {
Uuid16,
Uuid32,
Uuid128,
}
pub trait IsUuid: for<'a> FromBytes<'a> + ToBytes + Copy {
const KIND: UuidKind;
}
impl IsUuid for Uuid16 {
const KIND: UuidKind = UuidKind::Uuid16;
}
impl IsUuid for Uuid32 {
const KIND: UuidKind = UuidKind::Uuid32;
}
impl IsUuid for Uuid {
const KIND: UuidKind = UuidKind::Uuid128;
}