Skip to content

Commit

Permalink
Merge pull request #336 from pitdicker/stdweb_feature
Browse files Browse the repository at this point in the history
Put stdweb dependency behind a target feature
  • Loading branch information
pitdicker authored Mar 26, 2018
2 parents 1980163 + 157bf70 commit 43277d6
Show file tree
Hide file tree
Showing 7 changed files with 35 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=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)
- `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 `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
8 changes: 3 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand All @@ -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"
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
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 = "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 = "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 = "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 = "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 @@ -172,7 +172,7 @@ pub fn random<T>() -> T where Uniform: Distribution<T> {
#[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();
Expand Down

0 comments on commit 43277d6

Please sign in to comment.