Skip to content

Commit 31e5b2f

Browse files
authored
Update wasi dependency (#100)
1 parent 208b318 commit 31e5b2f

File tree

3 files changed

+28
-24
lines changed

3 files changed

+28
-24
lines changed

Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ core = { version = "1.0", optional = true, package = "rustc-std-workspace-core"
2929
libc = { version = "0.2.62", default-features = false }
3030

3131
[target.'cfg(target_os = "wasi")'.dependencies]
32-
wasi = "0.5"
32+
wasi = "0.7"
3333

3434
[target.wasm32-unknown-unknown.dependencies]
3535
wasm-bindgen = { version = "0.2.29", optional = true }

src/error.rs

+22-15
Original file line numberDiff line numberDiff line change
@@ -60,22 +60,29 @@ impl Error {
6060
}
6161
}
6262

63-
#[cfg(any(unix, target_os = "redox"))]
64-
fn os_err_desc(errno: i32, buf: &mut [u8]) -> Option<&str> {
65-
let buf_ptr = buf.as_mut_ptr() as *mut libc::c_char;
66-
if unsafe { libc::strerror_r(errno, buf_ptr, buf.len()) } != 0 {
67-
return None;
68-
}
69-
70-
// Take up to trailing null byte
71-
let n = buf.len();
72-
let idx = buf.iter().position(|&b| b == 0).unwrap_or(n);
73-
core::str::from_utf8(&buf[..idx]).ok()
74-
}
63+
cfg_if! {
64+
if #[cfg(unix)] {
65+
fn os_err_desc(errno: i32, buf: &mut [u8]) -> Option<&str> {
66+
let buf_ptr = buf.as_mut_ptr() as *mut libc::c_char;
67+
if unsafe { libc::strerror_r(errno, buf_ptr, buf.len()) } != 0 {
68+
return None;
69+
}
7570

76-
#[cfg(not(any(unix, target_os = "redox")))]
77-
fn os_err_desc(_errno: i32, _buf: &mut [u8]) -> Option<&str> {
78-
None
71+
// Take up to trailing null byte
72+
let n = buf.len();
73+
let idx = buf.iter().position(|&b| b == 0).unwrap_or(n);
74+
core::str::from_utf8(&buf[..idx]).ok()
75+
}
76+
} else if #[cfg(target_os = "wasi")] {
77+
fn os_err_desc(errno: i32, _buf: &mut [u8]) -> Option<&str> {
78+
core::num::NonZeroU16::new(errno as u16)
79+
.and_then(wasi::wasi_unstable::error_str)
80+
}
81+
} else {
82+
fn os_err_desc(_errno: i32, _buf: &mut [u8]) -> Option<&str> {
83+
None
84+
}
85+
}
7986
}
8087

8188
impl fmt::Debug for Error {

src/wasi.rs

+5-8
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,12 @@
88

99
//! Implementation for WASI
1010
use crate::Error;
11-
use core::num::NonZeroU32;
11+
use core::num;
1212
use wasi::wasi_unstable::random_get;
1313

1414
pub fn getrandom_inner(dest: &mut [u8]) -> Result<(), Error> {
15-
let ret = random_get(dest);
16-
if let Some(code) = NonZeroU32::new(ret as u32) {
17-
error!("WASI: random_get failed with return value {}", code);
18-
Err(Error::from(code))
19-
} else {
20-
Ok(()) // Zero means success for WASI
21-
}
15+
random_get(dest).map_err(|e: num::NonZeroU16| {
16+
// convert wasi's NonZeroU16 error into getrandom's NonZeroU32 error
17+
num::NonZeroU32::new(e.get() as u32).unwrap().into()
18+
})
2219
}

0 commit comments

Comments
 (0)