| 1 | use crate::Uuid; | 
| 2 |  | 
|---|
| 3 | impl Uuid { | 
|---|
| 4 | /// Creates a random UUID. | 
|---|
| 5 | /// | 
|---|
| 6 | /// This uses the [`getrandom`] crate to utilise the operating system's RNG | 
|---|
| 7 | /// as the source of random numbers. If you'd like to use a custom | 
|---|
| 8 | /// generator, don't use this method: generate random bytes using your | 
|---|
| 9 | /// custom generator and pass them to the | 
|---|
| 10 | /// [`uuid::Builder::from_random_bytes`][from_random_bytes] function | 
|---|
| 11 | /// instead. | 
|---|
| 12 | /// | 
|---|
| 13 | /// Note that usage of this method requires the `v4` feature of this crate | 
|---|
| 14 | /// to be enabled. | 
|---|
| 15 | /// | 
|---|
| 16 | /// # Examples | 
|---|
| 17 | /// | 
|---|
| 18 | /// Basic usage: | 
|---|
| 19 | /// | 
|---|
| 20 | /// ``` | 
|---|
| 21 | /// # use uuid::{Uuid, Version}; | 
|---|
| 22 | /// let uuid = Uuid::new_v4(); | 
|---|
| 23 | /// | 
|---|
| 24 | /// assert_eq!(Some(Version::Random), uuid.get_version()); | 
|---|
| 25 | /// ``` | 
|---|
| 26 | /// | 
|---|
| 27 | /// # References | 
|---|
| 28 | /// | 
|---|
| 29 | /// * [UUID Version 4 in RFC 9562](https://www.ietf.org/rfc/rfc9562.html#section-5.4) | 
|---|
| 30 | /// | 
|---|
| 31 | /// [`getrandom`]: https://crates.io/crates/getrandom | 
|---|
| 32 | /// [from_random_bytes]: struct.Builder.html#method.from_random_bytes | 
|---|
| 33 | pub fn new_v4() -> Uuid { | 
|---|
| 34 | // This is an optimized method for generating random UUIDs that just masks | 
|---|
| 35 | // out the bits for the version and variant and sets them both together | 
|---|
| 36 | Uuid::from_u128( | 
|---|
| 37 | crate::rng::u128() & 0xFFFFFFFFFFFF4FFFBFFFFFFFFFFFFFFF | 0x40008000000000000000, | 
|---|
| 38 | ) | 
|---|
| 39 | } | 
|---|
| 40 | } | 
|---|
| 41 |  | 
|---|
| 42 | #[ cfg(test)] | 
|---|
| 43 | mod tests { | 
|---|
| 44 | use super::*; | 
|---|
| 45 | use crate::{Variant, Version}; | 
|---|
| 46 |  | 
|---|
| 47 | #[ cfg(all( | 
|---|
| 48 | target_arch = "wasm32", | 
|---|
| 49 | target_vendor = "unknown", | 
|---|
| 50 | target_os = "unknown" | 
|---|
| 51 | ))] | 
|---|
| 52 | use wasm_bindgen_test::*; | 
|---|
| 53 |  | 
|---|
| 54 | #[ test] | 
|---|
| 55 | #[ cfg_attr( | 
|---|
| 56 | all( | 
|---|
| 57 | target_arch = "wasm32", | 
|---|
| 58 | target_vendor = "unknown", | 
|---|
| 59 | target_os = "unknown" | 
|---|
| 60 | ), | 
|---|
| 61 | wasm_bindgen_test | 
|---|
| 62 | )] | 
|---|
| 63 | fn test_new() { | 
|---|
| 64 | let uuid = Uuid::new_v4(); | 
|---|
| 65 |  | 
|---|
| 66 | assert_eq!(uuid.get_version(), Some(Version::Random)); | 
|---|
| 67 | assert_eq!(uuid.get_variant(), Variant::RFC4122); | 
|---|
| 68 | } | 
|---|
| 69 |  | 
|---|
| 70 | #[ test] | 
|---|
| 71 | #[ cfg_attr( | 
|---|
| 72 | all( | 
|---|
| 73 | target_arch = "wasm32", | 
|---|
| 74 | target_vendor = "unknown", | 
|---|
| 75 | target_os = "unknown" | 
|---|
| 76 | ), | 
|---|
| 77 | wasm_bindgen_test | 
|---|
| 78 | )] | 
|---|
| 79 | fn test_get_version() { | 
|---|
| 80 | let uuid = Uuid::new_v4(); | 
|---|
| 81 |  | 
|---|
| 82 | assert_eq!(uuid.get_version(), Some(Version::Random)); | 
|---|
| 83 | assert_eq!(uuid.get_version_num(), 4) | 
|---|
| 84 | } | 
|---|
| 85 | } | 
|---|
| 86 |  | 
|---|