You're Invited:Meet the Socket Team at RSAC and BSidesSF 2026, March 23–26.RSVP
Socket
Book a DemoSign in
Socket

getrandom

Package Overview
Dependencies
Maintainers
0
Versions
47
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

getrandom - cargo Package Compare versions

Comparing version
0.4.0-rc.0
to
0.4.0-rc.1
+61
src/backends/wasi_p2_3.rs
//! Implementation for WASIp2 and WASIp3.
use crate::Error;
use core::{mem::MaybeUninit, ptr::copy_nonoverlapping};
#[cfg(target_env = "p2")]
use wasip2 as wasi;
// Workaround to silence `unexpected_cfgs` warning
// on Rust version between 1.85 and 1.91
#[cfg(not(target_env = "p2"))]
#[cfg(target_env = "p3")]
use wasip3 as wasi;
#[cfg(not(target_env = "p2"))]
#[cfg(not(target_env = "p3"))]
compile_error!("Unknown version of WASI (only previews 1, 2 and 3 are supported)");
use wasi::random::random::get_random_u64;
#[inline]
pub fn inner_u32() -> Result<u32, Error> {
let val = get_random_u64();
Ok(crate::util::truncate(val))
}
#[inline]
pub fn inner_u64() -> Result<u64, Error> {
Ok(get_random_u64())
}
#[inline]
pub fn fill_inner(dest: &mut [MaybeUninit<u8>]) -> Result<(), Error> {
let (prefix, chunks, suffix) = unsafe { dest.align_to_mut::<MaybeUninit<u64>>() };
// We use `get_random_u64` instead of `get_random_bytes` because the latter creates
// an allocation due to the Wit IDL [restrictions][0]. This should be fine since
// the main use case of `getrandom` is seed generation.
//
// [0]: https://github.com/WebAssembly/wasi-random/issues/27
if !prefix.is_empty() {
let val = get_random_u64();
let src = (&val as *const u64).cast();
unsafe {
copy_nonoverlapping(src, prefix.as_mut_ptr(), prefix.len());
}
}
for dst in chunks {
dst.write(get_random_u64());
}
if !suffix.is_empty() {
let val = get_random_u64();
let src = (&val as *const u64).cast();
unsafe {
copy_nonoverlapping(src, suffix.as_mut_ptr(), suffix.len());
}
}
Ok(())
}
+1
-1
{
"git": {
"sha1": "78536276778a84f92a0ffc712401b581c6bfb354"
"sha1": "b6a28dcbcfc928bc45613f5f6807b93378f2f1be"
},
"path_in_vcs": ""
}
+249
-20

@@ -6,2 +6,8 @@ # This file is automatically @generated by Cargo.

[[package]]
name = "anyhow"
version = "1.0.100"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a23eb6b1614318a8071c9b2521f36b424b2c83db5eb3a0fead4a6c0809af6e61"
[[package]]
name = "async-trait"

@@ -24,2 +30,8 @@ version = "0.1.89"

[[package]]
name = "bitflags"
version = "2.10.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "812e12b5285cc515a9c72a5c1d3b6d46a19dac5acfef5265968c166106e31dd3"
[[package]]
name = "bumpalo"

@@ -38,5 +50,5 @@ version = "3.19.1"

