Skip to content

Move wasm32 Custom RNG impls back to main crate #149

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

Merged
merged 2 commits into from
May 29, 2020
Merged
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
13 changes: 5 additions & 8 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -67,17 +67,14 @@ jobs:
script:
- cargo test --target wasm32-wasi
# stdweb (wasm32-unknown-unknown) tests (Node, Chrome)
- cargo web test --package stdweb-getrandom
- cargo web test --package stdweb-getrandom --nodejs
- cargo web test --features js --nodejs
- cargo web test --features js
# wasm-bindgen (wasm32-unknown-unknown) tests (Node, Firefox, Chrome)
- cargo test --package wasm-bindgen-getrandom
--target wasm32-unknown-unknown --test node
- cargo test --target wasm32-unknown-unknown --features js
- GECKODRIVER=$HOME/geckodriver
cargo test --package wasm-bindgen-getrandom
--target wasm32-unknown-unknown --test web
cargo test --target wasm32-unknown-unknown --features js,test-in-browser
- CHROMEDRIVER=$HOME/chromedriver
cargo test --package wasm-bindgen-getrandom
--target wasm32-unknown-unknown --test web
cargo test --target wasm32-unknown-unknown --features js,test-in-browser

- name: "WASM via Emscripten"
rust: stable
Expand Down
18 changes: 12 additions & 6 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand All @@ -33,14 +27,26 @@ 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
rustc-dep-of-std = ["compiler_builtins", "core"]

# Test/wasm-bindgen only feature to run tests in a browser
test-in-browser = []

[package.metadata.docs.rs]
features = ["std", "custom"]
17 changes: 0 additions & 17 deletions custom/stdweb/Cargo.toml

This file was deleted.

7 changes: 0 additions & 7 deletions custom/stdweb/tests/test.rs

This file was deleted.

20 changes: 0 additions & 20 deletions custom/wasm-bindgen/Cargo.toml

This file was deleted.

7 changes: 0 additions & 7 deletions custom/wasm-bindgen/tests/node.rs

This file was deleted.

9 changes: 0 additions & 9 deletions custom/wasm-bindgen/tests/web.rs

This file was deleted.

5 changes: 5 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
12 changes: 3 additions & 9 deletions custom/stdweb/src/lib.rs → src/stdweb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,13 @@
// <LICENSE-MIT or https://opensource.org/licenses/MIT>, 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,
Expand All @@ -26,9 +22,7 @@ thread_local!(
static RNG_SOURCE: Result<RngSource, Error> = 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))
}

Expand Down
10 changes: 7 additions & 3 deletions src/test_common.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
// Both getrandom and test can be renamed by the parent module.
// Allow getrandom to be renamed by the parent module.
use super::getrandom;
#[cfg(all(target_arch = "wasm32", target_os = "unknown"))]
use super::test;

#[cfg(all(target_arch = "wasm32", target_os = "unknown", not(cargo_web)))]
use wasm_bindgen_test::wasm_bindgen_test as test;

#[cfg(feature = "test-in-browser")]
wasm_bindgen_test::wasm_bindgen_test_configure!(run_in_browser);

#[test]
fn test_zero() {
Expand Down
10 changes: 3 additions & 7 deletions custom/wasm-bindgen/src/lib.rs → src/wasm-bindgen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,13 @@
// <LICENSE-MIT or https://opensource.org/licenses/MIT>, 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),
Expand All @@ -26,9 +24,7 @@ thread_local!(
static RNG_SOURCE: Result<RngSource, Error> = 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)?;

Expand Down