Skip to content

Commit 42290b1

Browse files
authored
Add: _uuid module (#3974)
1 parent d39a28b commit 42290b1

File tree

4 files changed

+110
-2
lines changed

4 files changed

+110
-2
lines changed

Cargo.lock

Lines changed: 47 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

stdlib/Cargo.toml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,12 @@ num_enum = "0.5.7"
6666
ascii = "1.0.0"
6767
parking_lot = "0.12.0"
6868

69+
# uuid
70+
[target.'cfg(not(any(target_os = "ios", target_os = "android", target_os = "windows", target_arch = "wasm32")))'.dependencies]
71+
mac_address = "1.1.3"
72+
once_cell = "1.13.0"
73+
uuid = { version = "1.1.2", features = ["v1", "fast-rng", "macro-diagnostics"] }
74+
6975
# mmap
7076
[target.'cfg(all(unix, not(target_arch = "wasm32")))'.dependencies]
7177
memmap2 = "0.5.4"

stdlib/src/lib.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,13 @@ mod select;
5555
mod ssl;
5656
#[cfg(all(unix, not(target_os = "redox"), not(target_os = "ios")))]
5757
mod termios;
58+
#[cfg(not(any(
59+
target_os = "android",
60+
target_os = "ios",
61+
target_os = "windows",
62+
target_arch = "wasm32"
63+
)))]
64+
mod uuid;
5865

5966
use rustpython_common as common;
6067
use rustpython_vm as vm;
@@ -149,5 +156,9 @@ pub fn get_module_inits() -> impl Iterator<Item = (Cow<'static, str>, StdlibInit
149156
{
150157
"_scproxy" => scproxy::make_module,
151158
}
159+
#[cfg(not(any(target_os = "android", target_os = "ios", target_os = "windows", target_arch = "wasm32")))]
160+
{
161+
"_uuid" => uuid::make_module,
162+
}
152163
}
153164
}

stdlib/src/uuid.rs

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
pub(crate) use _uuid::make_module;
2+
3+
#[pymodule]
4+
mod _uuid {
5+
use crate::{builtins::PyNone, vm::VirtualMachine};
6+
use mac_address::get_mac_address;
7+
use once_cell::sync::OnceCell;
8+
use rand::Rng;
9+
use std::time::{Duration, SystemTime};
10+
use uuid::{
11+
v1::{Context, Timestamp},
12+
Uuid,
13+
};
14+
15+
fn get_node_id() -> [u8; 6] {
16+
match get_mac_address() {
17+
Ok(Some(_ma)) => get_mac_address().unwrap().unwrap().bytes(),
18+
_ => rand::thread_rng().gen::<[u8; 6]>(),
19+
}
20+
}
21+
22+
pub fn now_unix_duration() -> Duration {
23+
use std::time::UNIX_EPOCH;
24+
25+
let now = SystemTime::now();
26+
now.duration_since(UNIX_EPOCH)
27+
.expect("SystemTime before UNIX EPOCH!")
28+
}
29+
30+
#[pyfunction]
31+
fn generate_time_safe() -> (Vec<u8>, PyNone) {
32+
static CONTEXT: Context = Context::new(0);
33+
let now = now_unix_duration();
34+
let ts = Timestamp::from_unix(&CONTEXT, now.as_secs(), now.subsec_nanos());
35+
36+
static NODE_ID: OnceCell<[u8; 6]> = OnceCell::new();
37+
let unique_node_id = NODE_ID.get_or_init(get_node_id);
38+
39+
(Uuid::new_v1(ts, unique_node_id).as_bytes().to_vec(), PyNone)
40+
}
41+
42+
#[pyattr]
43+
fn has_uuid_generate_time_safe(_vm: &VirtualMachine) -> u32 {
44+
0
45+
}
46+
}

0 commit comments

Comments
 (0)