use rand::rngs::SmallRng;
use rand::{Rng, SeedableRng};
use wasm_bindgen::prelude::*;
#[wasm_bindgen(js_name = "Rng")]
pub struct WasmRng {
inner: SmallRng,
}
#[wasm_bindgen(js_class = "Rng")]
impl WasmRng {
#[wasm_bindgen(constructor)]
pub fn new() -> WasmRng {
WasmRng {
inner: SmallRng::from_entropy(),
}
}
#[wasm_bindgen(js_name = "fromSeed")]
pub fn from_seed(seed: u64) -> WasmRng {
WasmRng {
inner: SmallRng::seed_from_u64(seed),
}
}
#[wasm_bindgen(js_name = "nextU32")]
pub fn next_u32(&mut self) -> u32 {
self.inner.gen()
}
#[wasm_bindgen(js_name = "nextU64")]
pub fn next_u64(&mut self) -> u64 {
self.inner.gen()
}
#[wasm_bindgen(js_name = "nextFloat")]
pub fn next_float(&mut self) -> f64 {
self.inner.gen()
}
}
impl Default for WasmRng {
fn default() -> Self {
Self::new()
}
}
impl WasmRng {
pub fn as_rng(&mut self) -> &mut SmallRng {
&mut self.inner
}
pub fn inner_mut(&mut self) -> &mut SmallRng {
&mut self.inner
}
}