ps_uuid/methods/nil.rs
1use crate::{UUID, UUID_BYTES};
2
3impl UUID {
4 #[must_use]
5 pub const fn nil() -> Self {
6 Self {
7 bytes: [0; UUID_BYTES],
8 }
9 }
10}
11
12#[cfg(test)]
13mod tests {
14 use super::*;
15
16 /// Tests that the `nil()` function correctly produces a UUID with all
17 /// zero bytes.
18 #[test]
19 fn test_nil_is_all_zeros() {
20 let nil_uuid = UUID::nil();
21 let expected_bytes = [0u8; UUID_BYTES];
22 assert_eq!(
23 nil_uuid.bytes, expected_bytes,
24 "The bytes of a nil UUID should all be zero"
25 );
26 }
27
28 /// Tests that the `nil()` function is deterministic and that two generated
29 /// nil UUIDs are equal to each other.
30 #[test]
31 fn test_nil_is_deterministic() {
32 let nil1 = UUID::nil();
33 let nil2 = UUID::nil();
34 assert_eq!(nil1, nil2, "Two nil UUIDs should be equal");
35 }
36
37 /// This test verifies the `const` nature of the `nil()` function.
38 /// The test logic itself is trivial; the key is that the code *compiles*.
39 /// By successfully assigning the result of `UUID::nil()` to a `const`
40 /// item, we prove that it can be evaluated at compile time.
41 #[test]
42 fn test_nil_can_be_used_in_const_context() {
43 const COMPILE_TIME_NIL: UUID = UUID::nil();
44 const EXPECTED_NIL: UUID = UUID {
45 bytes: [0; UUID_BYTES],
46 };
47
48 // This assertion is somewhat redundant if the other tests pass,
49 // but it completes the test case.
50 assert_eq!(
51 COMPILE_TIME_NIL, EXPECTED_NIL,
52 "A const-evaluated nil UUID should match the expected value"
53 );
54
55 // We can also use it directly in a match arm or other const places.
56 match UUID::nil() {
57 COMPILE_TIME_NIL => (), // This arm must match
58 _ => panic!("A const nil UUID should match UUID::nil()"),
59 }
60 }
61}