Skip to content

Commit 73bbbdc

Browse files
authored
Always build on wasm32-unknown-unknown (#90)
This updates the documentation to explain exactly what we are doing on this target. Also adds a test that the target builds without features.
1 parent 3d1d2ff commit 73bbbdc

File tree

2 files changed

+28
-20
lines changed

2 files changed

+28
-20
lines changed

.travis.yml

+1
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ matrix:
5050
#- cargo web test --nodejs --target wasm32-unknown-emscripten
5151
#- cargo build --target wasm32-unknown-unknown # without any features
5252
- cargo build --target wasm32-wasi
53+
- cargo build --target wasm32-unknown-unknown
5354
- cargo build --target wasm32-unknown-unknown --features=wasm-bindgen
5455
- cargo web test --target wasm32-unknown-unknown --features=stdweb
5556
- cargo build --manifest-path tests/wasm_bindgen/Cargo.toml --target wasm32-unknown-unknown

src/lib.rs

+27-20
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
//! sizes.
3838
//!
3939
//! ## Unsupported targets
40+
//!
4041
//! By default, compiling `getrandom` for an unsupported target will result in
4142
//! a compilation error. If you want to build an application which uses `getrandom`
4243
//! for such target, you can either:
@@ -50,21 +51,22 @@
5051
//!
5152
//! ## Support for WebAssembly and asm.js
5253
//!
53-
//! The three Emscripten targets `asmjs-unknown-emscripten`,
54-
//! `wasm32-unknown-emscripten` and `wasm32-experimental-emscripten` use
55-
//! Emscripten's emulation of `/dev/random` on web browsers and Node.js.
56-
//!
57-
//! The bare WASM target `wasm32-unknown-unknown` tries to call the javascript
58-
//! methods directly, using either `stdweb` or `wasm-bindgen` depending on what
59-
//! features are activated for this crate. Note that if both features are
60-
//! enabled `wasm-bindgen` will be used. If neither feature is enabled,
61-
//! compiling `getrandom` will result in a compilation error. It can be avoided
62-
//! by enabling the `dummy` feature, which will make `getrandom` to use an
63-
//! always failing implementation.
54+
//! Getrandom supports all of Rust's current `wasm32` targets, and it works with
55+
//! both Node.js and web browsers. The three Emscripten targets
56+
//! `asmjs-unknown-emscripten`, `wasm32-unknown-emscripten`, and
57+
//! `wasm32-experimental-emscripten` use Emscripten's `/dev/random` emulation.
58+
//! The WASI target `wasm32-wasi` uses the [`__wasi_random_get`][17] function
59+
//! defined by the WASI standard.
6460
//!
65-
//! The WASI target `wasm32-wasi` uses the `__wasi_random_get` function defined
66-
//! by the WASI standard.
61+
//! Getrandom also supports `wasm32-unknown-unknown` by directly calling
62+
//! JavaScript methods. Rust currently has two ways to do this: [bindgen] and
63+
//! [stdweb]. Getrandom supports using either one by enabling the
64+
//! `wasm-bindgen` or `stdweb` crate features. Note that if both features are
65+
//! enabled, `wasm-bindgen` will be used. If neither feature is enabled, calls
66+
//! to `getrandom` will always fail at runtime.
6767
//!
68+
//! [bindgen]: https://github.com/rust-lang/rust-bindgen
69+
//! [stdweb]: https://github.com/koute/stdweb
6870
//!
6971
//! ## Early boot
7072
//!
@@ -236,13 +238,18 @@ cfg_if! {
236238
target_env = "sgx",
237239
)))] {
238240
#[path = "rdrand.rs"] mod imp;
239-
// the following two branches are intended only for `wasm32-unknown-unknown`
240-
// target and may not work or work inefficiently on targets which may be
241-
// added in future
242-
} else if #[cfg(all(target_arch = "wasm32", feature = "wasm-bindgen"))] {
243-
#[path = "wasm32_bindgen.rs"] mod imp;
244-
} else if #[cfg(all(target_arch = "wasm32", feature = "stdweb"))] {
245-
#[path = "wasm32_stdweb.rs"] mod imp;
241+
} else if #[cfg(all(target_arch = "wasm32", target_os = "unknown"))] {
242+
cfg_if! {
243+
if #[cfg(feature = "wasm-bindgen")] {
244+
#[path = "wasm32_bindgen.rs"] mod imp;
245+
} else if #[cfg(feature = "stdweb")] {
246+
#[path = "wasm32_stdweb.rs"] mod imp;
247+
} else {
248+
// Always have an implementation for wasm32-unknown-unknown.
249+
// See https://github.com/rust-random/getrandom/issues/87
250+
#[path = "dummy.rs"] mod imp;
251+
}
252+
}
246253
} else if #[cfg(feature = "dummy")] {
247254
#[path = "dummy.rs"] mod imp;
248255
} else {

0 commit comments

Comments
 (0)