Skip to content

Commit f1e927a

Browse files
authored
Merge pull request #5 from Amanieu/getrandom
Use getrandom directly instead of rand
2 parents e95a2ea + 87805c0 commit f1e927a

File tree

3 files changed

+26
-17
lines changed

3 files changed

+26
-17
lines changed

Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "const-random"
3-
version = "0.1.6"
3+
version = "0.1.7"
44
license = "MIT OR Apache-2.0"
55
repository = "https://github.com/tkaitchuck/constrandom"
66
documentation = "https://docs.rs/const-random"
@@ -11,5 +11,5 @@ readme = "README.md"
1111
edition = "2018"
1212

1313
[dependencies]
14-
const-random-macro = { path = "macro", version = "0.1.6"}
14+
const-random-macro = { path = "macro", version = "0.1.7"}
1515
proc-macro-hack = { version = "0.5" }

macro/Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "const-random-macro"
3-
version = "0.1.6"
3+
version = "0.1.7"
44
license = "MIT OR Apache-2.0"
55
repository = "https://github.com/tkaitchuck/constrandom"
66
documentation = "https://docs.rs/const-random"
@@ -14,4 +14,4 @@ proc-macro = true
1414

1515
[dependencies]
1616
proc-macro-hack = { version = "0.5" }
17-
rand = { version = "0.7", default-features = false, features = ["getrandom"] }
17+
getrandom = "0.1"

macro/src/lib.rs

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,33 @@
11
extern crate proc_macro;
22

3+
use getrandom;
34
use proc_macro::TokenStream;
45
use proc_macro_hack::proc_macro_hack;
5-
use rand::rngs::OsRng;
6-
use rand::Rng;
6+
use std::mem;
7+
8+
// Ideally we would use the proper interface for this through the rand crate,
9+
// but due to https://github.com/rust-lang/cargo/issues/5730 this leads to
10+
// issues for no_std crates that try to use rand themselves. So instead we skip
11+
// rand and generate random bytes straight from the OS.
12+
fn gen_random<T>() -> T {
13+
let mut out = [0u8; 16];
14+
getrandom::getrandom(&mut out).unwrap();
15+
unsafe { mem::transmute_copy(&out) }
16+
}
717

818
#[proc_macro_hack]
919
pub fn const_random(input: TokenStream) -> TokenStream {
1020
match &input.to_string()[..] {
11-
"u8" => format!("0x{:x}", OsRng.gen::<u8>()).parse().unwrap(),
12-
"u16" => format!("0x{:x}", OsRng.gen::<u16>()).parse().unwrap(),
13-
"u32" => format!("0x{:x}", OsRng.gen::<u32>()).parse().unwrap(),
14-
"u64" => format!("0x{:x}", OsRng.gen::<u64>()).parse().unwrap(),
15-
"u128" => format!("0x{:x}", OsRng.gen::<u128>()).parse().unwrap(),
16-
"i8" => format!("0x{:x}", OsRng.gen::<i8>()).parse().unwrap(),
17-
"i16" => format!("0x{:x}", OsRng.gen::<i16>()).parse().unwrap(),
18-
"i32" => format!("0x{:x}", OsRng.gen::<i32>()).parse().unwrap(),
19-
"i64" => format!("0x{:x}", OsRng.gen::<i64>()).parse().unwrap(),
20-
"i128" => format!("0x{:x}", OsRng.gen::<i128>()).parse().unwrap(),
21+
"u8" => format!("0x{:x}", gen_random::<u8>()).parse().unwrap(),
22+
"u16" => format!("0x{:x}", gen_random::<u16>()).parse().unwrap(),
23+
"u32" => format!("0x{:x}", gen_random::<u32>()).parse().unwrap(),
24+
"u64" => format!("0x{:x}", gen_random::<u64>()).parse().unwrap(),
25+
"u128" => format!("0x{:x}", gen_random::<u128>()).parse().unwrap(),
26+
"i8" => format!("0x{:x}", gen_random::<i8>()).parse().unwrap(),
27+
"i16" => format!("0x{:x}", gen_random::<i16>()).parse().unwrap(),
28+
"i32" => format!("0x{:x}", gen_random::<i32>()).parse().unwrap(),
29+
"i64" => format!("0x{:x}", gen_random::<i64>()).parse().unwrap(),
30+
"i128" => format!("0x{:x}", gen_random::<i128>()).parse().unwrap(),
2131
_ => panic!("Invalid integer type"),
2232
}
23-
2433
}

0 commit comments

Comments
 (0)