Skip to content

Commit 272f44a

Browse files
committed
Add Custom RNG: getrandom-wasm-bindgen
1 parent 57b0630 commit 272f44a

File tree

3 files changed

+24
-4
lines changed

3 files changed

+24
-4
lines changed

Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ appveyor = { repository = "rust-random/getrandom" }
1818
members = [
1919
"custom/dummy",
2020
"custom/stdweb",
21+
"custom/wasm-bindgen",
2122
]
2223

2324
[dependencies]

custom/wasm-bindgen/Cargo.toml

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
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+
[lib]
13+
crate-type = ["dylib"]
14+
15+
[dependencies]
16+
getrandom = { path = "../..", features = ["custom"] }
17+
wasm-bindgen = "0.2.29"

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

+6-4
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)