diff --git a/.travis.yml b/.travis.yml index ff4eca5336f..988b2393072 100644 --- a/.travis.yml +++ b/.travis.yml @@ -47,7 +47,7 @@ matrix: # Cargo has errors with sub-commands so ignore updating for now: - cargo --list | egrep "^\s*web$" -q || cargo install cargo-web script: - - cargo web test --target wasm32-unknown-unknown --nodejs + - cargo web test --target wasm32-unknown-unknown --nodejs --features=stdweb # Trust cross-built/emulated targets. We must repeat all non-default values. - rust: stable diff --git a/CHANGELOG.md b/CHANGELOG.md index 2c03338274d..8e473c892ad 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -17,6 +17,7 @@ You may also find the [Update Guide](UPDATING.md) useful. - Deprecate `rand_derive`. (#256) - Add `log` feature. Logging is now available in `JitterRng`, `OsRng`, `EntropyRng` and `ReseedingRng`. (#246) - Add `serde-1` feature for some PRNGs. (#189) +- `stdweb` feature for `OsRng` support on WASM via stdweb. (#272, #336) ### `Rng` trait - Split `Rng` in `RngCore` and `Rng` extension trait. @@ -68,7 +69,7 @@ You may also find the [Update Guide](UPDATING.md) useful. ### Platform support and `OsRng` - Add support for CloudABI. (#224) - Remove support for NaCl. (#225) -- Replace stubs with proper WASM support in `OsRng`. (#272) +- WASM support for `OsRng` via stdweb, behind the `stdweb` feature. (#272, #336) - On systems that do not have a syscall interface, only keep a single file descriptor open for `OsRng`. (#239) - Better error handling and reporting in `OsRng` (using new error type). (#225) - `OsRng` now uses non-blocking when available. (#225) diff --git a/Cargo.toml b/Cargo.toml index 93ff76037d7..ae39a57187d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -39,9 +39,10 @@ winapi = { version = "0.3", features = ["minwindef", "ntsecapi", "profileapi", " rand_core = { path = 'rand_core', default-features = false } log = { version = "0.4", optional = true } - -serde = {version="1",optional=true} +serde = {version="1", optional=true} serde_derive = {version="1", optional=true} +stdweb = {version="0.4", optional=true} + [workspace] members = ["rand_core"] @@ -57,6 +58,3 @@ cloudabi = "0.0.3" [target.'cfg(target_os = "fuchsia")'.dependencies] fuchsia-zircon = "0.3.2" - -[target.wasm32-unknown-unknown.dependencies] -stdweb = "0.4" diff --git a/README.md b/README.md index 311f8c3436e..09e91b99089 100644 --- a/README.md +++ b/README.md @@ -89,6 +89,7 @@ optional features are available: - `log` enables some logging via the `log` crate - `nightly` enables all unstable features (`i128_support`) - `serde-1` enables serialisation for some types, via Serde version 1 +- `stdweb` enables support for `OsRng` on WASM via stdweb. - `std` enabled by default; by setting "default-features = false" `no_std` mode is activated; this removes features depending on `std` functionality: - `OsRng` is entirely unavailable diff --git a/src/lib.rs b/src/lib.rs index 292e72b88e2..00907f0b9e1 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -179,7 +179,7 @@ #![cfg_attr(not(feature="std"), no_std)] #![cfg_attr(all(feature="alloc", not(feature="std")), feature(alloc))] #![cfg_attr(feature = "i128_support", feature(i128_type, i128))] -#![cfg_attr(all(target_arch = "wasm32", not(target_os = "emscripten")), recursion_limit="128")] +#![cfg_attr(feature = "stdweb", recursion_limit="128")] #[cfg(feature="std")] extern crate std as core; #[cfg(all(feature = "alloc", not(feature="std")))] extern crate alloc; @@ -188,7 +188,7 @@ #[cfg(feature="serde-1")] extern crate serde; #[cfg(feature="serde-1")] #[macro_use] extern crate serde_derive; -#[cfg(all(target_arch = "wasm32", not(target_os = "emscripten")))] +#[cfg(feature = "stdweb")] #[macro_use] extern crate stdweb; diff --git a/src/os.rs b/src/os.rs index 55269862341..7f59a975e37 100644 --- a/src/os.rs +++ b/src/os.rs @@ -613,7 +613,31 @@ mod imp { } } -#[cfg(all(target_arch = "wasm32", not(target_os = "emscripten")))] +#[cfg(all(target_arch = "wasm32", + not(target_os = "emscripten"), + not(feature = "stdweb")))] +mod imp { + use {Error, ErrorKind}; + + #[derive(Debug)] + pub struct OsRng; + + impl OsRng { + pub fn new() -> Result { + Err(Error::new(ErrorKind::Unavailable, + "not supported on WASM without stdweb")) + } + + pub fn try_fill_bytes(&mut self, _v: &mut [u8]) -> Result<(), Error> { + Err(Error::new(ErrorKind::Unavailable, + "not supported on WASM without stdweb")) + } + } +} + +#[cfg(all(target_arch = "wasm32", + not(target_os = "emscripten"), + feature = "stdweb"))] mod imp { use std::mem; use stdweb::unstable::TryInto; diff --git a/src/thread_rng.rs b/src/thread_rng.rs index 00e8604df5e..bc957a657ce 100644 --- a/src/thread_rng.rs +++ b/src/thread_rng.rs @@ -172,7 +172,7 @@ pub fn random() -> T where Uniform: Distribution { #[cfg(test)] mod test { #[test] - #[cfg(not(all(target_arch = "wasm32", not(target_os = "emscripten"))))] + #[cfg(not(feature="stdweb"))] fn test_thread_rng() { use Rng; let mut r = ::thread_rng();