| 1 |
use crate::{Builder, Uuid}; |
| 2 |
|
| 3 |
impl 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 |
/// * [Version 8 UUIDs in Draft RFC: New UUID Formats, Version 4](https://datatracker.ietf.org/doc/html/draft-peabody-dispatch-new-uuid-format-04#section-5.3) |
| 28 |
pub fn new_v8(buf: [u8; 16]) -> Uuid { |
| 29 |
Builder::from_custom_bytes(buf).into_uuid() |
| 30 |
} |
| 31 |
} |
| 32 |
|
| 33 |
#[cfg(test)] |
| 34 |
mod tests { |
| 35 |
use super::*; |
| 36 |
use crate::{Variant, Version}; |
| 37 |
use std::string::ToString; |
| 38 |
|
| 39 |
#[cfg(all( |
| 40 |
target_arch = "wasm32", |
| 41 |
target_vendor = "unknown", |
| 42 |
target_os = "unknown" |
| 43 |
))] |
| 44 |
use wasm_bindgen_test::*; |
| 45 |
|
| 46 |
#[test] |
| 47 |
#[cfg_attr( |
| 48 |
all( |
| 49 |
target_arch = "wasm32", |
| 50 |
target_vendor = "unknown", |
| 51 |
target_os = "unknown" |
| 52 |
), |
| 53 |
wasm_bindgen_test |
| 54 |
)] |
| 55 |
fn test_new() { |
| 56 |
let buf: [u8; 16] = [ |
| 57 |
0xf, 0xe, 0xd, 0xc, 0xb, 0xa, 0x9, 0x8, 0x7, 0x6, 0x5, 0x4, 0x3, 0x2, 0x1, 0x0, |
| 58 |
]; |
| 59 |
let uuid = Uuid::new_v8(buf); |
| 60 |
assert_eq!(uuid.get_version(), Some(Version::Custom)); |
| 61 |
assert_eq!(uuid.get_variant(), Variant::RFC4122); |
| 62 |
assert_eq!(uuid.get_version_num(), 8); |
| 63 |
assert_eq!( |
| 64 |
uuid.hyphenated().to_string(), |
| 65 |
"0f0e0d0c-0b0a-8908-8706-050403020100" |
| 66 |
); |
| 67 |
} |
| 68 |
} |