oscall 0.1.0

Low-level cross-platform interface to operating system services: files, memory mapping, networking and more, with no libc on Linux
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
//! `rand` on WASI: `random_get`.

use core::mem::MaybeUninit;

use crate::Result;
use crate::sys::cvt;

#[link(wasm_import_module = "wasi_snapshot_preview1")]
unsafe extern "C" {
    fn random_get(buf: *mut u8, len: usize) -> i32;
}

pub(crate) fn fill(buf: &mut [MaybeUninit<u8>]) -> Result<()> {
    // SAFETY: the host writes exactly `buf.len()` bytes, or fails.
    cvt(unsafe { random_get(buf.as_mut_ptr().cast(), buf.len()) })
}