Skip to content

Commit

Permalink
Merge pull request #686 from dhardy/master
Browse files Browse the repository at this point in the history
Fix #682
  • Loading branch information
dhardy committed Jan 8, 2019
2 parents afc9d9a + 1d07496 commit 4336232
Show file tree
Hide file tree
Showing 12 changed files with 112 additions and 229 deletions.
4 changes: 2 additions & 2 deletions .travis.yml
Expand Up @@ -149,8 +149,8 @@ matrix:
#- cargo web test --target wasm32-unknown-emscripten
#- cargo web test --nodejs --target wasm32-unknown-emscripten
#- cargo build --target wasm32-unknown-unknown # without any features
- cargo build --target wasm32-unknown-unknown --features=rand_os/wasm-bindgen
- cd rand_os && cargo web test --target wasm32-unknown-unknown --features=stdweb
- cargo build --target wasm32-unknown-unknown --features=wasm-bindgen
- cargo web test --target wasm32-unknown-unknown --features=stdweb

- rust: nightly
env: DESCRIPTION="cross-platform builder (doesn't run tests)"
Expand Down
5 changes: 5 additions & 0 deletions CHANGELOG.md
Expand Up @@ -9,6 +9,11 @@ A [separate changelog is kept for rand_core](rand_core/CHANGELOG.md).
You may also find the [Upgrade Guide](https://rust-random.github.io/book/update.html) useful.


## [0.6.4] - 2019-01-08
### Fixes
- Move wasm-bindgen shims to correct crate (#686)
- Make `wasm32-unknown-unknown` compile but fail at run-time if missing bindingsg (#686)

## [0.6.3] - 2019-01-04
### Fixes
- Make the `std` feature require the optional `rand_os` dependency (#675)
Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
@@ -1,6 +1,6 @@
[package]
name = "rand"
version = "0.6.3"
version = "0.6.4"
authors = ["The Rand Project Developers", "The Rust Project Developers"]
license = "MIT/Apache-2.0"
readme = "README.md"
Expand Down
11 changes: 9 additions & 2 deletions README.md
Expand Up @@ -74,8 +74,15 @@ pinned version of Rustc if you require compatibility with a specific version.

## Crate Features

Rand is built with only the `std` feature enabled by default. The following
optional features are available:
Rand is built with the `std` and `rand_os` features enabled by default:

- `std` enables functionality dependent on the `std` lib and implies `alloc`
and `rand_os`
- `rand_os` enables the `rand_os` crate, `rngs::OsRng` and enables its usage;
the continued existance of this feature is not guaranteed so users are
encouraged to specify `std` instead

The following optional features are available:

- `alloc` can be used instead of `std` to provide `Vec` and `Box`.
- `log` enables some logging via the `log` crate.
Expand Down
5 changes: 5 additions & 0 deletions rand_os/CHANGELOG.md
Expand Up @@ -4,5 +4,10 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).


## [0.1.1] - 2019-01-08
### Additions
- Add support for x86_64-fortanix-unknown-sgx target (#670)

## [0.1.0] - 2019-01-04
Initial release.
5 changes: 2 additions & 3 deletions rand_os/Cargo.toml
@@ -1,6 +1,6 @@
[package]
name = "rand_os"
version = "0.1.0"
version = "0.1.1"
authors = ["The Rand Project Developers"]
license = "MIT/Apache-2.0"
readme = "README.md"
Expand All @@ -21,9 +21,8 @@ log = { version = "0.4", optional = true }
[target.'cfg(unix)'.dependencies]
libc = "0.2"

# TODO: check if all features are required
[target.'cfg(windows)'.dependencies]
winapi = { version = "0.3", features = ["minwindef", "ntsecapi", "profileapi", "winnt"] }
winapi = { version = "0.3", features = ["minwindef", "ntsecapi", "winnt"] }

[target.'cfg(target_os = "cloudabi")'.dependencies]
cloudabi = "0.0.3"
Expand Down
81 changes: 78 additions & 3 deletions rand_os/src/lib.rs
Expand Up @@ -126,8 +126,8 @@
#![deny(missing_docs)]
#![deny(missing_debug_implementations)]
#![doc(test(attr(allow(unused_variables), deny(warnings))))]
// for stdweb
#![recursion_limit="128"]

#![cfg_attr(feature = "stdweb", recursion_limit="128")]

pub extern crate rand_core;
#[cfg(feature = "log")]
Expand Down Expand Up @@ -333,13 +333,33 @@ mod_use!(
wasm32_stdweb
);

/// Per #678 we use run-time failure where WASM bindings are missing
#[cfg(all(
target_arch = "wasm32",
not(target_os = "emscripten"),
not(feature = "wasm-bindgen"),
not(feature = "stdweb"),
))]
compile_error!("enable either wasm_bindgen or stdweb feature");
mod imp {
use rand_core::{Error, ErrorKind};
use super::OsRngImpl;

#[derive(Clone, Debug)]
pub struct OsRng;

impl OsRngImpl for OsRng {
fn new() -> Result<OsRng, Error> {
Err(Error::new(ErrorKind::Unavailable,
"OsRng: support for wasm32 requires emscripten, stdweb or wasm-bindgen"))
}

fn fill_chunk(&mut self, _dest: &mut [u8]) -> Result<(), Error> {
unimplemented!()
}

fn method_str(&self) -> &'static str { unimplemented!() }
}
}

#[cfg(not(any(
target_os = "android",
Expand All @@ -362,3 +382,58 @@ compile_error!("enable either wasm_bindgen or stdweb feature");
target_env = "sgx"
)))]
compile_error!("OS RNG support is not available for this platform");

