Skip to content

Commit

Permalink
Put stdweb dependency behind a target feature
Browse files Browse the repository at this point in the history
  • Loading branch information
pitdicker committed Mar 25, 2018
1 parent b264f38 commit e3be669
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 11 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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=wasm-stdweb

# Trust cross-built/emulated targets. We must repeat all non-default values.
- rust: stable
Expand Down
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
- `wasm-stdweb` feature for `OsRng` support on WASM via stdweb. (#272, #336)

### `Rng` trait
- Split `Rng` in `RngCore` and `Rng` extension trait.
Expand Down Expand Up @@ -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 `wasm-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)
Expand Down
9 changes: 4 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ alloc = ["rand_core/alloc"] # enables Vec and Box support without std
i128_support = [] # enables i128 and u128 support

serde-1 = ["serde", "serde_derive"]
wasm-stdweb = ["stdweb"]


[target.'cfg(unix)'.dependencies]
Expand All @@ -35,9 +36,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"]
Expand All @@ -53,6 +55,3 @@ cloudabi = "0.0.3"

[target.'cfg(target_os = "fuchsia")'.dependencies]
fuchsia-zircon = "0.3.2"

[target.wasm32-unknown-unknown.dependencies]
stdweb = "0.4"
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,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
- `wasm-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
Expand Down
4 changes: 2 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "wasm-stdweb", recursion_limit="128")]

#[cfg(feature="std")] extern crate std as core;
#[cfg(all(feature = "alloc", not(feature="std")))] extern crate alloc;
Expand All @@ -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 = "wasm-stdweb")]
#[macro_use]
extern crate stdweb;

Expand Down
26 changes: 25 additions & 1 deletion src/os.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "wasm-stdweb")))]
mod imp {
use {Error, ErrorKind};

#[derive(Debug)]
pub struct OsRng;

impl OsRng {
pub fn new() -> Result<OsRng, Error> {
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 = "wasm-stdweb"))]
mod imp {
use std::mem;
use stdweb::unstable::TryInto;
Expand Down
2 changes: 1 addition & 1 deletion src/thread_rng.rs
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ mod test {

#[test]
#[cfg(feature="std")]
#[cfg(not(all(target_arch = "wasm32", not(target_os = "emscripten"))))]
#[cfg(not(feature="wasm-stdweb"))]
fn test_thread_rng() {
let mut r = ::thread_rng();
r.gen::<i32>();
Expand Down

0 comments on commit e3be669

Please sign in to comment.