From c7ab9c3975ee8a195751c325abeff9c0cc706cc0 Mon Sep 17 00:00:00 2001 From: newpavlov Date: Fri, 2 Aug 2019 17:58:02 +0300 Subject: [PATCH] add dummy and wasm_dummy features --- Cargo.toml | 4 ++++ README.md | 9 +++++++-- src/lib.rs | 22 ++++++++++++++++++---- 3 files changed, 29 insertions(+), 6 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 42eecd68..b0a35de9 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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 = [] diff --git a/README.md b/README.md index 2efee205..eac625e3 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/src/lib.rs b/src/lib.rs index 338e806d..a964b9ce 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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 @@ -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. @@ -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\ + "); } }