diff --git a/CHANGELOG.md b/CHANGELOG.md index a7bdb29c..6f43b26f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,12 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## [0.1.12] - 2019-08-18 +### Changed +- Update wasi dependency from v0.5 to v0.7. [#100] + +[#100]: https://github.com/rust-random/getrandom/pull/100 + ## [0.1.11] - 2019-08-25 ### Fixed - Implement `std`-dependent traits for selected targets even if `std` diff --git a/Cargo.toml b/Cargo.toml index 03abdd4c..cf699363 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "getrandom" -version = "0.1.11" +version = "0.1.12" edition = "2018" authors = ["The Rand Project Developers"] license = "MIT OR Apache-2.0" @@ -29,7 +29,7 @@ core = { version = "1.0", optional = true, package = "rustc-std-workspace-core" libc = { version = "0.2.62", default-features = false } [target.'cfg(target_os = "wasi")'.dependencies] -wasi = "0.5" +wasi = "0.7" [target.wasm32-unknown-unknown.dependencies] wasm-bindgen = { version = "0.2.29", optional = true } @@ -37,7 +37,7 @@ stdweb = { version = "0.4.18", optional = true } [features] std = [] -# enables dummy implementation for unsupported targets +# Enables dummy implementation for unsupported targets dummy = [] # Unstable feature to support being a libstd dependency rustc-dep-of-std = ["compiler_builtins", "core"] diff --git a/src/error.rs b/src/error.rs index ca2e629a..d7e28e6d 100644 --- a/src/error.rs +++ b/src/error.rs @@ -60,22 +60,29 @@ impl Error { } } -#[cfg(any(unix, target_os = "redox"))] -fn os_err_desc(errno: i32, buf: &mut [u8]) -> Option<&str> { - let buf_ptr = buf.as_mut_ptr() as *mut libc::c_char; - if unsafe { libc::strerror_r(errno, buf_ptr, buf.len()) } != 0 { - return None; - } - - // Take up to trailing null byte - let n = buf.len(); - let idx = buf.iter().position(|&b| b == 0).unwrap_or(n); - core::str::from_utf8(&buf[..idx]).ok() -} +cfg_if! { + if #[cfg(unix)] { + fn os_err_desc(errno: i32, buf: &mut [u8]) -> Option<&str> { + let buf_ptr = buf.as_mut_ptr() as *mut libc::c_char; + if unsafe { libc::strerror_r(errno, buf_ptr, buf.len()) } != 0 { + return None; + } -#[cfg(not(any(unix, target_os = "redox")))] -fn os_err_desc(_errno: i32, _buf: &mut [u8]) -> Option<&str> { - None + // Take up to trailing null byte + let n = buf.len(); + let idx = buf.iter().position(|&b| b == 0).unwrap_or(n); + core::str::from_utf8(&buf[..idx]).ok() + } + } else if #[cfg(target_os = "wasi")] { + fn os_err_desc(errno: i32, _buf: &mut [u8]) -> Option<&str> { + core::num::NonZeroU16::new(errno as u16) + .and_then(wasi::wasi_unstable::error_str) + } + } else { + fn os_err_desc(_errno: i32, _buf: &mut [u8]) -> Option<&str> { + None + } + } } impl fmt::Debug for Error { diff --git a/src/lib.rs b/src/lib.rs index 48095799..8a72f732 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -166,14 +166,12 @@ cfg_if! { if #[cfg(any(target_os = "android", target_os = "dragonfly", target_os = "emscripten", target_os = "freebsd", target_os = "haiku", target_os = "illumos", target_os = "linux", target_os = "macos", target_os = "netbsd", - target_os = "openbsd", target_os = "redox", target_os = "solaris"))] { + target_os = "openbsd", target_os = "redox", target_os = "solaris", + target_os = "vxworks"))] { #[allow(dead_code)] mod util_libc; // Keep std-only trait definitions for backwards compatiblity mod error_impls; - } else if #[cfg(target_os = "vxworks")] { - #[allow(dead_code)] - mod util_libc; } else if #[cfg(feature = "std")] { mod error_impls; } diff --git a/src/wasi.rs b/src/wasi.rs index a050afc8..713c1ab9 100644 --- a/src/wasi.rs +++ b/src/wasi.rs @@ -8,15 +8,12 @@ //! Implementation for WASI use crate::Error; -use core::num::NonZeroU32; +use core::num; use wasi::wasi_unstable::random_get; pub fn getrandom_inner(dest: &mut [u8]) -> Result<(), Error> { - let ret = random_get(dest); - if let Some(code) = NonZeroU32::new(ret as u32) { - error!("WASI: random_get failed with return value {}", code); - Err(Error::from(code)) - } else { - Ok(()) // Zero means success for WASI - } + random_get(dest).map_err(|e: num::NonZeroU16| { + // convert wasi's NonZeroU16 error into getrandom's NonZeroU32 error + num::NonZeroU32::new(e.get() as u32).unwrap().into() + }) }