Skip to content

Commit 592dd33

Browse files
committed
Add Custom RNG: getrandom-wasm-bindgen
1 parent 464f19f commit 592dd33

File tree

3 files changed

+21
-4
lines changed

3 files changed

+21
-4
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ appveyor = { repository = "rust-random/getrandom" }
1717
[workspace]
1818
members = [
1919
"custom/stdweb",
20+
"custom/wasm-bindgen",
2021
]
2122

2223
[dependencies]

custom/wasm-bindgen/Cargo.toml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
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"

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)