Skip to content

Cleanup wasm32-wasi target #306

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ core = { version = "1.0", optional = true, package = "rustc-std-workspace-core"
libc = { version = "0.2.120", default-features = false }

[target.'cfg(target_os = "wasi")'.dependencies]
wasi = "0.11"
wasi = { version = "0.11", default-features = false }

[target.'cfg(all(target_arch = "wasm32", target_os = "unknown"))'.dependencies]
wasm-bindgen = { version = "0.2.62", default-features = false, optional = true }
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ cfg_if! {
} else if #[cfg(target_os = "openbsd")] {
mod util_libc;
#[path = "openbsd.rs"] mod imp;
} else if #[cfg(target_os = "wasi")] {
} else if #[cfg(all(target_arch = "wasm32", target_os = "wasi"))] {
#[path = "wasi.rs"] mod imp;
} else if #[cfg(all(target_arch = "x86_64", target_os = "hermit"))] {
#[path = "rdrand.rs"] mod imp;
Expand Down
18 changes: 12 additions & 6 deletions src/wasi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,18 @@

//! Implementation for WASI
use crate::Error;
use core::{mem::MaybeUninit, num::NonZeroU32};
use wasi::wasi_snapshot_preview1::random_get;
use core::{
mem::MaybeUninit,
num::{NonZeroU16, NonZeroU32},
};
use wasi::random_get;

pub fn getrandom_inner(dest: &mut [MaybeUninit<u8>]) -> Result<(), Error> {
match unsafe { random_get(dest.as_mut_ptr() as i32, dest.len() as i32) } {
0 => Ok(()),
err => Err(unsafe { NonZeroU32::new_unchecked(err as u32) }.into()),
}
unsafe { random_get(dest.as_mut_ptr() as *mut u8, dest.len()) }.map_err(|e| {
// The WASI errno will always be non-zero, but we check just in case.
match NonZeroU16::new(e.raw()) {
Some(r) => Error::from(NonZeroU32::from(r)),
None => Error::ERRNO_NOT_POSITIVE,
}
})
}