//! `rand` on macOS: `getentropy`.
use core::ffi::{c_int, c_void};
use core::mem::MaybeUninit;
use crate::Result;
use crate::sys::cvt;
unsafe extern "C" {
fn getentropy(buf: *mut c_void, len: usize) -> c_int;
}
/// The most `getentropy` gives in one call.
const CHUNK: usize = 256;
pub(crate) fn fill(buf: &mut [MaybeUninit<u8>]) -> Result<()> {
for chunk in buf.chunks_mut(CHUNK) {
// SAFETY: the function writes exactly `chunk.len()` bytes, which is
// no more than it accepts, or fails.
cvt(unsafe { getentropy(chunk.as_mut_ptr().cast(), chunk.len()) })?;
}
Ok(())
}