Skip to content

Commit 562a00f

Browse files
committed
Add Custom RNGs for wasm-bindgen and stdweb
These currently don't have any tests, but they will successfully build.
1 parent 90c2ee2 commit 562a00f

File tree

5 files changed

+57
-9
lines changed

5 files changed

+57
-9
lines changed

Cargo.toml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,12 @@ exclude = ["utils/*", ".*", "appveyor.yml"]
1414
travis-ci = { repository = "rust-random/getrandom" }
1515
appveyor = { repository = "rust-random/getrandom" }
1616

17+
[workspace]
18+
members = [
19+
"custom/stdweb",
20+
"custom/wasm-bindgen",
21+
]
22+
1723
[dependencies]
1824
log = { version = "0.4", optional = true }
1925
cfg-if = "0.1"

custom/stdweb/Cargo.toml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
[package]
2+
name = "getrandom-stdweb"
3+
version = "0.1.0"
4+
edition = "2018"
5+
authors = ["The Rand Project Developers"]
6+
license = "MIT OR Apache-2.0"
7+
description = "Custom shim for using getrandom with stdweb"
8+
documentation = "https://docs.rs/getrandom-stdweb"
9+
repository = "https://github.com/rust-random/getrandom/tree/master/custom/stdweb"
10+
categories = ["wasm"]
11+
12+
[dependencies]
13+
getrandom = { path = "../..", features = ["custom"] }
14+
stdweb = "0.4.18"
15+
log = "0.4"
16+
17+
[[test]]
18+
name = "common"
19+
path = "../../tests/common.rs"

src/wasm32_stdweb.rs renamed to custom/stdweb/src/lib.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,25 +6,28 @@
66
// option. This file may not be copied, modified, or distributed
77
// except according to those terms.
88

9-
//! Implementation for WASM via stdweb
10-
extern crate std;
9+
//! `getrandom` implementation for WASM via stdweb
10+
#![cfg(all(target_arch = "wasm32", target_os = "unknown"))]
1111

1212
use core::mem;
13+
use std::sync::Once;
1314

15+
use log::error;
1416
use stdweb::js;
1517
use stdweb::unstable::TryInto;
1618
use stdweb::web::error::Error as WebError;
1719

18-
use crate::Error;
19-
use std::sync::Once;
20+
use getrandom::{register_custom_getrandom, Error};
2021

2122
#[derive(Clone, Copy, Debug)]
2223
enum RngSource {
2324
Browser,
2425
Node,
2526
}
2627

27-
pub fn getrandom_inner(dest: &mut [u8]) -> Result<(), Error> {
28+
register_custom_getrandom!(getrandom_inner);
29+
30+
fn getrandom_inner(dest: &mut [u8]) -> Result<(), Error> {
2831
assert_eq!(mem::size_of::<usize>(), 4);
2932
static ONCE: Once = Once::new();
3033
static mut RNG_SOURCE: Result<RngSource, Error> = Ok(RngSource::Node);

custom/wasm-bindgen/Cargo.toml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
[package]
2+
name = "getrandom-wasm-bindgen"
3+
version = "0.1.0"
4+
edition = "2018"
5+
authors = ["The Rand Project Developers"]
6+
license = "MIT OR Apache-2.0"
7+
description = "Custom shim for using getrandom with wasm-bindgen"
8+
documentation = "https://docs.rs/getrandom-bindgen"
9+
repository = "https://github.com/rust-random/getrandom/tree/master/custom/bindgen"
10+
categories = ["wasm"]
11+
12+
[dependencies]
13+
getrandom = { path = "../..", features = ["custom"] }
14+
wasm-bindgen = "0.2.29"
15+
16+
[[test]]
17+
name = "common"
18+
path = "../../tests/common.rs"

src/wasm32_bindgen.rs renamed to custom/wasm-bindgen/src/lib.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,16 @@
66
// option. This file may not be copied, modified, or distributed
77
// except according to those terms.
88

9-
//! Implementation for WASM via wasm-bindgen
10-
extern crate std;
9+
//! `getrandom` implementation for WASM via wasm-bindgen
10+
#![cfg(all(target_arch = "wasm32", target_os = "unknown"))]
1111

1212
use core::cell::RefCell;
1313
use core::mem;
1414
use std::thread_local;
1515

1616
use wasm_bindgen::prelude::*;
1717

18-
use crate::Error;
18+
use getrandom::{register_custom_getrandom, Error};
1919

2020
#[derive(Clone, Debug)]
2121
enum RngSource {
@@ -29,7 +29,9 @@ thread_local!(
2929
static RNG_SOURCE: RefCell<Option<RngSource>> = RefCell::new(None);
3030
);
3131

32-
pub fn getrandom_inner(dest: &mut [u8]) -> Result<(), Error> {
32+
register_custom_getrandom!(getrandom_inner);
33+
34+
fn getrandom_inner(dest: &mut [u8]) -> Result<(), Error> {
3335
assert_eq!(mem::size_of::<usize>(), 4);
3436

3537
RNG_SOURCE.with(|f| {

0 commit comments

Comments
 (0)