Sha256: 356bbaac06552050f146fc5f966129ddcf17abfc6ea0eed2333127ddf260ad9e

Contents?: true

Size: 1.17 KB

Versions: 10

Compression:

Stored size: 1.17 KB

Contents

use crate::bindings::random::{insecure, insecure_seed, random};
use crate::{WasiImpl, WasiView};
use cap_rand::{distributions::Standard, Rng};

impl<T> random::Host for WasiImpl<T>
where
    T: WasiView,
{
    fn get_random_bytes(&mut self, len: u64) -> anyhow::Result<Vec<u8>> {
        Ok((&mut self.ctx().random)
            .sample_iter(Standard)
            .take(len as usize)
            .collect())
    }

    fn get_random_u64(&mut self) -> anyhow::Result<u64> {
        Ok(self.ctx().random.sample(Standard))
    }
}

impl<T> insecure::Host for WasiImpl<T>
where
    T: WasiView,
{
    fn get_insecure_random_bytes(&mut self, len: u64) -> anyhow::Result<Vec<u8>> {
        Ok((&mut self.ctx().insecure_random)
            .sample_iter(Standard)
            .take(len as usize)
            .collect())
    }

    fn get_insecure_random_u64(&mut self) -> anyhow::Result<u64> {
        Ok(self.ctx().insecure_random.sample(Standard))
    }
}

impl<T> insecure_seed::Host for WasiImpl<T>
where
    T: WasiView,
{
    fn insecure_seed(&mut self) -> anyhow::Result<(u64, u64)> {
        let seed: u128 = self.ctx().insecure_random_seed;
        Ok((seed as u64, (seed >> 64) as u64))
    }
}

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
wasmtime-29.0.0 ./ext/cargo-vendor/wasmtime-wasi-29.0.0/src/host/random.rs
wasmtime-28.0.0 ./ext/cargo-vendor/wasmtime-wasi-28.0.0/src/host/random.rs
wasmtime-27.0.0 ./ext/cargo-vendor/wasmtime-wasi-27.0.0/src/host/random.rs
wasmtime-26.0.0 ./ext/cargo-vendor/wasmtime-wasi-26.0.0/src/host/random.rs
wasmtime-25.0.2 ./ext/cargo-vendor/wasmtime-wasi-25.0.2/src/host/random.rs
wasmtime-25.0.1 ./ext/cargo-vendor/wasmtime-wasi-25.0.1/src/host/random.rs
wasmtime-25.0.0 ./ext/cargo-vendor/wasmtime-wasi-25.0.0/src/host/random.rs
wasmtime-24.0.0 ./ext/cargo-vendor/wasmtime-wasi-24.0.0/src/host/random.rs
wasmtime-23.0.2 ./ext/cargo-vendor/wasmtime-wasi-23.0.2/src/host/random.rs
wasmtime-22.0.0 ./ext/cargo-vendor/wasmtime-wasi-22.0.0/src/host/random.rs