use std::time::{SystemTime, UNIX_EPOCH};
pub fn uuid_v4() -> String {
let mut b = [0u8; 16];
let mut s = seed();
for chunk in b.chunks_mut(8) {
s = xorshift64(s);
chunk.copy_from_slice(&s.to_le_bytes()[..chunk.len()]);
}
b[6] = (b[6] & 0x0f) | 0x40;
b[8] = (b[8] & 0x3f) | 0x80;
format!(
"{:02x}{:02x}{:02x}{:02x}-{:02x}{:02x}-{:02x}{:02x}-{:02x}{:02x}-{:02x}{:02x}{:02x}{:02x}{:02x}{:02x}",
b[0], b[1], b[2], b[3],
b[4], b[5],
b[6], b[7],
b[8], b[9],
b[10], b[11], b[12], b[13], b[14], b[15]
)
}
fn seed() -> u64 {
let t = SystemTime::now()
.duration_since(UNIX_EPOCH)
.map(|d| d.as_nanos() as u64)
.unwrap_or(0x5eed_c0de);
t ^ (&t as *const u64 as u64)
}
fn xorshift64(mut x: u64) -> u64 {
if x == 0 {
x = 0xdead_beef_cafe_babe;
}
x ^= x << 13;
x ^= x >> 7;
x ^= x << 17;
x
}