Skip to content

Commit 0a3a2a0

Browse files
committed
Put stdweb dependency behind a target feature
1 parent b264f38 commit 0a3a2a0

File tree

7 files changed

+36
-11
lines changed

7 files changed

+36
-11
lines changed

.travis.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ matrix:
4747
# Cargo has errors with sub-commands so ignore updating for now:
4848
- cargo --list | egrep "^\s*web$" -q || cargo install cargo-web
4949
script:
50-
- cargo web test --target wasm32-unknown-unknown --nodejs
50+
- cargo web test --target wasm32-unknown-unknown --nodejs --features=wasm-stdweb
5151

5252
# Trust cross-built/emulated targets. We must repeat all non-default values.
5353
- rust: stable

CHANGELOG.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ You may also find the [Update Guide](UPDATING.md) useful.
1717
- Deprecate `rand_derive`. (#256)
1818
- Add `log` feature. Logging is now available in `JitterRng`, `OsRng`, `EntropyRng` and `ReseedingRng`. (#246)
1919
- Add `serde-1` feature for some PRNGs. (#189)
20+
- `wasm-stdweb` feature `OsRng` support on WASM via stdweb. (#272, #336)
2021

2122
### `Rng` trait
2223
- Split `Rng` in `RngCore` and `Rng` extension trait.
@@ -68,7 +69,7 @@ You may also find the [Update Guide](UPDATING.md) useful.
6869
### Platform support and `OsRng`
6970
- Add support for CloudABI. (#224)
7071
- Remove support for NaCl. (#225)
71-
- Replace stubs with proper WASM support in `OsRng`. (#272)
72+
- WASM support for `OsRng` via stdweb, behind the `wasm-stdweb` feature. (#272, #336)
7273
- On systems that do not have a syscall interface, only keep a single file descriptor open for `OsRng`. (#239)
7374
- Better error handling and reporting in `OsRng` (using new error type). (#225)
7475
- `OsRng` now uses non-blocking when available. (#225)

Cargo.toml

+4-5
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ alloc = ["rand_core/alloc"] # enables Vec and Box support without std
2323
i128_support = [] # enables i128 and u128 support
2424

2525
serde-1 = ["serde", "serde_derive"]
26+
wasm-stdweb = ["stdweb"]
2627

2728

2829
[target.'cfg(unix)'.dependencies]
@@ -35,9 +36,10 @@ winapi = { version = "0.3", features = ["minwindef", "ntsecapi", "profileapi", "
3536
rand_core = { path = 'rand_core', default-features = false }
3637

3738
log = { version = "0.4", optional = true }
38-
39-
serde = {version="1",optional=true}
39+
serde = {version="1", optional=true}
4040
serde_derive = {version="1", optional=true}
41+
stdweb = {version="0.4", optional=true}
42+
4143

4244
[workspace]
4345
members = ["rand_core"]
@@ -53,6 +55,3 @@ cloudabi = "0.0.3"
5355

5456
[target.'cfg(target_os = "fuchsia")'.dependencies]
5557
fuchsia-zircon = "0.3.2"
56-
57-
[target.wasm32-unknown-unknown.dependencies]
58-
stdweb = "0.4"

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ optional features are available:
8888
- `log` enables some logging via the `log` crate
8989
- `nightly` enables all unstable features (`i128_support`)
9090
- `serde-1` enables serialisation for some types, via Serde version 1
91+
- `wasm-stdweb` enables support for `OsRng` on WASM via stdweb.
9192
- `std` enabled by default; by setting "default-features = false" `no_std`
9293
mode is activated; this removes features depending on `std` functionality:
9394
- `OsRng` is entirely unavailable

src/lib.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@
179179
#![cfg_attr(not(feature="std"), no_std)]
180180
#![cfg_attr(all(feature="alloc", not(feature="std")), feature(alloc))]
181181
#![cfg_attr(feature = "i128_support", feature(i128_type, i128))]
182-
#![cfg_attr(all(target_arch = "wasm32", not(target_os = "emscripten")), recursion_limit="128")]
182+
#![cfg_attr(feature = "wasm-stdweb", recursion_limit="128")]
183183

184184
#[cfg(feature="std")] extern crate std as core;
185185
#[cfg(all(feature = "alloc", not(feature="std")))] extern crate alloc;
@@ -188,7 +188,7 @@
188188
#[cfg(feature="serde-1")] extern crate serde;
189189
#[cfg(feature="serde-1")] #[macro_use] extern crate serde_derive;
190190

191-
#[cfg(all(target_arch = "wasm32", not(target_os = "emscripten")))]
191+
#[cfg(feature = "wasm-stdweb")]
192192
#[macro_use]
193193
extern crate stdweb;
194194

src/os.rs

+25-1
Original file line numberDiff line numberDiff line change
@@ -613,7 +613,31 @@ mod imp {
613613
}
614614
}
615615

616-
#[cfg(all(target_arch = "wasm32", not(target_os = "emscripten")))]
616+
#[cfg(all(target_arch = "wasm32",
617+
not(target_os = "emscripten"),
618+
not(feature = "wasm-stdweb")))]
619+
mod imp {
620+
use {Error, ErrorKind};
621+
622+
#[derive(Debug)]
623+
pub struct OsRng;
624+
625+
impl OsRng {
626+
pub fn new() -> Result<OsRng, Error> {
627+
Err(Error::new(ErrorKind::Unavailable,
628+
"not supported on WASM without stdweb"))
629+
}
630+
631+
pub fn try_fill_bytes(&mut self, _v: &mut [u8]) -> Result<(), Error> {
632+
Err(Error::new(ErrorKind::Unavailable,
633+
"not supported on WASM without stdweb"))
634+
}
635+
}
636+
}
637+
638+
#[cfg(all(target_arch = "wasm32",
639+
not(target_os = "emscripten"),
640+
feature = "wasm-stdweb"))]
617641
mod imp {
618642
use std::mem;
619643
use stdweb::unstable::TryInto;

src/thread_rng.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ mod test {
175175

176176
#[test]
177177
#[cfg(feature="std")]
178-
#[cfg(not(all(target_arch = "wasm32", not(target_os = "emscripten"))))]
178+
#[cfg(not(feature="wasm-stdweb"))]
179179
fn test_thread_rng() {
180180
let mut r = ::thread_rng();
181181
r.gen::<i32>();

0 commit comments

Comments
 (0)