1use core::{convert::TryInto as _, str::FromStr};
15
16use crate::{
17 std::{borrow::Borrow, fmt, str},
18 Error, Uuid, Variant,
19};
20
21#[cfg(feature = "std")]
22use crate::std::string::{String, ToString};
23
24impl std::fmt::Debug for Uuid {
25 #[inline]
26 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
27 fmt::LowerHex::fmt(self, f)
28 }
29}
30
31impl fmt::Display for Uuid {
32 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
33 fmt::LowerHex::fmt(self, f)
34 }
35}
36
37#[cfg(feature = "std")]
38impl From<Uuid> for String {
39 fn from(uuid: Uuid) -> Self {
40 uuid.to_string()
41 }
42}
43
44impl fmt::Display for Variant {
45 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
46 match *self {
47 Variant::NCS => f.write_fmt(format_args!("NCS"))write!(f, "NCS"),
48 Variant::RFC4122 => f.write_fmt(format_args!("RFC4122"))write!(f, "RFC4122"),
49 Variant::Microsoft => f.write_fmt(format_args!("Microsoft"))write!(f, "Microsoft"),
50 Variant::Future => f.write_fmt(format_args!("Future"))write!(f, "Future"),
51 }
52 }
53}
54
55impl fmt::LowerHex for Uuid {
56 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
57 fmt::LowerHex::fmt(self.as_hyphenated(), f)
58 }
59}
60
61impl fmt::UpperHex for Uuid {
62 #[inline]
63 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
64 fmt::UpperHex::fmt(self.as_hyphenated(), f)
65 }
66}
67
68#[derive(
71 #[automatically_derived]
impl ::core::clone::Clone for Hyphenated {
#[inline]
fn clone(&self) -> Hyphenated {
let _: ::core::clone::AssertParamIsClone<Uuid>;
*self
}
}Clone,
72 #[automatically_derived]
impl ::core::marker::Copy for Hyphenated { }Copy,
73 #[automatically_derived]
impl ::core::fmt::Debug for Hyphenated {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
::core::fmt::Formatter::debug_tuple_field1_finish(f, "Hyphenated",
&&self.0)
}
}Debug,
74 #[automatically_derived]
impl ::core::default::Default for Hyphenated {
#[inline]
fn default() -> Hyphenated {
Hyphenated(::core::default::Default::default())
}
}Default,
75 #[automatically_derived]
impl ::core::cmp::Eq for Hyphenated {
#[inline]
#[doc(hidden)]
#[coverage(off)]
fn assert_receiver_is_total_eq(&self) -> () {
let _: ::core::cmp::AssertParamIsEq<Uuid>;
}
}Eq,
76 #[automatically_derived]
impl ::core::hash::Hash for Hyphenated {
#[inline]
fn hash<__H: ::core::hash::Hasher>(&self, state: &mut __H) -> () {
::core::hash::Hash::hash(&self.0, state)
}
}Hash,
77 #[automatically_derived]
impl ::core::cmp::Ord for Hyphenated {
#[inline]
fn cmp(&self, other: &Hyphenated) -> ::core::cmp::Ordering {
::core::cmp::Ord::cmp(&self.0, &other.0)
}
}Ord,
78 #[automatically_derived]
impl ::core::cmp::PartialEq for Hyphenated {
#[inline]
fn eq(&self, other: &Hyphenated) -> bool { self.0 == other.0 }
}PartialEq,
79 #[automatically_derived]
impl ::core::cmp::PartialOrd for Hyphenated {
#[inline]
fn partial_cmp(&self, other: &Hyphenated)
-> ::core::option::Option<::core::cmp::Ordering> {
::core::cmp::PartialOrd::partial_cmp(&self.0, &other.0)
}
}PartialOrd,
80)]
81#[cfg_attr(
82 all(uuid_unstable, feature = "zerocopy"),
83 derive(
84 zerocopy::IntoBytes,
85 zerocopy::FromBytes,
86 zerocopy::KnownLayout,
87 zerocopy::Immutable,
88 zerocopy::Unaligned
89 )
90)]
91#[repr(transparent)]
92pub struct Hyphenated(Uuid);
93
94#[derive(
97 #[automatically_derived]
impl ::core::clone::Clone for Simple {
#[inline]
fn clone(&self) -> Simple {
let _: ::core::clone::AssertParamIsClone<Uuid>;
*self
}
}Clone,
98 #[automatically_derived]
impl ::core::marker::Copy for Simple { }Copy,
99 #[automatically_derived]
impl ::core::fmt::Debug for Simple {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
::core::fmt::Formatter::debug_tuple_field1_finish(f, "Simple",
&&self.0)
}
}Debug,
100 #[automatically_derived]
impl ::core::default::Default for Simple {
#[inline]
fn default() -> Simple { Simple(::core::default::Default::default()) }
}Default,
101 #[automatically_derived]
impl ::core::cmp::Eq for Simple {
#[inline]
#[doc(hidden)]
#[coverage(off)]
fn assert_receiver_is_total_eq(&self) -> () {
let _: ::core::cmp::AssertParamIsEq<Uuid>;
}
}Eq,
102 #[automatically_derived]
impl ::core::hash::Hash for Simple {
#[inline]
fn hash<__H: ::core::hash::Hasher>(&self, state: &mut __H) -> () {
::core::hash::Hash::hash(&self.0, state)
}
}Hash,
103 #[automatically_derived]
impl ::core::cmp::Ord for Simple {
#[inline]
fn cmp(&self, other: &Simple) -> ::core::cmp::Ordering {
::core::cmp::Ord::cmp(&self.0, &other.0)
}
}Ord,
104 #[automatically_derived]
impl ::core::cmp::PartialEq for Simple {
#[inline]
fn eq(&self, other: &Simple) -> bool { self.0 == other.0 }
}PartialEq,
105 #[automatically_derived]
impl ::core::cmp::PartialOrd for Simple {
#[inline]
fn partial_cmp(&self, other: &Simple)
-> ::core::option::Option<::core::cmp::Ordering> {
::core::cmp::PartialOrd::partial_cmp(&self.0, &other.0)
}
}PartialOrd,
106)]
107#[cfg_attr(
108 all(uuid_unstable, feature = "zerocopy"),
109 derive(
110 zerocopy::IntoBytes,
111 zerocopy::FromBytes,
112 zerocopy::KnownLayout,
113 zerocopy::Immutable,
114 zerocopy::Unaligned
115 )
116)]
117#[repr(transparent)]
118pub struct Simple(Uuid);
119
120#[derive(
123 #[automatically_derived]
impl ::core::clone::Clone for Urn {
#[inline]
fn clone(&self) -> Urn {
let _: ::core::clone::AssertParamIsClone<Uuid>;
*self
}
}Clone,
124 #[automatically_derived]
impl ::core::marker::Copy for Urn { }Copy,
125 #[automatically_derived]
impl ::core::fmt::Debug for Urn {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
::core::fmt::Formatter::debug_tuple_field1_finish(f, "Urn", &&self.0)
}
}Debug,
126 #[automatically_derived]
impl ::core::default::Default for Urn {
#[inline]
fn default() -> Urn { Urn(::core::default::Default::default()) }
}Default,
127 #[automatically_derived]
impl ::core::cmp::Eq for Urn {
#[inline]
#[doc(hidden)]
#[coverage(off)]
fn assert_receiver_is_total_eq(&self) -> () {
let _: ::core::cmp::AssertParamIsEq<Uuid>;
}
}Eq,
128 #[automatically_derived]
impl ::core::hash::Hash for Urn {
#[inline]
fn hash<__H: ::core::hash::Hasher>(&self, state: &mut __H) -> () {
::core::hash::Hash::hash(&self.0, state)
}
}Hash,
129 #[automatically_derived]
impl ::core::cmp::Ord for Urn {
#[inline]
fn cmp(&self, other: &Urn) -> ::core::cmp::Ordering {
::core::cmp::Ord::cmp(&self.0, &other.0)
}
}Ord,
130 #[automatically_derived]
impl ::core::cmp::PartialEq for Urn {
#[inline]
fn eq(&self, other: &Urn) -> bool { self.0 == other.0 }
}PartialEq,
131 #[automatically_derived]
impl ::core::cmp::PartialOrd for Urn {
#[inline]
fn partial_cmp(&self, other: &Urn)
-> ::core::option::Option<::core::cmp::Ordering> {
::core::cmp::PartialOrd::partial_cmp(&self.0, &other.0)
}
}PartialOrd,
132)]
133#[cfg_attr(
134 all(uuid_unstable, feature = "zerocopy"),
135 derive(
136 zerocopy::IntoBytes,
137 zerocopy::FromBytes,
138 zerocopy::KnownLayout,
139 zerocopy::Immutable,
140 zerocopy::Unaligned
141 )
142)]
143#[repr(transparent)]
144pub struct Urn(Uuid);
145
146#[derive(
149 #[automatically_derived]
impl ::core::clone::Clone for Braced {
#[inline]
fn clone(&self) -> Braced {
let _: ::core::clone::AssertParamIsClone<Uuid>;
*self
}
}Clone,
150 #[automatically_derived]
impl ::core::marker::Copy for Braced { }Copy,
151 #[automatically_derived]
impl ::core::fmt::Debug for Braced {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
::core::fmt::Formatter::debug_tuple_field1_finish(f, "Braced",
&&self.0)
}
}Debug,
152 #[automatically_derived]
impl ::core::default::Default for Braced {
#[inline]
fn default() -> Braced { Braced(::core::default::Default::default()) }
}Default,
153 #[automatically_derived]
impl ::core::cmp::Eq for Braced {
#[inline]
#[doc(hidden)]
#[coverage(off)]
fn assert_receiver_is_total_eq(&self) -> () {
let _: ::core::cmp::AssertParamIsEq<Uuid>;
}
}Eq,
154 #[automatically_derived]
impl ::core::hash::Hash for Braced {
#[inline]
fn hash<__H: ::core::hash::Hasher>(&self, state: &mut __H) -> () {
::core::hash::Hash::hash(&self.0, state)
}
}Hash,
155 #[automatically_derived]
impl ::core::cmp::Ord for Braced {
#[inline]
fn cmp(&self, other: &Braced) -> ::core::cmp::Ordering {
::core::cmp::Ord::cmp(&self.0, &other.0)
}
}Ord,
156 #[automatically_derived]
impl ::core::cmp::PartialEq for Braced {
#[inline]
fn eq(&self, other: &Braced) -> bool { self.0 == other.0 }
}PartialEq,
157 #[automatically_derived]
impl ::core::cmp::PartialOrd for Braced {
#[inline]
fn partial_cmp(&self, other: &Braced)
-> ::core::option::Option<::core::cmp::Ordering> {
::core::cmp::PartialOrd::partial_cmp(&self.0, &other.0)
}
}PartialOrd,
158)]
159#[cfg_attr(
160 all(uuid_unstable, feature = "zerocopy"),
161 derive(
162 zerocopy::IntoBytes,
163 zerocopy::FromBytes,
164 zerocopy::KnownLayout,
165 zerocopy::Immutable,
166 zerocopy::Unaligned
167 )
168)]
169#[repr(transparent)]
170pub struct Braced(Uuid);
171
172impl Uuid {
173 #[inline]
175 pub const fn hyphenated(self) -> Hyphenated {
176 Hyphenated(self)
177 }
178
179 #[inline]
181 pub fn as_hyphenated(&self) -> &Hyphenated {
182 unsafe { core::mem::transmute::<&_, &_>(self) }unsafe_transmute_ref!(self)
183 }
184
185 #[inline]
187 pub const fn simple(self) -> Simple {
188 Simple(self)
189 }
190
191 #[inline]
193 pub fn as_simple(&self) -> &Simple {
194 unsafe { core::mem::transmute::<&_, &_>(self) }unsafe_transmute_ref!(self)
195 }
196
197 #[inline]
199 pub const fn urn(self) -> Urn {
200 Urn(self)
201 }
202
203 #[inline]
205 pub fn as_urn(&self) -> &Urn {
206 unsafe { core::mem::transmute::<&_, &_>(self) }unsafe_transmute_ref!(self)
207 }
208
209 #[inline]
211 pub const fn braced(self) -> Braced {
212 Braced(self)
213 }
214
215 #[inline]
217 pub fn as_braced(&self) -> &Braced {
218 unsafe { core::mem::transmute::<&_, &_>(self) }unsafe_transmute_ref!(self)
219 }
220}
221
222const UPPER: [u8; 16] = [
223 b'0', b'1', b'2', b'3', b'4', b'5', b'6', b'7', b'8', b'9', b'A', b'B', b'C', b'D', b'E', b'F',
224];
225const LOWER: [u8; 16] = [
226 b'0', b'1', b'2', b'3', b'4', b'5', b'6', b'7', b'8', b'9', b'a', b'b', b'c', b'd', b'e', b'f',
227];
228
229#[inline]
230const fn format_simple(src: &[u8; 16], upper: bool) -> [u8; 32] {
231 let lut = if upper { &UPPER } else { &LOWER };
232 let mut dst = [0; 32];
233 let mut i = 0;
234 while i < 16 {
235 let x = src[i];
236 dst[i * 2] = lut[(x >> 4) as usize];
237 dst[i * 2 + 1] = lut[(x & 0x0f) as usize];
238 i += 1;
239 }
240 dst
241}
242
243#[inline]
244const fn format_hyphenated(src: &[u8; 16], upper: bool) -> [u8; 36] {
245 let lut = if upper { &UPPER } else { &LOWER };
246 let groups = [(0, 8), (9, 13), (14, 18), (19, 23), (24, 36)];
247 let mut dst = [0; 36];
248
249 let mut group_idx = 0;
250 let mut i = 0;
251 while group_idx < 5 {
252 let (start, end) = groups[group_idx];
253 let mut j = start;
254 while j < end {
255 let x = src[i];
256 i += 1;
257
258 dst[j] = lut[(x >> 4) as usize];
259 dst[j + 1] = lut[(x & 0x0f) as usize];
260 j += 2;
261 }
262 if group_idx < 4 {
263 dst[end] = b'-';
264 }
265 group_idx += 1;
266 }
267 dst
268}
269
270#[inline]
271fn encode_simple<'b>(src: &[u8; 16], buffer: &'b mut [u8], upper: bool) -> &'b mut str {
272 let buf = &mut buffer[..Simple::LENGTH];
273 let buf: &mut [u8; Simple::LENGTH] = buf.try_into().unwrap();
274 *buf = format_simple(src, upper);
275
276 unsafe { str::from_utf8_unchecked_mut(buf) }
278}
279
280#[inline]
281fn encode_hyphenated<'b>(src: &[u8; 16], buffer: &'b mut [u8], upper: bool) -> &'b mut str {
282 let buf = &mut buffer[..Hyphenated::LENGTH];
283 let buf: &mut [u8; Hyphenated::LENGTH] = buf.try_into().unwrap();
284 *buf = format_hyphenated(src, upper);
285
286 unsafe { str::from_utf8_unchecked_mut(buf) }
288}
289
290#[inline]
291fn encode_braced<'b>(src: &[u8; 16], buffer: &'b mut [u8], upper: bool) -> &'b mut str {
292 let buf = &mut buffer[..Hyphenated::LENGTH + 2];
293 let buf: &mut [u8; Hyphenated::LENGTH + 2] = buf.try_into().unwrap();
294
295 #[cfg_attr(
296 all(uuid_unstable, feature = "zerocopy"),
297 derive(zerocopy::IntoBytes)
298 )]
299 #[repr(C)]
300 struct Braced {
301 open_curly: u8,
302 hyphenated: [u8; Hyphenated::LENGTH],
303 close_curly: u8,
304 }
305
306 let braced = Braced {
307 open_curly: b'{',
308 hyphenated: format_hyphenated(src, upper),
309 close_curly: b'}',
310 };
311
312 *buf = unsafe { core::mem::transmute::<_, _>(braced) }unsafe_transmute!(braced);
313
314 unsafe { str::from_utf8_unchecked_mut(buf) }
316}
317
318#[inline]
319fn encode_urn<'b>(src: &[u8; 16], buffer: &'b mut [u8], upper: bool) -> &'b mut str {
320 let buf = &mut buffer[..Urn::LENGTH];
321 buf[..9].copy_from_slice(b"urn:uuid:");
322
323 let dst = &mut buf[9..(9 + Hyphenated::LENGTH)];
324 let dst: &mut [u8; Hyphenated::LENGTH] = dst.try_into().unwrap();
325 *dst = format_hyphenated(src, upper);
326
327 unsafe { str::from_utf8_unchecked_mut(buf) }
329}
330
331impl Hyphenated {
332 pub const LENGTH: usize = 36;
336
337 pub const fn from_uuid(uuid: Uuid) -> Self {
342 Hyphenated(uuid)
343 }
344
345 #[inline]
392 pub fn encode_lower<'buf>(&self, buffer: &'buf mut [u8]) -> &'buf mut str {
393 encode_hyphenated(self.0.as_bytes(), buffer, false)
394 }
395
396 #[inline]
443 pub fn encode_upper<'buf>(&self, buffer: &'buf mut [u8]) -> &'buf mut str {
444 encode_hyphenated(self.0.as_bytes(), buffer, true)
445 }
446
447 pub const fn as_uuid(&self) -> &Uuid {
458 &self.0
459 }
460
461 pub const fn into_uuid(self) -> Uuid {
472 self.0
473 }
474}
475
476impl Braced {
477 pub const LENGTH: usize = 38;
481
482 pub const fn from_uuid(uuid: Uuid) -> Self {
487 Braced(uuid)
488 }
489
490 #[inline]
537 pub fn encode_lower<'buf>(&self, buffer: &'buf mut [u8]) -> &'buf mut str {
538 encode_braced(self.0.as_bytes(), buffer, false)
539 }
540
541 #[inline]
588 pub fn encode_upper<'buf>(&self, buffer: &'buf mut [u8]) -> &'buf mut str {
589 encode_braced(self.0.as_bytes(), buffer, true)
590 }
591
592 pub const fn as_uuid(&self) -> &Uuid {
603 &self.0
604 }
605
606 pub const fn into_uuid(self) -> Uuid {
617 self.0
618 }
619}
620
621impl Simple {
622 pub const LENGTH: usize = 32;
626
627 pub const fn from_uuid(uuid: Uuid) -> Self {
632 Simple(uuid)
633 }
634
635 #[inline]
683 pub fn encode_lower<'buf>(&self, buffer: &'buf mut [u8]) -> &'buf mut str {
684 encode_simple(self.0.as_bytes(), buffer, false)
685 }
686
687 #[inline]
731 pub fn encode_upper<'buf>(&self, buffer: &'buf mut [u8]) -> &'buf mut str {
732 encode_simple(self.0.as_bytes(), buffer, true)
733 }
734
735 pub const fn as_uuid(&self) -> &Uuid {
746 &self.0
747 }
748
749 pub const fn into_uuid(self) -> Uuid {
760 self.0
761 }
762}
763
764impl Urn {
765 pub const LENGTH: usize = 45;
769
770 pub const fn from_uuid(uuid: Uuid) -> Self {
775 Urn(uuid)
776 }
777
778 #[inline]
828 pub fn encode_lower<'buf>(&self, buffer: &'buf mut [u8]) -> &'buf mut str {
829 encode_urn(self.0.as_bytes(), buffer, false)
830 }
831
832 #[inline]
881 pub fn encode_upper<'buf>(&self, buffer: &'buf mut [u8]) -> &'buf mut str {
882 encode_urn(self.0.as_bytes(), buffer, true)
883 }
884
885 pub const fn as_uuid(&self) -> &Uuid {
896 &self.0
897 }
898
899 pub const fn into_uuid(self) -> Uuid {
910 self.0
911 }
912}
913
914impl FromStr for Hyphenated {
915 type Err = Error;
916
917 fn from_str(s: &str) -> Result<Self, Self::Err> {
918 crate::parser::parse_hyphenated(s.as_bytes())
919 .map(|b| Hyphenated(Uuid(b)))
920 .map_err(|invalid| invalid.into_err())
921 }
922}
923
924impl FromStr for Simple {
925 type Err = Error;
926
927 fn from_str(s: &str) -> Result<Self, Self::Err> {
928 crate::parser::parse_simple(s.as_bytes())
929 .map(|b| Simple(Uuid(b)))
930 .map_err(|invalid| invalid.into_err())
931 }
932}
933
934impl FromStr for Urn {
935 type Err = Error;
936
937 fn from_str(s: &str) -> Result<Self, Self::Err> {
938 crate::parser::parse_urn(s.as_bytes())
939 .map(|b| Urn(Uuid(b)))
940 .map_err(|invalid| invalid.into_err())
941 }
942}
943
944impl FromStr for Braced {
945 type Err = Error;
946
947 fn from_str(s: &str) -> Result<Self, Self::Err> {
948 crate::parser::parse_braced(s.as_bytes())
949 .map(|b| Braced(Uuid(b)))
950 .map_err(|invalid| invalid.into_err())
951 }
952}
953
954macro_rules! impl_fmt_traits {
955 ($($T:ident<$($a:lifetime),*>),+) => {$(
956 impl<$($a),*> fmt::Display for $T<$($a),*> {
957 #[inline]
958 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
959 fmt::LowerHex::fmt(self, f)
960 }
961 }
962
963 impl<$($a),*> fmt::LowerHex for $T<$($a),*> {
964 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
965 f.write_str(self.encode_lower(&mut [0; Self::LENGTH]))
966 }
967 }
968
969 impl<$($a),*> fmt::UpperHex for $T<$($a),*> {
970 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
971 f.write_str(self.encode_upper(&mut [0; Self::LENGTH]))
972 }
973 }
974
975 impl_fmt_from!($T<$($a),*>);
976 )+}
977}
978
979macro_rules! impl_fmt_from {
980 ($T:ident<>) => {
981 impl From<Uuid> for $T {
982 #[inline]
983 fn from(f: Uuid) -> Self {
984 $T(f)
985 }
986 }
987
988 impl From<$T> for Uuid {
989 #[inline]
990 fn from(f: $T) -> Self {
991 f.into_uuid()
992 }
993 }
994
995 impl AsRef<Uuid> for $T {
996 #[inline]
997 fn as_ref(&self) -> &Uuid {
998 &self.0
999 }
1000 }
1001
1002 impl Borrow<Uuid> for $T {
1003 #[inline]
1004 fn borrow(&self) -> &Uuid {
1005 &self.0
1006 }
1007 }
1008 };
1009 ($T:ident<$a:lifetime>) => {
1010 impl<$a> From<&$a Uuid> for $T<$a> {
1011 #[inline]
1012 fn from(f: &$a Uuid) -> Self {
1013 $T::from_uuid_ref(f)
1014 }
1015 }
1016
1017 impl<$a> From<$T<$a>> for &$a Uuid {
1018 #[inline]
1019 fn from(f: $T<$a>) -> &$a Uuid {
1020 f.0
1021 }
1022 }
1023
1024 impl<$a> AsRef<Uuid> for $T<$a> {
1025 #[inline]
1026 fn as_ref(&self) -> &Uuid {
1027 self.0
1028 }
1029 }
1030
1031 impl<$a> Borrow<Uuid> for $T<$a> {
1032 #[inline]
1033 fn borrow(&self) -> &Uuid {
1034 self.0
1035 }
1036 }
1037 };
1038}
1039
1040impl fmt::Display for Braced<> {
#[inline]
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::LowerHex::fmt(self, f)
}
}
impl fmt::LowerHex for Braced<> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(self.encode_lower(&mut [0; Self::LENGTH]))
}
}
impl fmt::UpperHex for Braced<> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(self.encode_upper(&mut [0; Self::LENGTH]))
}
}
impl From<Uuid> for Braced {
#[inline]
fn from(f: Uuid) -> Self { Braced(f) }
}
impl From<Braced> for Uuid {
#[inline]
fn from(f: Braced) -> Self { f.into_uuid() }
}
impl AsRef<Uuid> for Braced {
#[inline]
fn as_ref(&self) -> &Uuid { &self.0 }
}
impl Borrow<Uuid> for Braced {
#[inline]
fn borrow(&self) -> &Uuid { &self.0 }
}impl_fmt_traits! {
1041 Hyphenated<>,
1042 Simple<>,
1043 Urn<>,
1044 Braced<>
1045}
1046
1047#[cfg(test)]
1048mod tests {
1049 use super::*;
1050
1051 #[test]
1052 fn hyphenated_trailing() {
1053 let mut buf = [b'x'; 100];
1054 let len = Uuid::nil().hyphenated().encode_lower(&mut buf).len();
1055 assert_eq!(len, super::Hyphenated::LENGTH);
1056 assert!(buf[len..].iter().all(|x| *x == b'x'));
1057 }
1058
1059 #[test]
1060 fn hyphenated_ref_trailing() {
1061 let mut buf = [b'x'; 100];
1062 let len = Uuid::nil().as_hyphenated().encode_lower(&mut buf).len();
1063 assert_eq!(len, super::Hyphenated::LENGTH);
1064 assert!(buf[len..].iter().all(|x| *x == b'x'));
1065 }
1066
1067 #[test]
1068 fn simple_trailing() {
1069 let mut buf = [b'x'; 100];
1070 let len = Uuid::nil().simple().encode_lower(&mut buf).len();
1071 assert_eq!(len, super::Simple::LENGTH);
1072 assert!(buf[len..].iter().all(|x| *x == b'x'));
1073 }
1074
1075 #[test]
1076 fn simple_ref_trailing() {
1077 let mut buf = [b'x'; 100];
1078 let len = Uuid::nil().as_simple().encode_lower(&mut buf).len();
1079 assert_eq!(len, super::Simple::LENGTH);
1080 assert!(buf[len..].iter().all(|x| *x == b'x'));
1081 }
1082
1083 #[test]
1084 fn urn_trailing() {
1085 let mut buf = [b'x'; 100];
1086 let len = Uuid::nil().urn().encode_lower(&mut buf).len();
1087 assert_eq!(len, super::Urn::LENGTH);
1088 assert!(buf[len..].iter().all(|x| *x == b'x'));
1089 }
1090
1091 #[test]
1092 fn urn_ref_trailing() {
1093 let mut buf = [b'x'; 100];
1094 let len = Uuid::nil().as_urn().encode_lower(&mut buf).len();
1095 assert_eq!(len, super::Urn::LENGTH);
1096 assert!(buf[len..].iter().all(|x| *x == b'x'));
1097 }
1098
1099 #[test]
1100 fn braced_trailing() {
1101 let mut buf = [b'x'; 100];
1102 let len = Uuid::nil().braced().encode_lower(&mut buf).len();
1103 assert_eq!(len, super::Braced::LENGTH);
1104 assert!(buf[len..].iter().all(|x| *x == b'x'));
1105 }
1106
1107 #[test]
1108 fn braced_ref_trailing() {
1109 let mut buf = [b'x'; 100];
1110 let len = Uuid::nil().as_braced().encode_lower(&mut buf).len();
1111 assert_eq!(len, super::Braced::LENGTH);
1112 assert!(buf[len..].iter().all(|x| *x == b'x'));
1113 }
1114
1115 #[test]
1116 #[should_panic]
1117 fn hyphenated_too_small() {
1118 Uuid::nil().hyphenated().encode_lower(&mut [0; 35]);
1119 }
1120
1121 #[test]
1122 #[should_panic]
1123 fn simple_too_small() {
1124 Uuid::nil().simple().encode_lower(&mut [0; 31]);
1125 }
1126
1127 #[test]
1128 #[should_panic]
1129 fn urn_too_small() {
1130 Uuid::nil().urn().encode_lower(&mut [0; 44]);
1131 }
1132
1133 #[test]
1134 #[should_panic]
1135 fn braced_too_small() {
1136 Uuid::nil().braced().encode_lower(&mut [0; 37]);
1137 }
1138
1139 #[test]
1140 fn hyphenated_to_inner() {
1141 let hyphenated = Uuid::nil().hyphenated();
1142 assert_eq!(Uuid::from(hyphenated), Uuid::nil());
1143 }
1144
1145 #[test]
1146 fn simple_to_inner() {
1147 let simple = Uuid::nil().simple();
1148 assert_eq!(Uuid::from(simple), Uuid::nil());
1149 }
1150
1151 #[test]
1152 fn urn_to_inner() {
1153 let urn = Uuid::nil().urn();
1154 assert_eq!(Uuid::from(urn), Uuid::nil());
1155 }
1156
1157 #[test]
1158 fn braced_to_inner() {
1159 let braced = Uuid::nil().braced();
1160 assert_eq!(Uuid::from(braced), Uuid::nil());
1161 }
1162}