From c30665691421549a8f391f42fd27af93da06e277 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=90=D1=80=D1=82=D1=91=D0=BC=20=D0=9F=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=BE=D0=B2=20=5BArtyom=20Pavlov=5D?= Date: Fri, 27 Sep 2024 21:20:39 +0300 Subject: [PATCH] Remove `wasi` dependency completely --- Cargo.toml | 3 --- src/wasi.rs | 17 ++++++++++++++++- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 2d8baa62..eee54add 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -21,9 +21,6 @@ core = { version = "1.0", optional = true, package = "rustc-std-workspace-core" [target.'cfg(unix)'.dependencies] libc = { version = "0.2.154", default-features = false } -[target.'cfg(all(target_arch = "wasm32", target_os = "wasi", target_env = "p2"))'.dependencies] -wasi = { version = "0.13", default-features = false } - [target.'cfg(all(windows, not(target_vendor = "win7")))'.dependencies] windows-targets = "0.52" diff --git a/src/wasi.rs b/src/wasi.rs index db2a800e..8c8804c6 100644 --- a/src/wasi.rs +++ b/src/wasi.rs @@ -11,6 +11,9 @@ compile_error!( or Rust version older than 1.80 was used" ); +#[cfg(not(target_arch = "wasm32"))] +compile_error!("Currently only 32-bit WASI targets are supported"); + #[cfg(target_env = "p1")] pub fn getrandom_inner(dest: &mut [MaybeUninit]) -> Result<(), Error> { // This linking is vendored from the wasi crate: @@ -34,7 +37,19 @@ pub fn getrandom_inner(dest: &mut [MaybeUninit]) -> Result<(), Error> { #[cfg(target_env = "p2")] pub fn getrandom_inner(dest: &mut [MaybeUninit]) -> Result<(), Error> { use core::ptr::copy_nonoverlapping; - use wasi::random::random::get_random_u64; + + // Based on the code from the wasi v0.13 crate: + // https://docs.rs/wasi/0.13.2+wasi-0.2.1/src/wasi/bindings.rs.html#10845-10860 + fn get_random_u64() -> u64 { + unsafe { + #[link(wasm_import_module = "wasi:random/random@0.2.1")] + extern "C" { + #[link_name = "get-random-u64"] + fn wit_import() -> i64; + } + wit_import() as u64 + } + } let (prefix, chunks, suffix) = unsafe { dest.align_to_mut::>() };