Skip to content

Commit 57b0630

Browse files
committed
Add Custom RNG: getrandom-stdweb
1 parent ff80c0c commit 57b0630

File tree

3 files changed

+27
-5
lines changed

3 files changed

+27
-5
lines changed

Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ appveyor = { repository = "rust-random/getrandom" }
1717
[workspace]
1818
members = [
1919
"custom/dummy",
20+
"custom/stdweb",
2021
]
2122

2223
[dependencies]

custom/stdweb/Cargo.toml

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

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

+8-5
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);

0 commit comments

Comments
 (0)