Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,5 @@ stdweb = { version = "0.4.18", optional = true }

[features]
std = []
# enables dummy implementation for unsupported targets
dummy = []
20 changes: 15 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ library like [`rand`].

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


## Usage

Add this to your `Cargo.toml`:
Expand All @@ -45,17 +44,29 @@ 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 the `wasm32-unknown-unknown` target, one of the following features should be
enabled:

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

By default, compiling `getrandom` for an unsupported target will result in
a compilation error. If you want to build an application which uses `getrandom`
for such target, you can either:
- Use [`[replace]`][replace] or [`[patch]`][patch] section in your `Cargo.toml`
to switch to a custom implementation with a support of your target.
- Enable the `dummy` feature to have getrandom use an implementation that always
fails on unsupported targets.
Comment thread
newpavlov marked this conversation as resolved.
Outdated

We also accept pull requests to support additional targets.
Comment thread
newpavlov marked this conversation as resolved.
Outdated

[replace]: https://doc.rust-lang.org/cargo/reference/manifest.html#the-replace-section
[patch]: https://doc.rust-lang.org/cargo/reference/manifest.html#the-patch-section

## Minimum Supported Rust Version

This crate requires Rust 1.32.0 or later.


# License

The `getrandom` library is distributed under either of
Expand All @@ -64,4 +75,3 @@ The `getrandom` library is distributed under either of
* [MIT license](LICENSE-MIT)

at your option.

32 changes: 19 additions & 13 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
Comment thread
newpavlov marked this conversation as resolved.
Outdated
//! by enabling the `dummy` feature, which will make `getrandom` to use an
//! always failing implementation.
//!
//! The WASI target `wasm32-wasi` uses the `__wasi_random_get` function defined
//! by the WASI standard.
Expand Down Expand Up @@ -225,18 +227,22 @@ cfg_if! {
target_env = "sgx",
)))] {
#[path = "rdrand.rs"] mod imp;
} else if #[cfg(target_arch = "wasm32")] {
cfg_if! {
if #[cfg(feature = "wasm-bindgen")] {
#[path = "wasm32_bindgen.rs"] mod imp;
} else if #[cfg(feature = "stdweb")] {
#[path = "wasm32_stdweb.rs"] mod imp;
} else {
#[path = "dummy.rs"] mod imp;
}
}
} else {
// the following two branches are intended only for `wasm32-unknown-unknown`
// target and may not work or work inefficiently on targets which may be
// added in future
} else if #[cfg(all(target_arch = "wasm32", feature = "wasm-bindgen"))] {
#[path = "wasm32_bindgen.rs"] mod imp;
} else if #[cfg(all(target_arch = "wasm32", feature = "stdweb"))] {
#[path = "wasm32_stdweb.rs"] mod imp;
} else if #[cfg(feature = "dummy")] {
#[path = "dummy.rs"] mod imp;
} else {
compile_error!("\
target is not supported, you may enable dummy implementation \
using the `dummy` feature or overwrite `getrandom` crate with \
a custom one which supports your target using `[replace]` or \
Comment thread
newpavlov marked this conversation as resolved.
Outdated
`[patch]` section in your `Cargo.toml`\
");
}
}

Expand Down