// Due to rustwasm/wasm-bindgen#201 this can't be defined in the inner os
// modules, so hack around it for now and place it at the root.
#[cfg(all(feature = "wasm-bindgen", target_arch = "wasm32"))]
#[doc(hidden)]
#[allow(missing_debug_implementations)]
pub mod __wbg_shims {

// `extern { type Foo; }` isn't supported on 1.22 syntactically, so use a
// macro to work around that.
macro_rules! rust_122_compat {
($($t:tt)*) => ($($t)*)
}

rust_122_compat! {
extern crate wasm_bindgen;

pub use wasm_bindgen::prelude::*;

#[wasm_bindgen]
extern "C" {
pub type Function;
#[wasm_bindgen(constructor)]
pub fn new(s: &str) -> Function;
#[wasm_bindgen(method)]
pub fn call(this: &Function, self_: &JsValue) -> JsValue;

pub type This;
#[wasm_bindgen(method, getter, structural, js_name = self)]
pub fn self_(me: &This) -> JsValue;
#[wasm_bindgen(method, getter, structural)]
pub fn crypto(me: &This) -> JsValue;

#[derive(Clone, Debug)]
pub type BrowserCrypto;

// TODO: these `structural` annotations here ideally wouldn't be here to
// avoid a JS shim, but for now with feature detection they're
// unavoidable.
#[wasm_bindgen(method, js_name = getRandomValues, structural, getter)]
pub fn get_random_values_fn(me: &BrowserCrypto) -> JsValue;
#[wasm_bindgen(method, js_name = getRandomValues, structural)]
pub fn get_random_values(me: &BrowserCrypto, buf: &mut [u8]);

#[wasm_bindgen(js_name = require)]
pub fn node_require(s: &str) -> NodeCrypto;

#[derive(Clone, Debug)]
pub type NodeCrypto;

#[wasm_bindgen(method, js_name = randomFillSync, structural)]
pub fn random_fill_sync(me: &NodeCrypto, buf: &mut [u8]);
}
}
}
75 changes: 4 additions & 71 deletions src/deprecated.rs
Expand Up @@ -291,45 +291,12 @@ impl SeedableRng for StdRng {
impl CryptoRng for StdRng {}


#[cfg(all(feature="std",
any(target_os = "linux", target_os = "android",
target_os = "netbsd",
target_os = "dragonfly",
target_os = "haiku",
target_os = "emscripten",
target_os = "solaris",
target_os = "cloudabi",
target_os = "macos", target_os = "ios",
target_os = "freebsd",
target_os = "openbsd", target_os = "bitrig",
target_os = "redox",
target_os = "fuchsia",
windows,
all(target_arch = "wasm32", feature = "stdweb"),
all(target_arch = "wasm32", feature = "wasm-bindgen"),
)))]
#[cfg(feature="rand_os")]
#[derive(Clone, Debug)]
#[deprecated(since="0.6.0", note="import with rand::rngs::OsRng instead")]
pub struct OsRng(rngs::OsRng);

#[cfg(all(feature="std",
any(target_os = "linux", target_os = "android",
target_os = "netbsd",
target_os = "dragonfly",
target_os = "haiku",
target_os = "emscripten",
target_os = "solaris",
target_os = "cloudabi",
target_os = "macos", target_os = "ios",
target_os = "freebsd",
target_os = "openbsd", target_os = "bitrig",
target_os = "redox",
target_os = "fuchsia",
windows,
all(target_arch = "wasm32", feature = "stdweb"),
all(target_arch = "wasm32", feature = "wasm-bindgen"),
)))]
#[cfg(feature="std")]
#[cfg(feature="rand_os")]
impl RngCore for OsRng {
#[inline(always)]
fn next_u32(&mut self) -> u32 {
Expand All @@ -352,48 +319,14 @@ impl RngCore for OsRng {
}
}

#[cfg(all(feature="std",
any(target_os = "linux", target_os = "android",
target_os = "netbsd",
target_os = "dragonfly",
target_os = "haiku",
target_os = "emscripten",
target_os = "solaris",
target_os = "cloudabi",
target_os = "macos", target_os = "ios",
target_os = "freebsd",
target_os = "openbsd", target_os = "bitrig",
target_os = "redox",
target_os = "fuchsia",
windows,
all(target_arch = "wasm32", feature = "stdweb"),
all(target_arch = "wasm32", feature = "wasm-bindgen"),
)))]
#[cfg(feature="std")]
#[cfg(feature="rand_os")]
impl OsRng {
pub fn new() -> Result<Self, Error> {
rngs::OsRng::new().map(OsRng)
}
}

#[cfg(all(feature="std",
any(target_os = "linux", target_os = "android",
target_os = "netbsd",
target_os = "dragonfly",
target_os = "haiku",
target_os = "emscripten",
target_os = "solaris",
target_os = "cloudabi",
target_os = "macos", target_os = "ios",
target_os = "freebsd",
target_os = "openbsd", target_os = "bitrig",
target_os = "redox",
target_os = "fuchsia",
windows,
all(target_arch = "wasm32", feature = "stdweb"),
all(target_arch = "wasm32", feature = "wasm-bindgen"),
)))]
#[cfg(feature="std")]
#[cfg(feature="rand_os")]
impl CryptoRng for OsRng {}


Expand Down

0 comments on commit 4336232

Please sign in to comment.