oscall 0.1.1

Low-level cross-platform interface to operating system services: files, memory mapping, networking and more, with no libc on Linux
Documentation
//! `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(())
}