Skip to content

Commit 88413c4

Browse files
committed
Add Rng::bytes
Add tests for `fill` and `bytes`.
1 parent ae75703 commit 88413c4

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

src/lib.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -449,6 +449,14 @@ impl Rng {
449449
}
450450
}
451451

452+
/// Return a byte array of the given size.
453+
#[inline]
454+
pub fn bytes<const N: usize>(&self) -> [u8; N] {
455+
let mut bytes = [0u8; N];
456+
self.fill(&mut bytes);
457+
bytes
458+
}
459+
452460
rng_integer!(
453461
u8,
454462
u8,

tests/smoke.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,29 @@ fn u128() {
8181
}
8282
}
8383

84+
#[test]
85+
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
86+
fn fill() {
87+
let r = fastrand::Rng::new();
88+
let mut a = [0u8; 64];
89+
let mut b = [0u8; 64];
90+
91+
r.fill(&mut a);
92+
r.fill(&mut b);
93+
94+
assert_ne!(a, b);
95+
}
96+
97+
#[test]
98+
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
99+
fn bytes() {
100+
let r = fastrand::Rng::new();
101+
let a: [u8; 32] = r.bytes();
102+
let b: [u8; 32] = r.bytes();
103+
104+
assert_ne!(a, b);
105+
}
106+
84107
#[test]
85108
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
86109
fn rng() {

0 commit comments

Comments
 (0)