Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix build for wasm32/64-unknown-unknown without std #395

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ js-sys = { version = "0.3", optional = true }
wasm-bindgen-test = "0.3.18"

[features]
default = ["std"]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a breaking change.

# Implement std-only traits for getrandom::Error
std = []
# Feature to enable fallback RDRAND-based implementation on x86/x86_64
Expand Down
34 changes: 27 additions & 7 deletions src/js.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
//! Implementation for WASM based on Web and Node.js
use crate::Error;

extern crate std;
use std::{mem::MaybeUninit, thread_local};
use core::mem::MaybeUninit;

#[cfg(not(std))]
use core::cell::RefCell;

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

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

pub(crate) fn getrandom_inner(dest: &mut [MaybeUninit<u8>]) -> Result<(), Error> {
RNG_SOURCE.with(|result| {
let source = result.as_ref().map_err(|&e| e)?;
#[cfg(not(std))]
#[thread_local]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#[thread_local] is unstable and even after stabilization will have a very high MSRV.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, you're right however if I'm not mistaken this is the only alternative for no_std envs

static RNG_SOURCE: RefCell<Option<RngSource>> = RefCell::new(None);

pub(crate) fn getrandom_inner(dest: &mut [MaybeUninit<u8>]) -> Result<(), Error> {
let mut getrandom_impl = |source: &RngSource| {
match source {
RngSource::Node(n) => {
for chunk in dest.chunks_mut(NODE_MAX_BUFFER_SIZE) {
Expand Down Expand Up @@ -64,8 +69,23 @@ pub(crate) fn getrandom_inner(dest: &mut [MaybeUninit<u8>]) -> Result<(), Error>
}
}
};

Ok(())
})
};

#[cfg(not(std))]
{
if RNG_SOURCE.borrow().is_none() {
let rng_source = getrandom_init()?;
*RNG_SOURCE.borrow_mut() = Some(rng_source);
}

let binding = RNG_SOURCE.borrow();
getrandom_impl(binding.as_ref().expect("initialized above"))
}

#[cfg(std)]
RNG_SOURCE.with(|result| getrandom_impl(result.as_ref().map_err(|&e| e)?))
}

fn getrandom_init() -> Result<RngSource, Error> {
Expand Down
11 changes: 10 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,9 +188,18 @@
html_favicon_url = "https://www.rust-lang.org/favicon.ico",
html_root_url = "https://docs.rs/getrandom/0.2.12"
)]
#![no_std]
#![cfg_attr(not(std), no_std)]
#![warn(rust_2018_idioms, unused_lifetimes, missing_docs)]
#![cfg_attr(docsrs, feature(doc_auto_cfg))]
#![cfg_attr(
all(
not(std),
feature = "js",
any(target_arch = "wasm32", target_arch = "wasm64"),
target_os = "unknown"
),
feature(thread_local)
)]

#[macro_use]
extern crate cfg_if;
Expand Down
Loading