Skip to main content

ps_uuid/methods/
to_u128.rs

1use crate::UUID;
2
3impl UUID {
4    /// Returns the UUID as a `u128` integer in big-endian byte order.
5    ///
6    /// This is a `const fn` equivalent of `u128::from(uuid)`.
7    #[must_use]
8    pub const fn to_u128(&self) -> u128 {
9        u128::from_be_bytes(self.bytes)
10    }
11}
12
13#[cfg(test)]
14mod tests {
15    use crate::UUID;
16
17    #[test]
18    fn nil_returns_zero() {
19        assert_eq!(UUID::nil().to_u128(), 0);
20    }
21
22    #[test]
23    fn max_returns_u128_max() {
24        assert_eq!(UUID::max().to_u128(), u128::MAX);
25    }
26
27    #[test]
28    fn roundtrip_with_from_u128() {
29        let value: u128 = 0x0123_4567_89ab_cdef_0123_4567_89ab_cdef;
30        let uuid = UUID::from_u128(value);
31        assert_eq!(uuid.to_u128(), value);
32    }
33
34    #[test]
35    fn const_context() {
36        const VALUE: u128 = UUID::nil().to_u128();
37        assert_eq!(VALUE, 0);
38    }
39}