|
37 | 37 | //! sizes. |
38 | 38 | //! |
39 | 39 | //! ## Unsupported targets |
| 40 | +//! |
40 | 41 | //! By default, compiling `getrandom` for an unsupported target will result in |
41 | 42 | //! a compilation error. If you want to build an application which uses `getrandom` |
42 | 43 | //! for such target, you can either: |
|
50 | 51 | //! |
51 | 52 | //! ## Support for WebAssembly and asm.js |
52 | 53 | //! |
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. |
64 | 60 | //! |
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. |
67 | 67 | //! |
| 68 | +//! [bindgen]: https://github.com/rust-lang/rust-bindgen |
| 69 | +//! [stdweb]: https://github.com/koute/stdweb |
68 | 70 | //! |
69 | 71 | //! ## Early boot |
70 | 72 | //! |
@@ -236,13 +238,18 @@ cfg_if! { |
236 | 238 | target_env = "sgx", |
237 | 239 | )))] { |
238 | 240 | #[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 | + } |
246 | 253 | } else if #[cfg(feature = "dummy")] { |
247 | 254 | #[path = "dummy.rs"] mod imp; |
248 | 255 | } else { |
|
0 commit comments