Skip to content

Commit 4336232

Browse files
authored
Merge pull request #686 from dhardy/master
Fix #682
2 parents afc9d9a + 1d07496 commit 4336232

File tree

12 files changed

+112
-229
lines changed

12 files changed

+112
-229
lines changed

.travis.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -149,8 +149,8 @@ matrix:
149149
#- cargo web test --target wasm32-unknown-emscripten
150150
#- cargo web test --nodejs --target wasm32-unknown-emscripten
151151
#- cargo build --target wasm32-unknown-unknown # without any features
152-
- cargo build --target wasm32-unknown-unknown --features=rand_os/wasm-bindgen
153-
- cd rand_os && cargo web test --target wasm32-unknown-unknown --features=stdweb
152+
- cargo build --target wasm32-unknown-unknown --features=wasm-bindgen
153+
- cargo web test --target wasm32-unknown-unknown --features=stdweb
154154

155155
- rust: nightly
156156
env: DESCRIPTION="cross-platform builder (doesn't run tests)"

CHANGELOG.md

+5
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@ A [separate changelog is kept for rand_core](rand_core/CHANGELOG.md).
99
You may also find the [Upgrade Guide](https://rust-random.github.io/book/update.html) useful.
1010

1111

12+
## [0.6.4] - 2019-01-08
13+
### Fixes
14+
- Move wasm-bindgen shims to correct crate (#686)
15+
- Make `wasm32-unknown-unknown` compile but fail at run-time if missing bindingsg (#686)
16+
1217
## [0.6.3] - 2019-01-04
1318
### Fixes
1419
- Make the `std` feature require the optional `rand_os` dependency (#675)

Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "rand"
3-
version = "0.6.3"
3+
version = "0.6.4"
44
authors = ["The Rand Project Developers", "The Rust Project Developers"]
55
license = "MIT/Apache-2.0"
66
readme = "README.md"

README.md

+9-2
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,15 @@ pinned version of Rustc if you require compatibility with a specific version.
7474

7575
## Crate Features
7676

77-
Rand is built with only the `std` feature enabled by default. The following
78-
optional features are available:
77+
Rand is built with the `std` and `rand_os` features enabled by default:
78+
79+
- `std` enables functionality dependent on the `std` lib and implies `alloc`
80+
and `rand_os`
81+
- `rand_os` enables the `rand_os` crate, `rngs::OsRng` and enables its usage;
82+
the continued existance of this feature is not guaranteed so users are
83+
encouraged to specify `std` instead
84+
85+
The following optional features are available:
7986

8087
- `alloc` can be used instead of `std` to provide `Vec` and `Box`.
8188
- `log` enables some logging via the `log` crate.

rand_os/CHANGELOG.md

+5
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,10 @@ All notable changes to this project will be documented in this file.
44
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
55
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
66

7+
8+
## [0.1.1] - 2019-01-08
9+
### Additions
10+
- Add support for x86_64-fortanix-unknown-sgx target (#670)
11+
712
## [0.1.0] - 2019-01-04
813
Initial release.

rand_os/Cargo.toml

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "rand_os"
3-
version = "0.1.0"
3+
version = "0.1.1"
44
authors = ["The Rand Project Developers"]
55
license = "MIT/Apache-2.0"
66
readme = "README.md"
@@ -21,9 +21,8 @@ log = { version = "0.4", optional = true }
2121
[target.'cfg(unix)'.dependencies]
2222
libc = "0.2"
2323

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

2827
[target.'cfg(target_os = "cloudabi")'.dependencies]
2928
cloudabi = "0.0.3"

rand_os/src/lib.rs

+78-3
Original file line numberDiff line numberDiff line change
@@ -126,8 +126,8 @@
126126
#![deny(missing_docs)]
127127
#![deny(missing_debug_implementations)]
128128
#![doc(test(attr(allow(unused_variables), deny(warnings))))]
129-
// for stdweb
130-
#![recursion_limit="128"]
129+
130+
#![cfg_attr(feature = "stdweb", recursion_limit="128")]
131131

132132
pub extern crate rand_core;
133133
#[cfg(feature = "log")]
@@ -333,13 +333,33 @@ mod_use!(
333333
wasm32_stdweb
334334
);
335335

336+
/// Per #678 we use run-time failure where WASM bindings are missing
336337
#[cfg(all(
337338
target_arch = "wasm32",
338339
not(target_os = "emscripten"),
339340
not(feature = "wasm-bindgen"),
340341
not(feature = "stdweb"),
341342
))]
342-
compile_error!("enable either wasm_bindgen or stdweb feature");
343+
mod imp {
344+
use rand_core::{Error, ErrorKind};
345+
use super::OsRngImpl;
346+
347+
#[derive(Clone, Debug)]
348+
pub struct OsRng;
349+
350+
impl OsRngImpl for OsRng {
351+
fn new() -> Result<OsRng, Error> {
352+
Err(Error::new(ErrorKind::Unavailable,
353+
"OsRng: support for wasm32 requires emscripten, stdweb or wasm-bindgen"))
354+
}
355+
356+
fn fill_chunk(&mut self, _dest: &mut [u8]) -> Result<(), Error> {
357+
unimplemented!()
358+
}
359+
360+
fn method_str(&self) -> &'static str { unimplemented!() }
361+
}
362+
}
343363

344364
#[cfg(not(any(
345365
target_os = "android",
@@ -362,3 +382,58 @@ compile_error!("enable either wasm_bindgen or stdweb feature");
362382
target_env = "sgx"
363383
)))]
364384
compile_error!("OS RNG support is not available for this platform");
385+
386+
// Due to rustwasm/wasm-bindgen#201 this can't be defined in the inner os
387+
// modules, so hack around it for now and place it at the root.
388+
#[cfg(all(feature = "wasm-bindgen", target_arch = "wasm32"))]
389+
#[doc(hidden)]
390+
#[allow(missing_debug_implementations)]
391+
pub mod __wbg_shims {
392+
393+
// `extern { type Foo; }` isn't supported on 1.22 syntactically, so use a
394+
// macro to work around that.
395+
macro_rules! rust_122_compat {
396+
($($t:tt)*) => ($($t)*)
397+
}
398+
399+
rust_122_compat! {
400+
extern crate wasm_bindgen;
401+
402+
pub use wasm_bindgen::prelude::*;
403+
404+
#[wasm_bindgen]
405+
extern "C" {
406+
pub type Function;
407+
#[wasm_bindgen(constructor)]
408+
pub fn new(s: &str) -> Function;
409+
#[wasm_bindgen(method)]
410+
pub fn call(this: &Function, self_: &JsValue) -> JsValue;
411+
412+
pub type This;
413+
#[wasm_bindgen(method, getter, structural, js_name = self)]
414+
pub fn self_(me: &This) -> JsValue;
415+
#[wasm_bindgen(method, getter, structural)]
416+
pub fn crypto(me: &This) -> JsValue;
417+
418+
#[derive(Clone, Debug)]
419+
pub type BrowserCrypto;
420+
421+
// TODO: these `structural` annotations here ideally wouldn't be here to
422+
// avoid a JS shim, but for now with feature detection they're
423+
// unavoidable.
424+
#[wasm_bindgen(method, js_name = getRandomValues, structural, getter)]
425+
pub fn get_random_values_fn(me: &BrowserCrypto) -> JsValue;
426+
#[wasm_bindgen(method, js_name = getRandomValues, structural)]
427+
pub fn get_random_values(me: &BrowserCrypto, buf: &mut [u8]);
428+
429+
#[wasm_bindgen(js_name = require)]
430+
pub fn node_require(s: &str) -> NodeCrypto;
431+
432+
#[derive(Clone, Debug)]
433+
pub type NodeCrypto;
434+
435+
#[wasm_bindgen(method, js_name = randomFillSync, structural)]
436+
pub fn random_fill_sync(me: &NodeCrypto, buf: &mut [u8]);
437+
}
438+
}
439+
}

