Skip to content

Commit 7449a08

Browse files
authored
Add dummy feature (#71)
1 parent 283f9f8 commit 7449a08

File tree

3 files changed

+44
-18
lines changed

3 files changed

+44
-18
lines changed

Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,5 +37,7 @@ stdweb = { version = "0.4.18", optional = true }
3737

3838
[features]
3939
std = []
40+
# enables dummy implementation for unsupported targets
41+
dummy = []
4042
# Unstable feature to support being a libstd dependancy
4143
rustc-dep-of-std = ["compiler_builtins", "core"]

README.md

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ library like [`rand`].
1818

1919
[`rand`]: https://crates.io/crates/rand
2020

21-
2221
## Usage
2322

2423
Add this to your `Cargo.toml`:
@@ -48,17 +47,27 @@ will require linking against system libraries (i.e. `libc` for Unix,
4847
The `log` library is supported as an optional dependency. If enabled, error
4948
reporting will be improved on some platforms.
5049

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

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

56+
By default, compiling `getrandom` for an unsupported target will result in
57+
a compilation error. If you want to build an application which uses `getrandom`
58+
for such target, you can either:
59+
- Use [`[replace]`][replace] or [`[patch]`][patch] section in your `Cargo.toml`
60+
to switch to a custom implementation with a support of your target.
61+
- Enable the `dummy` feature to have getrandom use an implementation that always
62+
fails at run-time on unsupported targets.
63+
64+
[replace]: https://doc.rust-lang.org/cargo/reference/manifest.html#the-replace-section
65+
[patch]: https://doc.rust-lang.org/cargo/reference/manifest.html#the-patch-section
66+
5767
## Minimum Supported Rust Version
5868

5969
This crate requires Rust 1.32.0 or later.
6070

61-
6271
# License
6372

6473
The `getrandom` library is distributed under either of
@@ -67,4 +76,3 @@ The `getrandom` library is distributed under either of
6776
* [MIT license](LICENSE-MIT)
6877

6978
at your option.
70-

src/lib.rs

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,19 @@
3636
//! systems are using the recommended interface and respect maximum buffer
3737
//! sizes.
3838
//!
39-
//! ## Support for WebAssembly and ams.js
39+
//! ## Unsupported targets
40+
//! By default, compiling `getrandom` for an unsupported target will result in
41+
//! a compilation error. If you want to build an application which uses `getrandom`
42+
//! for such target, you can either:
43+
//! - Use [`[replace]`][replace] or [`[patch]`][patch] section in your `Cargo.toml`
44+
//! to switch to a custom implementation with a support of your target.
45+
//! - Enable the `dummy` feature to have getrandom use an implementation that always
46+
//! fails at run-time on unsupported targets.
47+
//!
48+
//! [replace]: https://doc.rust-lang.org/cargo/reference/manifest.html#the-replace-section
49+
//! [patch]: https://doc.rust-lang.org/cargo/reference/manifest.html#the-patch-section
50+
//!
51+
//! ## Support for WebAssembly and asm.js
4052
//!
4153
//! The three Emscripten targets `asmjs-unknown-emscripten`,
4254
//! `wasm32-unknown-emscripten` and `wasm32-experimental-emscripten` use
@@ -46,7 +58,9 @@
4658
//! methods directly, using either `stdweb` or `wasm-bindgen` depending on what
4759
//! features are activated for this crate. Note that if both features are
4860
//! enabled `wasm-bindgen` will be used. If neither feature is enabled,
49-
//! `getrandom` will always fail.
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.
5064
//!
5165
//! The WASI target `wasm32-wasi` uses the `__wasi_random_get` function defined
5266
//! by the WASI standard.
@@ -221,18 +235,20 @@ cfg_if! {
221235
target_env = "sgx",
222236
)))] {
223237
#[path = "rdrand.rs"] mod imp;
224-
} else if #[cfg(target_arch = "wasm32")] {
225-
cfg_if! {
226-
if #[cfg(feature = "wasm-bindgen")] {
227-
#[path = "wasm32_bindgen.rs"] mod imp;
228-
} else if #[cfg(feature = "stdweb")] {
229-
#[path = "wasm32_stdweb.rs"] mod imp;
230-
} else {
231-
#[path = "dummy.rs"] mod imp;
232-
}
233-
}
234-
} else {
238+
// the following two branches are intended only for `wasm32-unknown-unknown`
239+
// target and may not work or work inefficiently on targets which may be
240+
// added in future
241+
} else if #[cfg(all(target_arch = "wasm32", feature = "wasm-bindgen"))] {
242+
#[path = "wasm32_bindgen.rs"] mod imp;
243+
} else if #[cfg(all(target_arch = "wasm32", feature = "stdweb"))] {
244+
#[path = "wasm32_stdweb.rs"] mod imp;
245+
} else if #[cfg(feature = "dummy")] {
235246
#[path = "dummy.rs"] mod imp;
247+
} else {
248+
compile_error!("\
249+
target is not supported, for more information see: \
250+
https://docs.rs/getrandom/#unsupported-targets\
251+
");
236252
}
237253
}
238254

0 commit comments

Comments
 (0)