Skip to content

Commit

Permalink
Address review feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
josephlr committed Jul 27, 2019
1 parent c6452d3 commit 387f2e7
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 15 deletions.
39 changes: 25 additions & 14 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -111,25 +116,31 @@ impl From<NonZeroU32> 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"),
Expand Down
8 changes: 7 additions & 1 deletion src/util_libc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
// <LICENSE-MIT or https://opensource.org/licenses/MIT>, 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;
Expand All @@ -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:
Expand Down

0 comments on commit 387f2e7

Please sign in to comment.