Skip to content

Commit 752f8d2

Browse files
committed
adjust to no_std environments
1 parent d102c36 commit 752f8d2

File tree

3 files changed

+37
-7
lines changed

3 files changed

+37
-7
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ js-sys = { version = "0.3", optional = true }
3030
wasm-bindgen-test = "0.3.18"
3131

3232
[features]
33+
default = ["std"]
3334
# Implement std-only traits for getrandom::Error
3435
std = []
3536
# Feature to enable fallback RDRAND-based implementation on x86/x86_64

src/js.rs

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
//! Implementation for WASM based on Web and Node.js
22
use crate::Error;
33

4-
extern crate std;
5-
use std::{mem::MaybeUninit, thread_local};
4+
use core::mem::MaybeUninit;
5+
6+
#[cfg(not(std))]
7+
use core::cell::RefCell;
68

79
use js_sys::{global, Function, Uint8Array};
810
use wasm_bindgen::{prelude::wasm_bindgen, JsCast, JsValue};
@@ -20,14 +22,17 @@ enum RngSource {
2022

2123
// JsValues are always per-thread, so we initialize RngSource for each thread.
2224
// See: https://github.com/rustwasm/wasm-bindgen/pull/955
23-
thread_local!(
25+
#[cfg(std)]
26+
std::thread_local!(
2427
static RNG_SOURCE: Result<RngSource, Error> = getrandom_init();
2528
);
2629

27-
pub(crate) fn getrandom_inner(dest: &mut [MaybeUninit<u8>]) -> Result<(), Error> {
28-
RNG_SOURCE.with(|result| {
29-
let source = result.as_ref().map_err(|&e| e)?;
30+
#[cfg(not(std))]
31+
#[thread_local]
32+
static RNG_SOURCE: RefCell<Option<RngSource>> = RefCell::new(None);
3033

34+
pub(crate) fn getrandom_inner(dest: &mut [MaybeUninit<u8>]) -> Result<(), Error> {
35+
let mut getrandom_impl = |source: &RngSource| {
3136
match source {
3237
RngSource::Node(n) => {
3338
for chunk in dest.chunks_mut(NODE_MAX_BUFFER_SIZE) {
@@ -64,7 +69,24 @@ pub(crate) fn getrandom_inner(dest: &mut [MaybeUninit<u8>]) -> Result<(), Error>
6469
}
6570
}
6671
};
72+
6773
Ok(())
74+
};
75+
76+
#[cfg(not(std))]
77+
{
78+
if RNG_SOURCE.borrow().is_none() {
79+
let rng_source = getrandom_init()?;
80+
*RNG_SOURCE.borrow_mut() = Some(rng_source);
81+
}
82+
83+
let binding = RNG_SOURCE.borrow();
84+
getrandom_impl(binding.as_ref().expect("initialized above"))
85+
}
86+
87+
#[cfg(std)]
88+
RNG_SOURCE.with(|result| {
89+
getrandom_impl(result.as_ref().map_err(|&e| e)?)
6890
})
6991
}
7092

src/lib.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,9 +188,16 @@
188188
html_favicon_url = "https://www.rust-lang.org/favicon.ico",
189189
html_root_url = "https://docs.rs/getrandom/0.2.12"
190190
)]
191-
#![no_std]
191+
#![cfg_attr(not(std), no_std)]
192192
#![warn(rust_2018_idioms, unused_lifetimes, missing_docs)]
193193
#![cfg_attr(docsrs, feature(doc_auto_cfg))]
194+
#![cfg_attr(
195+
all(not(std),
196+
feature = "js",
197+
any(target_arch = "wasm32", target_arch = "wasm64"),
198+
target_os = "unknown"),
199+
feature(thread_local)
200+
)]
194201

195202
#[macro_use]
196203
extern crate cfg_if;

0 commit comments

Comments
 (0)