Skip to content

Commit

Permalink
Remove wasi dependency completely
Browse files Browse the repository at this point in the history
  • Loading branch information
newpavlov committed Sep 27, 2024
1 parent 289b068 commit c306656
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
3 changes: 0 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down
17 changes: 16 additions & 1 deletion src/wasi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<u8>]) -> Result<(), Error> {
// This linking is vendored from the wasi crate:
Expand All @@ -34,7 +37,19 @@ pub fn getrandom_inner(dest: &mut [MaybeUninit<u8>]) -> Result<(), Error> {
#[cfg(target_env = "p2")]
pub fn getrandom_inner(dest: &mut [MaybeUninit<u8>]) -> 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::<MaybeUninit<u64>>() };

Expand Down

0 comments on commit c306656

Please sign in to comment.