From 387f2e756b9cdcc3763bfa2ce4d44b0fc15c7e1f Mon Sep 17 00:00:00 2001 From: Joe Richey Date: Fri, 26 Jul 2019 17:46:51 -0700 Subject: [PATCH] Address review feedback --- src/error.rs | 39 +++++++++++++++++++++++++-------------- src/util_libc.rs | 8 +++++++- 2 files changed, 32 insertions(+), 15 deletions(-) diff --git a/src/error.rs b/src/error.rs index fb0f3368..49ce5cb4 100644 --- a/src/error.rs +++ b/src/error.rs @@ -20,6 +20,11 @@ use core::num::NonZeroU32; pub struct Error(NonZeroU32); impl Error { + #[deprecated(since = "0.1.7")] + pub const UNKNOWN: Error = UNSUPPORTED; + #[deprecated(since = "0.1.7")] + pub const UNAVAILABLE: Error = UNSUPPORTED; + /// Codes below this point represent OS Errors (i.e. positive i32 values). /// Codes at or above this point, but below [`Error::CUSTOM_START`] are /// reserved for use by the `rand` and `getrandom` crates. @@ -111,25 +116,31 @@ impl From for Error { } } -/// Internal Error constants -pub(crate) const UNSUPPORTED: Error = internal_error(0); -pub(crate) const UNKNOWN_IO_ERROR: Error = internal_error(1); -pub(crate) const SEC_RANDOM_FAILED: Error = internal_error(2); -pub(crate) const RTL_GEN_RANDOM_FAILED: Error = internal_error(3); -pub(crate) const FAILED_RDRAND: Error = internal_error(4); -pub(crate) const NO_RDRAND: Error = internal_error(5); -pub(crate) const BINDGEN_CRYPTO_UNDEF: Error = internal_error(6); -pub(crate) const BINDGEN_GRV_UNDEF: Error = internal_error(7); -pub(crate) const STDWEB_NO_RNG: Error = internal_error(8); -pub(crate) const STDWEB_RNG_FAILED: Error = internal_error(9); - -const fn internal_error(n: u16) -> Error { - Error(unsafe { NonZeroU32::new_unchecked(Error::INTERNAL_START + n as u32) }) +// TODO: Convert to a function when min_version >= 1.33 +macro_rules! internal_error { + ($code:expr) => {{ + let n: u16 = $code; + Error(unsafe { NonZeroU32::new_unchecked(Error::INTERNAL_START + n as u32) }) + }}; } +/// Internal Error constants +pub(crate) const UNSUPPORTED: Error = internal_error!(0); +pub(crate) const ERRNO_NOT_POSITIVE: Error = internal_error!(1); +pub(crate) const UNKNOWN_IO_ERROR: Error = internal_error!(2); +pub(crate) const SEC_RANDOM_FAILED: Error = internal_error!(3); +pub(crate) const RTL_GEN_RANDOM_FAILED: Error = internal_error!(4); +pub(crate) const FAILED_RDRAND: Error = internal_error!(5); +pub(crate) const NO_RDRAND: Error = internal_error!(6); +pub(crate) const BINDGEN_CRYPTO_UNDEF: Error = internal_error!(7); +pub(crate) const BINDGEN_GRV_UNDEF: Error = internal_error!(8); +pub(crate) const STDWEB_NO_RNG: Error = internal_error!(9); +pub(crate) const STDWEB_RNG_FAILED: Error = internal_error!(10); + fn internal_desc(error: Error) -> Option<&'static str> { match error { UNSUPPORTED => Some("getrandom: this target is not supported"), + ERRNO_NOT_POSITIVE => Some("errno: did not return a positive value"), UNKNOWN_IO_ERROR => Some("Unknown std::io::Error"), SEC_RANDOM_FAILED => Some("SecRandomCopyBytes: call failed"), RTL_GEN_RANDOM_FAILED => Some("RtlGenRandom: call failed"), diff --git a/src/util_libc.rs b/src/util_libc.rs index 89c610c3..015d1a04 100644 --- a/src/util_libc.rs +++ b/src/util_libc.rs @@ -5,6 +5,7 @@ // , at your // option. This file may not be copied, modified, or distributed // except according to those terms. +use crate::error::ERRNO_NOT_POSITIVE; use crate::util::LazyUsize; use crate::Error; use core::num::NonZeroU32; @@ -25,7 +26,12 @@ cfg_if! { } pub fn last_os_error() -> Error { - Error::from(NonZeroU32::new(unsafe { *errno_location() } as u32).unwrap()) + let errno = unsafe { *errno_location() }; + if errno > 0 { + Error::from(NonZeroU32::new(errno as u32).unwrap()) + } else { + ERRNO_NOT_POSITIVE + } } // Fill a buffer by repeatedly invoking a system call. The `sys_fill` function: