ps_uuid/implementations/
as_ref.rs1use crate::{UUID, UUID_BYTES};
2
3impl AsRef<[u8; UUID_BYTES]> for UUID {
4 fn as_ref(&self) -> &[u8; UUID_BYTES] {
5 self.as_bytes()
6 }
7}
8
9impl AsRef<[u8]> for UUID {
10 fn as_ref(&self) -> &[u8] {
11 self.as_bytes()
12 }
13}
14
15#[cfg(test)]
16mod tests {
17 use crate::{UUID, UUID_BYTES};
18
19 #[test]
20 fn as_ref_slice_matches_as_bytes() {
21 let uuid = UUID::from(0x0123_4567_89ab_cdef_u128);
22 let slice: &[u8] = uuid.as_ref();
23 assert_eq!(slice, uuid.as_bytes().as_slice());
24 }
25
26 #[test]
27 fn as_ref_array_matches_as_bytes() {
28 let uuid = UUID::from(0x0123_4567_89ab_cdef_u128);
29 let arr: &[u8; UUID_BYTES] = uuid.as_ref();
30 assert_eq!(arr, uuid.as_bytes());
31 assert!(std::ptr::eq(arr, uuid.as_bytes()));
32 }
33
34 #[test]
35 fn as_ref_slice_length() {
36 let uuid = UUID::nil();
37 let slice: &[u8] = uuid.as_ref();
38 assert_eq!(slice.len(), UUID_BYTES);
39 }
40
41 #[test]
42 fn as_ref_nil() {
43 let uuid = UUID::nil();
44 let slice: &[u8] = uuid.as_ref();
45 assert!(slice.iter().all(|&b| b == 0));
46 }
47
48 #[test]
49 fn as_ref_max() {
50 let uuid = UUID::max();
51 let slice: &[u8] = uuid.as_ref();
52 assert!(slice.iter().all(|&b| b == 0xFF));
53 }
54
55 #[test]
56 fn works_with_generic_as_ref_u8() {
57 fn accepts_as_ref(v: &impl AsRef<[u8]>) -> usize {
58 v.as_ref().len()
59 }
60 let uuid = UUID::from(42u128);
61 assert_eq!(accepts_as_ref(&uuid), UUID_BYTES);
62 }
63
64 #[test]
65 fn works_with_generic_as_ref_array() {
66 fn accepts_as_ref(v: &impl AsRef<[u8; UUID_BYTES]>) -> u8 {
67 v.as_ref()[0]
68 }
69 let uuid = UUID::from(0xFF00_0000_0000_0000_0000_0000_0000_0000_u128);
70 assert_eq!(accepts_as_ref(&uuid), 0xFF);
71 }
72}