Skip to content

Commit

Permalink
add dummy and wasm_dummy features
Browse files Browse the repository at this point in the history
  • Loading branch information
newpavlov committed Aug 2, 2019
1 parent d93954a commit c7ab9c3
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 6 deletions.
4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,7 @@ stdweb = { version = "0.4.18", optional = true }

[features]
std = []
# enables dummy implementation for wasm32-unknown-unknown
wasm_dummy = []
# enables dummy implementation for unsupported targets
dummy = []
9 changes: 7 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,17 @@ This library is `no_std` compatible, but uses `std` on most platforms.
The `log` library is supported as an optional dependency. If enabled, error
reporting will be improved on some platforms.

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

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

By default compiling crate for an unsupported platform will result in a
compilation error. You may either replace this crate with a custom one, which
will include support for your target, or enable `dummy` feature to use an
always erroring dummy implementation.

## Minimum Supported Rust Version

This crate requires Rust 1.32.0 or later.
Expand Down
22 changes: 18 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
//! systems are using the recommended interface and respect maximum buffer
//! sizes.
//!
//! ## Support for WebAssembly and ams.js
//! ## Support for WebAssembly and asm.js
//!
//! The three Emscripten targets `asmjs-unknown-emscripten`,
//! `wasm32-unknown-emscripten` and `wasm32-experimental-emscripten` use
Expand All @@ -46,7 +46,9 @@
//! methods directly, using either `stdweb` or `wasm-bindgen` depending on what
//! features are activated for this crate. Note that if both features are
//! enabled `wasm-bindgen` will be used. If neither feature is enabled,
//! `getrandom` will always fail.
//! compiling `getrandom` will result in a compilation error. It can be disabled
//! by enabling `wasm_dummy` feature, with which `getrandom` will use an always
//! erroring dummy implementation.
//!
//! The WASI target `wasm32-wasi` uses the `__wasi_random_get` function defined
//! by the WASI standard.
Expand Down Expand Up @@ -231,12 +233,24 @@ cfg_if! {
#[path = "wasm32_bindgen.rs"] mod imp;
} else if #[cfg(feature = "stdweb")] {
#[path = "wasm32_stdweb.rs"] mod imp;
} else {
} else if #[cfg(any(feature = "wasm_dummy", feature = "dummy"))] {
#[path = "dummy.rs"] mod imp;
} else {
compile_error!("\
wasm32-unknown-unknown target requires one of the following
features to be enabled: `stdweb`, `wasm-bindgen` or
`dummy`/`wasm_dummy`\
");
}
}
} else {
} else if #[cfg(feature = "dummy")] {
#[path = "dummy.rs"] mod imp;
} else {
compile_error!("\
target is not supported, you may enable dummy implementation \
using `dummy` feature or replace `getrandom` crate with a custom \
crate which supports your target\
");
}
}

Expand Down

0 comments on commit c7ab9c3

Please sign in to comment.