[go: up one dir, main page]

uuid/
v8.rs

1use crate::{Builder, Uuid};
2
3impl Uuid {
4    /// Creates a custom UUID comprised almost entirely of user-supplied bytes.
5    ///
6    /// This will inject the UUID Version at 4 bits starting at the 48th bit
7    /// and the Variant into 2 bits 64th bit. Any existing bits in the user-supplied bytes
8    /// at those locations will be overridden.
9    ///
10    /// Note that usage of this method requires the `v8` feature of this crate
11    /// to be enabled.
12    ///
13    /// # Examples
14    ///
15    /// Basic usage:
16    ///
17    /// ```
18    /// # use uuid::{Uuid, Version};
19    /// let buf: [u8; 16] = *b"abcdefghijklmnop";
20    /// let uuid = Uuid::new_v8(buf);
21    ///
22    /// assert_eq!(Some(Version::Custom), uuid.get_version());
23    /// ```
24    ///
25    /// # References
26    ///
27    /// * [UUID Version 8 in RFC 9562](https://www.ietf.org/rfc/rfc9562.html#section-5.8)
28    pub const fn new_v8(buf: [u8; 16]) -> Uuid {
29        Builder::from_custom_bytes(buf).into_uuid()
30    }
31}
32
33#[cfg(test)]
34mod tests {
35    use super::*;
36    use crate::{Variant, Version};
37    use std::string::ToString;
38
39    #[cfg(all(target_arch = "wasm32", any(target_os = "unknown", target_os = "none")))]
40    use wasm_bindgen_test::*;
41
42    #[test]
43    #[cfg_attr(
44        all(target_arch = "wasm32", any(target_os = "unknown", target_os = "none")),
45        wasm_bindgen_test
46    )]
47    fn test_new() {
48        let buf: [u8; 16] = [
49            0xf, 0xe, 0xd, 0xc, 0xb, 0xa, 0x9, 0x8, 0x7, 0x6, 0x5, 0x4, 0x3, 0x2, 0x1, 0x0,
50        ];
51        let uuid = Uuid::new_v8(buf);
52        assert_eq!(uuid.get_version(), Some(Version::Custom));
53        assert_eq!(uuid.get_variant(), Variant::RFC4122);
54        assert_eq!(uuid.get_version_num(), 8);
55        assert_eq!(
56            uuid.hyphenated().to_string(),
57            "0f0e0d0c-0b0a-8908-8706-050403020100"
58        );
59    }
60}