diff --git a/Cargo.toml b/Cargo.toml index 5ace3432..ef488a07 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -14,12 +14,6 @@ exclude = ["utils/*", ".*", "appveyor.yml"] travis-ci = { repository = "rust-random/getrandom" } appveyor = { repository = "rust-random/getrandom" } -[workspace] -members = [ - "custom/stdweb", - "custom/wasm-bindgen", -] - [dependencies] cfg-if = "0.1.2" @@ -33,10 +27,19 @@ libc = { version = "0.2.64", default-features = false } [target.'cfg(target_os = "wasi")'.dependencies] wasi = "0.9" +[target.'cfg(all(target_arch = "wasm32", target_os = "unknown", cargo_web))'.dependencies] +stdweb = { version = "0.4.18", default-features = false, optional = true } +[target.'cfg(all(target_arch = "wasm32", target_os = "unknown", not(cargo_web)))'.dependencies] +wasm-bindgen = { version = "0.2.62", default-features = false, optional = true } +[target.'cfg(all(target_arch = "wasm32", target_os = "unknown", not(cargo_web)))'.dev-dependencies] +wasm-bindgen-test = "0.3.12" + [features] std = [] # Feature to enable fallback RDRAND-based implementation rdrand = [] +# Feature to enable JavaScript bindings on wasm32-unknown-unknown +js = ["stdweb", "wasm-bindgen"] # Feature to enable custom RNG implementations custom = [] # Unstable feature to support being a libstd dependency diff --git a/custom/stdweb/Cargo.toml b/custom/stdweb/Cargo.toml deleted file mode 100644 index 67e44b99..00000000 --- a/custom/stdweb/Cargo.toml +++ /dev/null @@ -1,17 +0,0 @@ -[package] -name = "stdweb-getrandom" -version = "0.1.0" -edition = "2018" -authors = ["The Rand Project Developers"] -license = "MIT OR Apache-2.0" -description = "Custom shim for using getrandom with stdweb" -documentation = "https://docs.rs/stdweb-getrandom" -repository = "https://github.com/rust-random/getrandom" -categories = ["wasm"] - -[dependencies] -getrandom = { path = "../..", version = "0.2", features = ["custom"] } -stdweb = "0.4.18" - -[package.metadata.docs.rs] -default-target = "wasm32-unknown-unknown" diff --git a/custom/stdweb/tests/test.rs b/custom/stdweb/tests/test.rs deleted file mode 100644 index 744da522..00000000 --- a/custom/stdweb/tests/test.rs +++ /dev/null @@ -1,7 +0,0 @@ -// Explicitly use the Custom RNG crate to link it in. -use stdweb_getrandom as _; - -use getrandom::getrandom; -use test; -#[path = "../../../src/test_common.rs"] -mod test_common; diff --git a/custom/wasm-bindgen/Cargo.toml b/custom/wasm-bindgen/Cargo.toml deleted file mode 100644 index 2a665928..00000000 --- a/custom/wasm-bindgen/Cargo.toml +++ /dev/null @@ -1,20 +0,0 @@ -[package] -name = "wasm-bindgen-getrandom" -version = "0.1.0" -edition = "2018" -authors = ["The Rand Project Developers"] -license = "MIT OR Apache-2.0" -description = "Custom shim for using getrandom with wasm-bindgen" -documentation = "https://docs.rs/wasm-bindgen-getrandom" -repository = "https://github.com/rust-random/getrandom" -categories = ["wasm"] - -[dependencies] -getrandom = { path = "../..", version = "0.2", features = ["custom"] } -wasm-bindgen = "0.2.46" - -[dev-dependencies] -wasm-bindgen-test = "0.2.46" - -[package.metadata.docs.rs] -default-target = "wasm32-unknown-unknown" diff --git a/custom/wasm-bindgen/tests/node.rs b/custom/wasm-bindgen/tests/node.rs deleted file mode 100644 index 0861401c..00000000 --- a/custom/wasm-bindgen/tests/node.rs +++ /dev/null @@ -1,7 +0,0 @@ -// Explicitly use the Custom RNG crate to link it in. -use wasm_bindgen_getrandom as _; - -use getrandom::getrandom; -use wasm_bindgen_test::wasm_bindgen_test as test; -#[path = "../../../src/test_common.rs"] -mod test_common; diff --git a/custom/wasm-bindgen/tests/web.rs b/custom/wasm-bindgen/tests/web.rs deleted file mode 100644 index f09ddca2..00000000 --- a/custom/wasm-bindgen/tests/web.rs +++ /dev/null @@ -1,9 +0,0 @@ -// Explicitly use the Custom RNG crate to link it in. -use wasm_bindgen_getrandom as _; - -wasm_bindgen_test::wasm_bindgen_test_configure!(run_in_browser); - -use getrandom::getrandom; -use wasm_bindgen_test::wasm_bindgen_test as test; -#[path = "../../../src/test_common.rs"] -mod test_common; diff --git a/src/lib.rs b/src/lib.rs index b988445c..2cfc20d8 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -186,6 +186,11 @@ cfg_if! { } else if #[cfg(all(feature = "rdrand", any(target_arch = "x86_64", target_arch = "x86")))] { #[path = "rdrand.rs"] mod imp; + } else if #[cfg(all(feature = "js", + target_arch = "wasm32", target_os = "unknown"))] { + #[cfg_attr(cargo_web, path = "stdweb.rs")] + #[cfg_attr(not(cargo_web), path = "wasm-bindgen.rs")] + mod imp; } else if #[cfg(feature = "custom")] { use custom as imp; } else { diff --git a/custom/stdweb/src/lib.rs b/src/stdweb.rs similarity index 87% rename from custom/stdweb/src/lib.rs rename to src/stdweb.rs index a2b8eef4..5a50b68d 100644 --- a/custom/stdweb/src/lib.rs +++ b/src/stdweb.rs @@ -5,17 +5,13 @@ // , at your // option. This file may not be copied, modified, or distributed // except according to those terms. +use crate::Error; -#![recursion_limit = "128"] -#[cfg(not(all(target_arch = "wasm32", target_os = "unknown")))] -compile_error!("This crate is only for the `wasm32-unknown-unknown` target"); - +extern crate std; use std::thread_local; use stdweb::js; -use getrandom::{register_custom_getrandom, Error}; - #[derive(Clone, Copy, PartialEq)] enum RngSource { Browser, @@ -26,9 +22,7 @@ thread_local!( static RNG_SOURCE: Result = getrandom_init(); ); -register_custom_getrandom!(getrandom_inner); - -fn getrandom_inner(dest: &mut [u8]) -> Result<(), Error> { +pub(crate) fn getrandom_inner(dest: &mut [u8]) -> Result<(), Error> { RNG_SOURCE.with(|&source| getrandom_fill(source?, dest)) } diff --git a/custom/wasm-bindgen/src/lib.rs b/src/wasm-bindgen.rs similarity index 91% rename from custom/wasm-bindgen/src/lib.rs rename to src/wasm-bindgen.rs index 28cac6ba..3ec62f90 100644 --- a/custom/wasm-bindgen/src/lib.rs +++ b/src/wasm-bindgen.rs @@ -5,15 +5,13 @@ // , at your // option. This file may not be copied, modified, or distributed // except according to those terms. +use crate::Error; -#[cfg(not(all(target_arch = "wasm32", target_os = "unknown")))] -compile_error!("This crate is only for the `wasm32-unknown-unknown` target"); - +extern crate std; use std::thread_local; use wasm_bindgen::prelude::*; -use getrandom::{register_custom_getrandom, Error}; enum RngSource { Node(NodeCrypto), @@ -26,9 +24,7 @@ thread_local!( static RNG_SOURCE: Result = getrandom_init(); ); -register_custom_getrandom!(getrandom_inner); - -fn getrandom_inner(dest: &mut [u8]) -> Result<(), Error> { +pub(crate) fn getrandom_inner(dest: &mut [u8]) -> Result<(), Error> { RNG_SOURCE.with(|result| { let source = result.as_ref().map_err(|&e| e)?;