src/deprecated.rs

+4-71
Original file line numberDiff line numberDiff line change
@@ -291,45 +291,12 @@ impl SeedableRng for StdRng {
291291
impl CryptoRng for StdRng {}
292292

293293

294-
#[cfg(all(feature="std",
295-
any(target_os = "linux", target_os = "android",
296-
target_os = "netbsd",
297-
target_os = "dragonfly",
298-
target_os = "haiku",
299-
target_os = "emscripten",
300-
target_os = "solaris",
301-
target_os = "cloudabi",
302-
target_os = "macos", target_os = "ios",
303-
target_os = "freebsd",
304-
target_os = "openbsd", target_os = "bitrig",
305-
target_os = "redox",
306-
target_os = "fuchsia",
307-
windows,
308-
all(target_arch = "wasm32", feature = "stdweb"),
309-
all(target_arch = "wasm32", feature = "wasm-bindgen"),
310-
)))]
294+
#[cfg(feature="rand_os")]
311295
#[derive(Clone, Debug)]
312296
#[deprecated(since="0.6.0", note="import with rand::rngs::OsRng instead")]
313297
pub struct OsRng(rngs::OsRng);
314298

315-
#[cfg(all(feature="std",
316-
any(target_os = "linux", target_os = "android",
317-
target_os = "netbsd",
318-
target_os = "dragonfly",
319-
target_os = "haiku",
320-
target_os = "emscripten",
321-
target_os = "solaris",
322-
target_os = "cloudabi",
323-
target_os = "macos", target_os = "ios",
324-
target_os = "freebsd",
325-
target_os = "openbsd", target_os = "bitrig",
326-
target_os = "redox",
327-
target_os = "fuchsia",
328-
windows,
329-
all(target_arch = "wasm32", feature = "stdweb"),
330-
all(target_arch = "wasm32", feature = "wasm-bindgen"),
331-
)))]
332-
#[cfg(feature="std")]
299+
#[cfg(feature="rand_os")]
333300
impl RngCore for OsRng {
334301
#[inline(always)]
335302
fn next_u32(&mut self) -> u32 {
@@ -352,48 +319,14 @@ impl RngCore for OsRng {
352319
}
353320
}
354321

355-
#[cfg(all(feature="std",
356-
any(target_os = "linux", target_os = "android",
357-
target_os = "netbsd",
358-
target_os = "dragonfly",
359-
target_os = "haiku",
360-
target_os = "emscripten",
361-
target_os = "solaris",
362-
target_os = "cloudabi",
363-
target_os = "macos", target_os = "ios",
364-
target_os = "freebsd",
365-
target_os = "openbsd", target_os = "bitrig",
366-
target_os = "redox",
367-
target_os = "fuchsia",
368-
windows,
369-
all(target_arch = "wasm32", feature = "stdweb"),
370-
all(target_arch = "wasm32", feature = "wasm-bindgen"),
371-
)))]
372-
#[cfg(feature="std")]
322+
#[cfg(feature="rand_os")]
373323
impl OsRng {
374324
pub fn new() -> Result<Self, Error> {
375325
rngs::OsRng::new().map(OsRng)
376326
}
377327
}
378328

379-
#[cfg(all(feature="std",
380-
any(target_os = "linux", target_os = "android",
381-
target_os = "netbsd",
382-
target_os = "dragonfly",
383-
target_os = "haiku",
384-
target_os = "emscripten",
385-
target_os = "solaris",
386-
target_os = "cloudabi",
387-
target_os = "macos", target_os = "ios",
388-
target_os = "freebsd",
389-
target_os = "openbsd", target_os = "bitrig",
390-
target_os = "redox",
391-
target_os = "fuchsia",
392-
windows,
393-
all(target_arch = "wasm32", feature = "stdweb"),
394-
all(target_arch = "wasm32", feature = "wasm-bindgen"),
395-
)))]
396-
#[cfg(feature="std")]
329+
#[cfg(feature="rand_os")]
397330
impl CryptoRng for OsRng {}
398331

399332

0 commit comments

Comments
 (0)