Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 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.

28 changes: 21 additions & 7 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, with which `getrandom` will use an always
Comment thread
newpavlov marked this conversation as resolved.
Outdated
//! 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,30 @@ cfg_if! {
target_env = "sgx",
)))] {
#[path = "rdrand.rs"] mod imp;
} else if #[cfg(target_arch = "wasm32")] {
// ideally we would like to use `target = "wasm32-unknown-unknown"`,
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Personally I find something like:

...
} 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!("\
...

to be a little more readable. It avoids nested cfg_if!s, and we don't need the unknown specifier. If we get here, the other wasm32 cases are taken care of.

In theory, a new wasm32 target could be added that's not addressed in the above, but having the stdweb/wasm-bindgen features work in that case seems fine (even ideal).

Copy link
Copy Markdown
Member Author

@newpavlov newpavlov Aug 5, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure about not needing the unknown specifier. Imagine that we don't have WASI or Emscripten and they will be added in future, with this config even if getrandom will work on them, I wouldn't call it a correct target support. But I guess since we can't use target_vendor here, we are not completely future proof anyway, so it makes sense to simplify the code.

// but unfortunately it does not work, see:
// https://github.com/rust-lang/rust/issues/63217
} else if #[cfg(all(
target_arch = "wasm32",
target_os = "unknown",
any(feature = "wasm-bindgen", feature = "stdweb"),
))] {
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;
#[path = "wasm32_stdweb.rs"] mod imp;
}
}
} else {
} 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