twizzler_rt_abi/random.rs
1//! Functions for collecting randomness.
2use core::mem::MaybeUninit;
3
4use crate::nk;
5
6bitflags::bitflags! {
7 /// Possible flags to get random.
8 pub struct GetRandomFlags: crate::bindings::get_random_flags {
9 /// Do not block. If the operation would block, return fewer than requested bytes.
10 const NON_BLOCKING = crate::bindings::GET_RANDOM_NON_BLOCKING;
11 }
12}
13
14/// Fill up to buf.len() bytes of randomness into the buffer.
15pub fn twz_rt_get_random(buf: &mut [MaybeUninit<u8>], flags: GetRandomFlags) -> usize {
16 unsafe {
17 nk!(crate::bindings::twz_rt_get_random(
18 buf.as_mut_ptr().cast(),
19 buf.len(),
20 flags.bits()
21 ))
22 }
23}