Skip to content

Commit da4b092

Browse files
committed
rdrand: Use once_cell::race::OnceBool
1 parent 267639e commit da4b092

File tree

3 files changed

+13
-6
lines changed

3 files changed

+13
-6
lines changed

Cargo.toml

+9-1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ exclude = [".*"]
1313

1414
[dependencies]
1515
cfg-if = "1"
16+
once_cell = { version = "1.19.0", default-features = false, optional = true }
17+
18+
[target.'cfg(all(target_arch = "x86_64", target_env = "sgx"))'.dependencies]
19+
once_cell = { version = "1.19.0", default-features = false, features = ["race"] }
1620

1721
# When built as part of libstd
1822
compiler_builtins = { version = "0.1", optional = true }
@@ -30,6 +34,10 @@ windows-targets = "0.52"
3034
[target.'cfg(all(any(target_arch = "wasm32", target_arch = "wasm64"), target_os = "unknown"))'.dependencies]
3135
wasm-bindgen = { version = "0.2.62", default-features = false, optional = true }
3236
js-sys = { version = "0.3", optional = true }
37+
38+
[target.'cfg(any(target_arch = "x86", target_arch = "x86_64"))'.dev-dependencies]
39+
once_cell = { version = "1.19.0", default-features = false, features = ["race"] }
40+
3341
[target.'cfg(all(any(target_arch = "wasm32", target_arch = "wasm64"), target_os = "unknown"))'.dev-dependencies]
3442
wasm-bindgen-test = "0.3.18"
3543

@@ -40,7 +48,7 @@ std = []
4048
# Bumps minimum supported Linux kernel version to 3.17 and Android API level to 23 (Marshmallow).
4149
linux_disable_fallback = []
4250
# Feature to enable fallback RDRAND-based implementation on x86/x86_64
43-
rdrand = []
51+
rdrand = ["once_cell/race"]
4452
# Feature to enable JavaScript bindings on wasm*-unknown-unknown
4553
js = ["wasm-bindgen", "js-sys"]
4654
# Feature to enable custom RNG implementations

src/rdrand.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
//! RDRAND backend for x86(-64) targets
2-
use crate::{lazy::LazyBool, util::slice_as_uninit, Error};
2+
use crate::{util::slice_as_uninit, Error};
33
use core::mem::{size_of, MaybeUninit};
4+
use once_cell::race::OnceBool;
45

56
cfg_if! {
67
if #[cfg(target_arch = "x86_64")] {
@@ -94,8 +95,8 @@ fn is_rdrand_good() -> bool {
9495
}
9596

9697
pub fn getrandom_inner(dest: &mut [MaybeUninit<u8>]) -> Result<(), Error> {
97-
static RDRAND_GOOD: LazyBool = LazyBool::new();
98-
if !RDRAND_GOOD.unsync_init(is_rdrand_good) {
98+
static RDRAND_GOOD: OnceBool = OnceBool::new();
99+
if !RDRAND_GOOD.get_or_init(is_rdrand_good) {
99100
return Err(Error::NO_RDRAND);
100101
}
101102
// SAFETY: After this point, we know rdrand is supported.

tests/rdrand.rs

-2
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@
66
use getrandom::Error;
77
#[macro_use]
88
extern crate cfg_if;
9-
#[path = "../src/lazy.rs"]
10-
mod lazy;
119
#[path = "../src/rdrand.rs"]
1210
mod rdrand;
1311
#[path = "../src/util.rs"]

0 commit comments

Comments
 (0)