Skip to main content

ps_uuid/implementations/
from_bytes.rs

1use crate::{UUID, UUID_BYTES};
2
3impl From<[u8; UUID_BYTES]> for UUID {
4    fn from(bytes: [u8; UUID_BYTES]) -> Self {
5        Self::from_bytes(bytes)
6    }
7}
8
9impl From<UUID> for [u8; UUID_BYTES] {
10    fn from(uuid: UUID) -> Self {
11        *uuid.as_bytes()
12    }
13}
14
15#[cfg(test)]
16mod tests {
17    use crate::{UUID, UUID_BYTES};
18
19    #[test]
20    fn from_array_roundtrip() {
21        let bytes: [u8; UUID_BYTES] = [
22            0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E,
23            0x0F, 0x10,
24        ];
25        let uuid = UUID::from(bytes);
26        let back: [u8; UUID_BYTES] = uuid.into();
27        assert_eq!(bytes, back);
28    }
29
30    #[test]
31    fn from_array_matches_from_bytes() {
32        let bytes = [0xAB; UUID_BYTES];
33        assert_eq!(UUID::from(bytes), UUID::from_bytes(bytes));
34    }
35
36    #[test]
37    fn into_array_matches_as_bytes() {
38        let uuid = UUID::from(0x0123_4567_89ab_cdef_u128);
39        let arr: [u8; UUID_BYTES] = uuid.into();
40        assert_eq!(&arr, uuid.as_bytes());
41    }
42
43    #[test]
44    fn nil_roundtrip() {
45        let uuid = UUID::nil();
46        let arr: [u8; UUID_BYTES] = uuid.into();
47        assert_eq!(arr, [0u8; UUID_BYTES]);
48        assert_eq!(UUID::from(arr), uuid);
49    }
50
51    #[test]
52    fn max_roundtrip() {
53        let uuid = UUID::max();
54        let arr: [u8; UUID_BYTES] = uuid.into();
55        assert_eq!(arr, [0xFF; UUID_BYTES]);
56        assert_eq!(UUID::from(arr), uuid);
57    }
58}