forked from RustPython/RustPython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuuid.rs
More file actions
46 lines (38 loc) · 1.27 KB
/
uuid.rs
File metadata and controls
46 lines (38 loc) · 1.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
pub(crate) use _uuid::make_module;
#[pymodule]
mod _uuid {
use crate::{builtins::PyNone, vm::VirtualMachine};
use mac_address::get_mac_address;
use once_cell::sync::OnceCell;
use rand::Rng;
use std::time::{Duration, SystemTime};
use uuid::{
v1::{Context, Timestamp},
Uuid,
};
fn get_node_id() -> [u8; 6] {
match get_mac_address() {
Ok(Some(_ma)) => get_mac_address().unwrap().unwrap().bytes(),
_ => rand::thread_rng().gen::<[u8; 6]>(),
}
}
pub fn now_unix_duration() -> Duration {
use std::time::UNIX_EPOCH;
let now = SystemTime::now();
now.duration_since(UNIX_EPOCH)
.expect("SystemTime before UNIX EPOCH!")
}
#[pyfunction]
fn generate_time_safe() -> (Vec<u8>, PyNone) {
static CONTEXT: Context = Context::new(0);
let now = now_unix_duration();
let ts = Timestamp::from_unix(&CONTEXT, now.as_secs(), now.subsec_nanos());
static NODE_ID: OnceCell<[u8; 6]> = OnceCell::new();
let unique_node_id = NODE_ID.get_or_init(get_node_id);
(Uuid::new_v1(ts, unique_node_id).as_bytes().to_vec(), PyNone)
}
#[pyattr]
fn has_uuid_generate_time_safe(_vm: &VirtualMachine) -> u32 {
0
}
}