Skip to content

Commit c7ab9c3

Browse files
committed
add dummy and wasm_dummy features
1 parent d93954a commit c7ab9c3

3 files changed

Lines changed: 29 additions & 6 deletions

File tree

Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,7 @@ stdweb = { version = "0.4.18", optional = true }
3030

3131
[features]
3232
std = []
33+
# enables dummy implementation for wasm32-unknown-unknown
34+
wasm_dummy = []
35+
# enables dummy implementation for unsupported targets
36+
dummy = []

README.md

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,17 @@ This library is `no_std` compatible, but uses `std` on most platforms.
4545
The `log` library is supported as an optional dependency. If enabled, error
4646
reporting will be improved on some platforms.
4747

48-
For WebAssembly (`wasm32`), WASI and Emscripten targets are supported directly; otherwise
49-
one of the following features must be enabled:
48+
For `wasm32-unknown-unknown` target one of the following features must be
49+
enabled:
5050

5151
- [`wasm-bindgen`](https://crates.io/crates/wasm_bindgen)
5252
- [`stdweb`](https://crates.io/crates/stdweb)
5353

54+
By default compiling crate for an unsupported platform will result in a
55+
compilation error. You may either replace this crate with a custom one, which
56+
will include support for your target, or enable `dummy` feature to use an
57+
always erroring dummy implementation.
58+
5459
## Minimum Supported Rust Version
5560

5661
This crate requires Rust 1.32.0 or later.

src/lib.rs

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
//! systems are using the recommended interface and respect maximum buffer
3737
//! sizes.
3838
//!
39-
//! ## Support for WebAssembly and ams.js
39+
//! ## Support for WebAssembly and asm.js
4040
//!
4141
//! The three Emscripten targets `asmjs-unknown-emscripten`,
4242
//! `wasm32-unknown-emscripten` and `wasm32-experimental-emscripten` use
@@ -46,7 +46,9 @@
4646
//! methods directly, using either `stdweb` or `wasm-bindgen` depending on what
4747
//! features are activated for this crate. Note that if both features are
4848
//! enabled `wasm-bindgen` will be used. If neither feature is enabled,
49-
//! `getrandom` will always fail.
49+
//! compiling `getrandom` will result in a compilation error. It can be disabled
50+
//! by enabling `wasm_dummy` feature, with which `getrandom` will use an always
51+
//! erroring dummy implementation.
5052
//!
5153
//! The WASI target `wasm32-wasi` uses the `__wasi_random_get` function defined
5254
//! by the WASI standard.
@@ -231,12 +233,24 @@ cfg_if! {
231233
#[path = "wasm32_bindgen.rs"] mod imp;
232234
} else if #[cfg(feature = "stdweb")] {
233235
#[path = "wasm32_stdweb.rs"] mod imp;
234-
} else {
236+
} else if #[cfg(any(feature = "wasm_dummy", feature = "dummy"))] {
235237
#[path = "dummy.rs"] mod imp;
238+
} else {
239+
compile_error!("\
240+
wasm32-unknown-unknown target requires one of the following
241+
features to be enabled: `stdweb`, `wasm-bindgen` or
242+
`dummy`/`wasm_dummy`\
243+
");
236244
}
237245
}
238-
} else {
246+
} else if #[cfg(feature = "dummy")] {
239247
#[path = "dummy.rs"] mod imp;
248+
} else {
249+
compile_error!("\
250+
target is not supported, you may enable dummy implementation \
251+
using `dummy` feature or replace `getrandom` crate with a custom \
252+
crate which supports your target\
253+
");
240254
}
241255
}
242256

0 commit comments

Comments
 (0)