Skip to content

Commit 387f2e7

Browse files
committed
Address review feedback
1 parent c6452d3 commit 387f2e7

File tree

2 files changed

+32
-15
lines changed

2 files changed

+32
-15
lines changed

src/error.rs

+25-14
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@ use core::num::NonZeroU32;
2020
pub struct Error(NonZeroU32);
2121

2222
impl Error {
23+
#[deprecated(since = "0.1.7")]
24+
pub const UNKNOWN: Error = UNSUPPORTED;
25+
#[deprecated(since = "0.1.7")]
26+
pub const UNAVAILABLE: Error = UNSUPPORTED;
27+
2328
/// Codes below this point represent OS Errors (i.e. positive i32 values).
2429
/// Codes at or above this point, but below [`Error::CUSTOM_START`] are
2530
/// reserved for use by the `rand` and `getrandom` crates.
@@ -111,25 +116,31 @@ impl From<NonZeroU32> for Error {
111116
}
112117
}
113118

114-
/// Internal Error constants
115-
pub(crate) const UNSUPPORTED: Error = internal_error(0);
116-
pub(crate) const UNKNOWN_IO_ERROR: Error = internal_error(1);
117-
pub(crate) const SEC_RANDOM_FAILED: Error = internal_error(2);
118-
pub(crate) const RTL_GEN_RANDOM_FAILED: Error = internal_error(3);
119-
pub(crate) const FAILED_RDRAND: Error = internal_error(4);
120-
pub(crate) const NO_RDRAND: Error = internal_error(5);
121-
pub(crate) const BINDGEN_CRYPTO_UNDEF: Error = internal_error(6);
122-
pub(crate) const BINDGEN_GRV_UNDEF: Error = internal_error(7);
123-
pub(crate) const STDWEB_NO_RNG: Error = internal_error(8);
124-
pub(crate) const STDWEB_RNG_FAILED: Error = internal_error(9);
125-
126-
const fn internal_error(n: u16) -> Error {
127-
Error(unsafe { NonZeroU32::new_unchecked(Error::INTERNAL_START + n as u32) })
119+
// TODO: Convert to a function when min_version >= 1.33
120+
macro_rules! internal_error {
121+
($code:expr) => {{
122+
let n: u16 = $code;
123+
Error(unsafe { NonZeroU32::new_unchecked(Error::INTERNAL_START + n as u32) })
124+
}};
128125
}
129126

127+
/// Internal Error constants
128+
pub(crate) const UNSUPPORTED: Error = internal_error!(0);
129+
pub(crate) const ERRNO_NOT_POSITIVE: Error = internal_error!(1);
130+
pub(crate) const UNKNOWN_IO_ERROR: Error = internal_error!(2);
131+
pub(crate) const SEC_RANDOM_FAILED: Error = internal_error!(3);
132+
pub(crate) const RTL_GEN_RANDOM_FAILED: Error = internal_error!(4);
133+
pub(crate) const FAILED_RDRAND: Error = internal_error!(5);
134+
pub(crate) const NO_RDRAND: Error = internal_error!(6);
135+
pub(crate) const BINDGEN_CRYPTO_UNDEF: Error = internal_error!(7);
136+
pub(crate) const BINDGEN_GRV_UNDEF: Error = internal_error!(8);
137+
pub(crate) const STDWEB_NO_RNG: Error = internal_error!(9);
138+
pub(crate) const STDWEB_RNG_FAILED: Error = internal_error!(10);
139+
130140
fn internal_desc(error: Error) -> Option<&'static str> {
131141
match error {
132142
UNSUPPORTED => Some("getrandom: this target is not supported"),
143+
ERRNO_NOT_POSITIVE => Some("errno: did not return a positive value"),
133144
UNKNOWN_IO_ERROR => Some("Unknown std::io::Error"),
134145
SEC_RANDOM_FAILED => Some("SecRandomCopyBytes: call failed"),
135146
RTL_GEN_RANDOM_FAILED => Some("RtlGenRandom: call failed"),

src/util_libc.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
// <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
66
// option. This file may not be copied, modified, or distributed
77
// except according to those terms.
8+
use crate::error::ERRNO_NOT_POSITIVE;
89
use crate::util::LazyUsize;
910
use crate::Error;
1011
use core::num::NonZeroU32;
@@ -25,7 +26,12 @@ cfg_if! {
2526
}
2627

2728
pub fn last_os_error() -> Error {
28-
Error::from(NonZeroU32::new(unsafe { *errno_location() } as u32).unwrap())
29+
let errno = unsafe { *errno_location() };
30+
if errno > 0 {
31+
Error::from(NonZeroU32::new(errno as u32).unwrap())
32+
} else {
33+
ERRNO_NOT_POSITIVE
34+
}
2935
}
3036

3137
// Fill a buffer by repeatedly invoking a system call. The `sys_fill` function:

0 commit comments

Comments
 (0)