Skip to content

Commit

Permalink
Implement Rng::bytes
Browse files Browse the repository at this point in the history
Add tests for `fill` and `bytes`.
  • Loading branch information
cloudhead committed Sep 10, 2022
1 parent ae75703 commit 66b2491
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -449,6 +449,14 @@ impl Rng {
}
}

/// Return a byte array of the given size.
#[inline]
pub fn bytes<const N: usize>(&self) -> [u8; N] {
let mut bytes = [0u8; N];
self.fill(&mut bytes);
bytes
}

rng_integer!(
u8,
u8,
Expand Down
23 changes: 23 additions & 0 deletions tests/smoke.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,29 @@ fn u128() {
}
}

#[test]
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
fn fill() {
let r = fastrand::Rng::new();
let mut a = [0u8; 64];
let mut b = [0u8; 64];

r.fill(&mut a);
r.fill(&mut b);

assert_ne!(a, b);
}

#[test]
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
fn bytes() {
let r = fastrand::Rng::new();
let a: [u8; 32] = r.bytes();
let b: [u8; 32] = r.bytes();

assert_ne!(a, b);
}

#[test]
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
fn rng() {
Expand Down

0 comments on commit 66b2491

Please sign in to comment.