name = "cc"
version = "1.2.51"
version = "1.2.53"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7a0aeaff4ff1a90589618835a598e545176939b97874f7abc7851caa0618f203"
checksum = "755d2fce177175ffca841e9a06afdb2c4ab0f593d53b4dee48147dfaade85932"
dependencies = [

@@ -54,10 +66,22 @@ "find-msvc-tools",

[[package]]
name = "equivalent"
version = "1.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "877a4ace8713b0bcf2a4e7eec82529c029f1d0619886d18145fea96c3ffe5c0f"
[[package]]
name = "find-msvc-tools"
version = "0.1.6"
version = "0.1.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "645cbb3a84e60b7531617d5ae4e57f7e27308f6445f5abf653209ea76dec8dff"
checksum = "8591b0bcc8a98a64310a2fae1bb3e9b8564dd10e381e6e28010fde8e8e8568db"
[[package]]
name = "foldhash"
version = "0.1.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d9c4f5dac5e15c24eb999c26181a6ca40b39fe946cbe4c263c7209467bc83af2"
[[package]]
name = "getrandom"
version = "0.4.0-rc.0"
version = "0.4.0-rc.1"
dependencies = [

@@ -70,2 +94,3 @@ "cfg-if",

"wasip2",
"wasip3",
"wasm-bindgen",

@@ -76,2 +101,41 @@ "wasm-bindgen-test",

[[package]]
name = "hashbrown"
version = "0.15.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9229cfe53dfd69f0609a49f65461bd93001ea1ef889cd5529dd176593f5338a1"
dependencies = [
"foldhash",
]
[[package]]
name = "hashbrown"
version = "0.16.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "841d1cc9bed7f9236f321df977030373f4a4163ae1a7dbfe1a51a2c1a51d9100"
[[package]]
name = "heck"
version = "0.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea"
[[package]]
name = "id-arena"
version = "2.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3d3067d79b975e8844ca9eb072e16b31c3c1c36928edf9c6789548c524d0d954"
[[package]]
name = "indexmap"
version = "2.13.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7714e70437a7dc3ac8eb7e6f8df75fd8eb422675fc7678aff7364301092b1017"
dependencies = [
"equivalent",
"hashbrown 0.16.1",
"serde",
"serde_core",
]
[[package]]
name = "itoa"

@@ -93,6 +157,12 @@ version = "1.0.17"

[[package]]
name = "leb128fmt"
version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "09edd9e8b54e49e587e4f6295a7d29c3ea94d469cb40ab8ca70b288248a81db2"
[[package]]
name = "libc"
version = "0.2.178"
version = "0.2.180"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "37c93d8daa9d8a012fd8ab92f088405fb202ea0b6ab73ee2482ae66af4f42091"
checksum = "bcc35a38544a891a5f7c865aca548a982ccb3b8650a5b06d0fd33a10283c56fc"

@@ -106,2 +176,8 @@ [[package]]

[[package]]
name = "log"
version = "0.4.29"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5e5032e24019045c762d3c0f28f5b6b8bbf38563a65908389bf7978758920897"
[[package]]
name = "memchr"

@@ -154,6 +230,16 @@ version = "2.7.6"

[[package]]
name = "prettyplease"
version = "0.2.37"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "479ca8adacdd7ce8f1fb39ce9ecccbfe93a3f1344b3d0d97f20bc0196208f62b"
dependencies = [
"proc-macro2",
"syn",
]
[[package]]
name = "proc-macro2"
version = "1.0.103"
version = "1.0.105"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5ee95bc4ef87b8d5ba32e8b7714ccc834865276eab0aed5c9958d00ec45f49e8"
checksum = "535d180e0ecab6268a3e718bb9fd44db66bbbc256257165fc699dadf70d16fe7"
dependencies = [

@@ -165,5 +251,5 @@ "unicode-ident",

name = "quote"
version = "1.0.42"
version = "1.0.43"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a338cc41d27e6cc6dce6cefc13a0729dfbb81c262b1f519331575dd80ef3067f"
checksum = "dc74d9a594b72ae6656596548f56f667211f8a97b3d4c3d467150794690dc40a"
dependencies = [

@@ -181,5 +267,5 @@ "proc-macro2",

name = "rand_core"
version = "0.10.0-rc-3"
version = "0.10.0-rc-6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f66ee92bc15280519ef199a274fe0cafff4245d31bc39aaa31c011ad56cb1f05"
checksum = "70765ff7112b0fb2d272d24d9a2f907fc206211304328fe58b2db15a5649ef28"

@@ -202,2 +288,8 @@ [[package]]

[[package]]
name = "semver"
version = "1.0.27"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d767eb0aabc880b29956c35734170f26ed551a859dbd361d140cdbeca61ab1e2"
[[package]]
name = "serde"

@@ -234,5 +326,5 @@ version = "1.0.228"

name = "serde_json"
version = "1.0.148"
version = "1.0.149"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3084b546a1dd6289475996f182a22aba973866ea8e8b02c51d9f46b1336a22da"
checksum = "83fc039473c5595ace860d8c4fafa220ff474b3fc6bfdb4293327f1a37e94d86"
dependencies = [

@@ -254,5 +346,5 @@ "itoa",

name = "syn"
version = "2.0.111"
version = "2.0.114"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "390cc9a294ab71bdb1aa2e99d13be9c753cd2d7bd6560c77118597410c4d2e87"
checksum = "d4d107df263a3013ef9b1879b0df87d706ff80f65a86ea879bd9c31f9b307c2a"
dependencies = [

@@ -271,2 +363,8 @@ "proc-macro2",

[[package]]
name = "unicode-xid"
version = "0.2.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ebc1c04c71510c7f702b52b7c350734c9ff1295c464a03335b00bb84fc54f853"
[[package]]
name = "walkdir"

@@ -287,6 +385,15 @@ version = "2.5.0"

dependencies = [
"wit-bindgen",
"wit-bindgen 0.46.0",
]
[[package]]
name = "wasip3"
version = "0.3.1+wasi-0.3.0-rc-2025-09-16"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "87ba4be47b1d11244670d11857eee0758a8f2c39aea64d80b78c1ce29b4642cd"
dependencies = [
"wit-bindgen 0.48.1",
]
[[package]]
name = "wasm-bindgen"

@@ -382,2 +489,36 @@ version = "0.2.106"

[[package]]
name = "wasm-encoder"
version = "0.241.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e01164c9dda68301e34fdae536c23ed6fe90ce6d97213ccc171eebbd3d02d6b8"
dependencies = [
"leb128fmt",
"wasmparser",
]
[[package]]
name = "wasm-metadata"
version = "0.241.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "876fe286f2fa416386deedebe8407e6f19e0b5aeaef3d03161e77a15fa80f167"
dependencies = [
"anyhow",
"indexmap",
"wasm-encoder",
"wasmparser",
]
[[package]]
name = "wasmparser"
version = "0.241.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "46d90019b1afd4b808c263e428de644f3003691f243387d30d673211ee0cb8e8"
dependencies = [
"bitflags",
"hashbrown 0.15.5",
"indexmap",
"semver",
]
[[package]]
name = "web-sys"

@@ -423,5 +564,93 @@ version = "0.3.83"

[[package]]
name = "wit-bindgen"
version = "0.48.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7f8c2adb5f74ac9395bc3121c99a1254bf9310482c27b13f97167aedb5887138"
dependencies = [
"wit-bindgen-rust-macro",
]
[[package]]
name = "wit-bindgen-core"
version = "0.48.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9b881a098cae03686d7a0587f8f306f8a58102ad8da8b5599100fbe0e7f5800b"
dependencies = [
"anyhow",
"heck",
"wit-parser",
]
[[package]]
name = "wit-bindgen-rust"
version = "0.48.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "69667efa439a453e1d50dac939c6cab6d2c3ac724a9d232b6631dad2472a5b70"
dependencies = [
"anyhow",
"heck",
"indexmap",
"prettyplease",
"syn",
"wasm-metadata",
"wit-bindgen-core",
"wit-component",
]
[[package]]
name = "wit-bindgen-rust-macro"
version = "0.48.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "eae2e22cceb5d105d52326c07e3e67603a861cc7add70fc467f7cc7ec5265017"
dependencies = [
"anyhow",
"prettyplease",
"proc-macro2",
"quote",
"syn",
"wit-bindgen-core",
"wit-bindgen-rust",
]
[[package]]
name = "wit-component"
version = "0.241.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1fd0c57df25e7ee612d946d3b7646c1ddb2310f8280aa2c17e543b66e0812241"
dependencies = [
"anyhow",
"bitflags",
"indexmap",
"log",
"serde",
"serde_derive",
"serde_json",
"wasm-encoder",
"wasm-metadata",
"wasmparser",
"wit-parser",
]
[[package]]
name = "wit-parser"
version = "0.241.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "09ef1c6ad67f35c831abd4039c02894de97034100899614d1c44e2268ad01c91"
dependencies = [
"anyhow",
"id-arena",
"indexmap",
"log",
"semver",
"serde",
"serde_derive",
"serde_json",
"unicode-xid",
"wasmparser",
]
[[package]]
name = "zmij"
version = "1.0.0"
version = "1.0.15"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e6d6085d62852e35540689d1f97ad663e3971fc19cf5eceab364d62c646ea167"
checksum = "94f63c051f4fe3c1509da62131a678643c5b6fbdc9273b2b79d4378ebda003d2"

@@ -16,3 +16,3 @@ # THIS FILE IS AUTOMATICALLY GENERATED BY CARGO

name = "getrandom"
version = "0.4.0-rc.0"
version = "0.4.0-rc.1"
authors = ["The Rand Project Developers"]

@@ -80,3 +80,3 @@ build = "build.rs"

[dependencies.rand_core]
version = "0.10.0-rc-3"
version = "0.10.0-rc-6"
optional = true

@@ -105,2 +105,6 @@

[target.'cfg(all(target_arch = "wasm32", target_os = "wasi", target_env = "p3"))'.dependencies.wasip3]
version = "0.3"
default-features = false
[target.'cfg(all(target_os = "uefi", getrandom_backend = "efi_rng"))'.dependencies.r-efi]

@@ -142,3 +146,3 @@ version = "5.1"

check-cfg = [
'cfg(getrandom_backend, values("custom", "efi_rng", "rdrand", "rndr", "linux_getrandom", "linux_raw", "wasm_js", "windows_legacy", "unsupported"))',
'cfg(getrandom_backend, values("custom", "efi_rng", "rdrand", "rndr", "linux_getrandom", "linux_raw", "windows_legacy", "unsupported"))',
"cfg(getrandom_msan)",

@@ -145,0 +149,0 @@ "cfg(getrandom_test_linux_fallback)",

@@ -12,2 +12,3 @@ # Changelog

- `SysRng` behind new feature `sys_rng` [#751]
- WASIp3 support [#779]

@@ -20,2 +21,3 @@ ### Changed

[#751]: https://github.com/rust-random/getrandom/pull/751
[#779]: https://github.com/rust-random/getrandom/pull/779

@@ -193,2 +195,11 @@ ## [0.3.4] - 2025-10-14

## [0.2.17] - 2026-01-12
### Fixed
- Use `doc_cfg` instead of `doc_auto_cfg` (partial backport of [#732]) [#768]
- `BCryptGenRandom` signature [#778]
[#732]: https://github.com/rust-random/getrandom/pull/732
[#768]: https://github.com/rust-random/getrandom/pull/768
[#778]: https://github.com/rust-random/getrandom/pull/778
## [0.2.16] - 2025-04-22

@@ -656,2 +667,3 @@ ### Added

[0.3.0]: https://github.com/rust-random/getrandom/compare/v0.2.15...v0.3.0
[0.2.17]: https://github.com/rust-random/getrandom/compare/v0.2.16...v0.2.17
[0.2.16]: https://github.com/rust-random/getrandom/compare/v0.2.15...v0.2.16

@@ -658,0 +670,0 @@ [0.2.15]: https://github.com/rust-random/getrandom/compare/v0.2.14...v0.2.15

+23
-26

@@ -76,2 +76,23 @@ # getrandom: system's random number generator

### WebAssembly support
This crate fully supports the [WASI] and [Emscripten] targets. However,
the `wasm32-unknown-unknown` target (i.e. the target used by `wasm-pack`)
is not automatically supported since, from the target name alone, we cannot deduce
which JavaScript interface should be used (or if JavaScript is available at all).
We do not include support for this target in the default configuration because our JS backend
(supporting web browsers, web workers and Node.js v19 or later) requires [`wasm-bindgen`],
**bloating `Cargo.lock`** and **potentially breaking builds** on non-web WASM platforms.
To enable `getrandom`'s functionality on `wasm32-unknown-unknown` using
[`Crypto.getRandomValues`] via [`wasm-bindgen`], enable the `wasm_js` crate feature.
WARNING: We strongly recommend against enabling this feature in libraries (except for tests)
since it is known to break non-Web WASM builds and further since the usage of `wasm-bindgen`
causes significant bloat to `Cargo.lock` (on all targets).
The only exception to this rule: if your crate already unconditionally depends on `wasm-bindgen`
or `js-sys` on "unknown" WASM targets then it's acceptable to enable this feature unconditionally.
### Opt-in backends

@@ -88,3 +109,2 @@

| `rndr` | AArch64 | `aarch64-*` | [`RNDR`] register
| `wasm_js` | Web Browser, Node.js | `wasm32‑unknown‑unknown`, `wasm32v1-none` | [`Crypto.getRandomValues`]. Enabled by the `wasm_js` feature ([see below](#webassembly-support)).
| `efi_rng` | UEFI | `*-unknown‑uefi` | [`EFI_RNG_PROTOCOL`] with `EFI_RNG_ALGORITHM_RAW` (requires `std` and Nightly compiler)

@@ -99,4 +119,4 @@ | `windows_legacy` | Windows | `*-windows-*` | [`RtlGenRandom`]

# It's recommended to set the flag on a per-target basis:
[target.wasm32-unknown-unknown]
rustflags = ['--cfg', 'getrandom_backend="wasm_js"']
[target.'cfg(target_os = "linux")']
rustflags = ['--cfg', 'getrandom_backend="linux_getrandom"']
```

@@ -130,25 +150,2 @@

### WebAssembly support
This crate fully supports the [WASI] and [Emscripten] targets. However,
the `wasm32-unknown-unknown` target (i.e. the target used by `wasm-pack`)
is not automatically supported since, from the target name alone, we cannot deduce
which JavaScript interface should be used (or if JavaScript is available at all).
We do not include support for this target in the default configuration because
our JS backend (supporting web browsers, web workers and Node.js v19 or later)
requires [`wasm-bindgen`], **bloating `Cargo.lock`** and
**potentially breaking builds** on non-web WASM platforms.
To enable `getrandom`'s functionality on `wasm32-unknown-unknown` using the Web
Crypto methods [described above][opt-in] via [`wasm-bindgen`], enable the
`wasm_js` feature flag. Setting `RUSTFLAGS='--cfg getrandom_backend="wasm_js"'`
is allowed but is no longer required and does nothing (it was required in a
prior version of this crate).
WARNING: enabling the `wasm_js` feature will bloat `Cargo.lock` on all platforms
(where [`wasm-bindgen`] is not an existing dependency) and is known to cause
build issues on some non-web WASM platforms, even when a different backend is
selected via `getrandom_backend`.
### Custom backend

@@ -155,0 +152,0 @@

@@ -32,15 +32,2 @@ //! System-specific implementations.

pub use windows_legacy::*;
} else if #[cfg(getrandom_backend = "wasm_js")] {
cfg_if! {
if #[cfg(feature = "wasm_js")] {
mod wasm_js;
pub use wasm_js::*;
} else {
compile_error!(concat!(
"The \"wasm_js\" backend requires the `wasm_js` feature \
for `getrandom`. For more information see: \
https://docs.rs/getrandom/", env!("CARGO_PKG_VERSION"), "/#webassembly-support"
));
}
}
} else if #[cfg(getrandom_backend = "unsupported")] {

@@ -155,10 +142,5 @@ mod unsupported;

pub use wasi_p1::*;
} else if #[cfg(target_env = "p2")] {
mod wasi_p2;
pub use wasi_p2::*;
} else {
compile_error!(
"Unknown version of WASI (only previews 1 and 2 are supported) \
or Rust version older than 1.80 was used"
);
mod wasi_p2_3;
pub use wasi_p2_3::*;
}

@@ -192,4 +174,3 @@ }

"The wasm32-unknown-unknown targets are not supported by default; \
you may need to enable the \"wasm_js\" configuration flag. Note \
that enabling the `wasm_js` feature flag alone is insufficient. \
you may need to enable the \"wasm_js\" crate feature. \
For more information see: \

@@ -196,0 +177,0 @@ https://docs.rs/getrandom/", env!("CARGO_PKG_VERSION"), "/#webassembly-support"

@@ -180,3 +180,7 @@ #[cfg(feature = "std")]

Error::WINDOWS_RTL_GEN_RANDOM => "RtlGenRandom: Windows system function failure",
#[cfg(all(feature = "wasm_js", getrandom_backend = "wasm_js"))]
#[cfg(all(
feature = "wasm_js",
target_arch = "wasm32",
any(target_os = "unknown", target_os = "none")
))]
Error::WEB_CRYPTO => "Web Crypto API is unavailable",

@@ -183,0 +187,0 @@ #[cfg(target_os = "vxworks")]

use crate::Error;
use rand_core::{TryCryptoRng, TryRngCore};
use rand_core::{TryCryptoRng, TryRng};
/// A [`TryRngCore`] interface over the system's preferred random number source
/// A [`TryRng`] interface over the system's preferred random number source
///

@@ -12,5 +12,5 @@ /// This is a zero-sized struct. It can be freely constructed with just `SysRng`.

///
/// `SysRng` implements [`TryRngCore`]:
/// `SysRng` implements [`TryRng`]:
/// ```
/// use getrandom::{rand_core::TryRngCore, SysRng};
/// use getrandom::{rand_core::TryRng, SysRng};
///

@@ -21,8 +21,8 @@ /// let mut key = [0u8; 32];

///
/// Using it as an [`RngCore`] is possible using [`TryRngCore::unwrap_err`]:
/// Using it as an [`Rng`] is possible using [`UnwrapErr`]:
/// ```
/// use getrandom::rand_core::{TryRngCore, RngCore};
/// use getrandom::rand_core::{Rng, UnwrapErr};
/// use getrandom::SysRng;
///
/// let mut rng = SysRng.unwrap_err();
/// let mut rng = UnwrapErr(SysRng);
/// let random_u64 = rng.next_u64();

@@ -33,7 +33,7 @@ /// ```

/// [`rand::rngs::SysRng`]: https://docs.rs/rand/latest/rand/rngs/struct.SysRng.html
/// [`RngCore`]: rand_core::RngCore
/// [`Rng`]: rand_core::Rng
#[derive(Clone, Copy, Debug, Default)]
pub struct SysRng;
impl TryRngCore for SysRng {
impl TryRng for SysRng {
type Error = Error;

@@ -40,0 +40,0 @@

#![cfg(feature = "sys_rng")]
use getrandom::SysRng;
use getrandom::rand_core::TryRngCore;
use getrandom::rand_core::TryRng;

@@ -6,0 +6,0 @@ #[test]

//! Implementation for WASI Preview 2.
use crate::Error;
use core::{mem::MaybeUninit, ptr::copy_nonoverlapping};
use wasip2::random::random::get_random_u64;
#[inline]
pub fn inner_u32() -> Result<u32, Error> {
let val = get_random_u64();
Ok(crate::util::truncate(val))
}
#[inline]
pub fn inner_u64() -> Result<u64, Error> {
Ok(get_random_u64())
}
#[inline]
pub fn fill_inner(dest: &mut [MaybeUninit<u8>]) -> Result<(), Error> {
let (prefix, chunks, suffix) = unsafe { dest.align_to_mut::<MaybeUninit<u64>>() };
// We use `get_random_u64` instead of `get_random_bytes` because the latter creates
// an allocation due to the Wit IDL [restrictions][0]. This should be fine since
// the main use case of `getrandom` is seed generation.
//
// [0]: https://github.com/WebAssembly/wasi-random/issues/27
if !prefix.is_empty() {
let val = get_random_u64();
let src = (&val as *const u64).cast();
unsafe {
copy_nonoverlapping(src, prefix.as_mut_ptr(), prefix.len());
}
}
for dst in chunks {
dst.write(get_random_u64());
}
if !suffix.is_empty() {
let val = get_random_u64();
let src = (&val as *const u64).cast();
unsafe {
copy_nonoverlapping(src, suffix.as_mut_ptr(), suffix.len());
}
}
Ok(())
}

Sorry, the diff of this file is not supported yet