Skip to main content

ps_uuid/methods/
from_u128.rs

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