2023-09-04 22:00:00 -04:00
|
|
|
use crate::rng::get_random_bytes;
|
|
|
|
|
|
|
|
use crate::{Uuid, Variant, Version};
|
|
|
|
|
|
|
|
impl Uuid {
|
|
|
|
pub fn new_v4() -> Self {
|
|
|
|
Uuid::new_random()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn new_random() -> Self {
|
|
|
|
let mut bytes = get_random_bytes();
|
|
|
|
|
|
|
|
Uuid::set_variant(&mut bytes, Variant::Default);
|
|
|
|
Uuid::set_version(&mut bytes, Version::Random);
|
|
|
|
|
|
|
|
Uuid(bytes)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2023-09-08 15:51:26 -04:00
|
|
|
use crate::{Uuid, Variant, Version};
|
2023-09-04 22:00:00 -04:00
|
|
|
|
|
|
|
#[test]
|
2023-09-08 15:51:26 -04:00
|
|
|
fn test_new_v4_version() {
|
|
|
|
let uuid = Uuid::new_v4();
|
|
|
|
|
|
|
|
assert_eq!(uuid.get_version(), Version::Random);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_new_v4_version_num() {
|
|
|
|
let uuid = Uuid::new_v4();
|
|
|
|
|
|
|
|
assert_eq!(uuid.get_version_num(), 4);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_new_v4_variant() {
|
2023-09-04 22:00:00 -04:00
|
|
|
let uuid = Uuid::new_v4();
|
|
|
|
|
|
|
|
assert_eq!(uuid.get_variant(), Variant::Default);
|
|
|
|
}
|
|
|
|
}
|