Debian Codegraph @

third_party/rust/uuid/src/v7.rs

raw
1 //! The implementation for Version 7 UUIDs.
2 //!
3 //! Note that you need to enable the `v7` Cargo feature
4 //! in order to use this module.
5
6 use crate::{std::convert::TryInto, rng, timestamp::Timestamp, Builder, Uuid};
7
8 impl Uuid {
9 /// Create a new version 7 UUID using the current time value and random bytes.
10 ///
11 /// This method is a convenient alternative to [`Uuid::new_v7`] that uses the current system time
12 /// as the source timestamp.
13 #[cfg(feature = "std")]
14 pub fn now_v7() -> Self {
15 Self::new_v7(Timestamp::now(crate::NoContext))
16 }
17
18 /// Create a new version 7 UUID using a time value and random bytes.
19 ///
20 /// When the `std` feature is enabled, you can also use [`Uuid::now_v7`].
21 ///
22 /// Note that usage of this method requires the `v7` feature of this crate
23 /// to be enabled.
24 ///
25 /// Also see [`Uuid::now_v7`] for a convenient way to generate version 7
26 /// UUIDs using the current system time.
27 ///
28 /// # Examples
29 ///
30 /// A v7 UUID can be created from a unix [`Timestamp`] plus a 128 bit
31 /// random number. When supplied as such, the data will be
32 ///
33 /// ```rust
34 /// # use uuid::{Uuid, Timestamp, NoContext};
35 /// let ts = Timestamp::from_unix(NoContext, 1497624119, 1234);
36 ///
37 /// let uuid = Uuid::new_v7(ts);
38 ///
39 /// assert!(
40 /// uuid.hyphenated().to_string().starts_with("015cb15a-86d8-7")
41 /// );
42 /// ```
43 ///
44 /// # References
45 ///
46 /// * [Version 7 UUIDs in Draft RFC: New UUID Formats, Version 4](https://datatracker.ietf.org/doc/html/draft-peabody-dispatch-new-uuid-format-04#section-5.2)
47 pub fn new_v7(ts: Timestamp) -> Self {
48 let (secs, nanos) = ts.to_unix();
49 let millis = (secs * 1000).saturating_add(nanos as u64 / 1_000_000);
50
51 Builder::from_unix_timestamp_millis(millis, &rng::bytes()[..10].try_into().unwrap())
52 .into_uuid()
53 }
54 }
55
56 #[cfg(test)]
57 mod tests {
58 use super::*;
59 use crate::{std::string::ToString, NoContext, Variant, Version};
60 #[cfg(target_arch = "wasm32")]
61 use wasm_bindgen_test::*;
62
63 #[test]
64 #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
65 fn test_new() {
66 let ts: u64 = 1645557742000;
67
68 let seconds = ts / 1000;
69 let nanos = ((ts % 1000) * 1_000_000) as u32;
70
71 let uuid = Uuid::new_v7(Timestamp::from_unix(NoContext, seconds, nanos));
72 let uustr = uuid.hyphenated().to_string();
73
74 assert_eq!(uuid.get_version(), Some(Version::SortRand));
75 assert_eq!(uuid.get_variant(), Variant::RFC4122);
76 assert!(uuid.hyphenated().to_string().starts_with("017f22e2-79b0-7"));
77
78 // Ensure parsing the same UUID produces the same timestamp
79 let parsed = Uuid::parse_str(uustr.as_str()).unwrap();
80
81 assert_eq!(uuid, parsed);
82 }
83
84 #[test]
85 #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
86 #[cfg(feature = "std")]
87 fn test_now() {
88 let uuid = Uuid::now_v7();
89
90 assert_eq!(uuid.get_version(), Some(Version::SortRand));
91 assert_eq!(uuid.get_variant(), Variant::RFC4122);
92 }
93
94 #[test]
95 #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
96 fn test_sorting() {
97 let time1: u64 = 1_496_854_535;
98 let time_fraction1: u32 = 812_000_000;
99
100 let time2 = time1 + 4000;
101 let time_fraction2 = time_fraction1;
102
103 let uuid1 = Uuid::new_v7(Timestamp::from_unix(NoContext, time1, time_fraction1));
104 let uuid2 = Uuid::new_v7(Timestamp::from_unix(NoContext, time2, time_fraction2));
105
106 assert!(uuid1.as_bytes() < uuid2.as_bytes());
107 assert!(uuid1.to_string() < uuid2.to_string());
108 }
109
110 #[test]
111 #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
112 fn test_new_timestamp_roundtrip() {
113 let time: u64 = 1_496_854_535;
114 let time_fraction: u32 = 812_000_000;
115
116 let ts = Timestamp::from_unix(NoContext, time, time_fraction);
117
118 let uuid = Uuid::new_v7(ts);
119
120 let decoded_ts = uuid.get_timestamp().unwrap();
121
122 assert_eq!(ts.to_unix(), decoded_ts.to_unix());
123 }
124 }