forked from rust-random/getrandom
/
mod.rs
29 lines (25 loc) · 816 Bytes
/
mod.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
// Common infrastructure for the custom* test suites (only).
use core::{
num::NonZeroU32,
sync::atomic::{AtomicU8, Ordering},
};
use getrandom::{register_custom_getrandom, Error};
pub fn len7_err() -> Error {
NonZeroU32::new(Error::INTERNAL_START + 7).unwrap().into()
}
fn getrandom_impl(buf: &mut [u8]) -> Result<(), Error> {
// `getrandom` guarantees it will not call any implementation if the output
// buffer is empty.
assert!(!buf.is_empty());
// Length 7 buffers return a custom error
if buf.len() == 7 {
return Err(len7_err());
}
// Otherwise, increment an atomic counter
static COUNTER: AtomicU8 = AtomicU8::new(0);
for b in buf {
*b = COUNTER.fetch_add(1, Ordering::Relaxed);
}
Ok(())
}
register_custom_getrandom!(getrandom_impl);