Skip to content

Commit 5eeb89c

Browse files
committed
Move Error constants into the impl Error
This makes it easier to export Error constants in later commits
1 parent be56f20 commit 5eeb89c

8 files changed

+46
-50
lines changed

src/error.rs

Lines changed: 33 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,33 @@ use core::num::NonZeroU32;
1919
#[derive(Copy, Clone, Eq, PartialEq)]
2020
pub struct Error(NonZeroU32);
2121

22+
// TODO: Convert to a function when min_version >= 1.33
23+
macro_rules! internal_error {
24+
($n:expr) => {
25+
Error(unsafe { NonZeroU32::new_unchecked(Error::INTERNAL_START + $n as u16 as u32) })
26+
};
27+
}
28+
2229
impl Error {
30+
/// Error constants
31+
pub(crate) const UNSUPPORTED: Error = internal_error!(0);
32+
pub(crate) const ERRNO_NOT_POSITIVE: Error = internal_error!(1);
33+
pub(crate) const UNKNOWN_IO_ERROR: Error = internal_error!(2);
34+
pub(crate) const SEC_RANDOM_FAILED: Error = internal_error!(3);
35+
pub(crate) const RTL_GEN_RANDOM_FAILED: Error = internal_error!(4);
36+
pub(crate) const FAILED_RDRAND: Error = internal_error!(5);
37+
pub(crate) const NO_RDRAND: Error = internal_error!(6);
38+
pub(crate) const BINDGEN_CRYPTO_UNDEF: Error = internal_error!(7);
39+
pub(crate) const BINDGEN_GRV_UNDEF: Error = internal_error!(8);
40+
pub(crate) const STDWEB_NO_RNG: Error = internal_error!(9);
41+
pub(crate) const STDWEB_RNG_FAILED: Error = internal_error!(10);
42+
2343
#[deprecated(since = "0.1.7")]
2444
/// Unknown error.
25-
pub const UNKNOWN: Error = UNSUPPORTED;
45+
pub const UNKNOWN: Error = Error::UNSUPPORTED;
2646
#[deprecated(since = "0.1.7")]
2747
/// System entropy source is unavailable.
28-
pub const UNAVAILABLE: Error = UNSUPPORTED;
48+
pub const UNAVAILABLE: Error = Error::UNSUPPORTED;
2949

3050
/// Codes below this point represent OS Errors (i.e. positive i32 values).
3151
/// Codes at or above this point, but below [`Error::CUSTOM_START`] are
@@ -126,39 +146,19 @@ impl From<NonZeroU32> for Error {
126146
}
127147
}
128148

129-
// TODO: Convert to a function when min_version >= 1.33
130-
macro_rules! internal_error {
131-
($n:expr) => {
132-
Error(unsafe { NonZeroU32::new_unchecked(Error::INTERNAL_START + $n as u16 as u32) })
133-
};
134-
}
135-
136-
/// Internal Error constants
137-
pub(crate) const UNSUPPORTED: Error = internal_error!(0);
138-
pub(crate) const ERRNO_NOT_POSITIVE: Error = internal_error!(1);
139-
pub(crate) const UNKNOWN_IO_ERROR: Error = internal_error!(2);
140-
pub(crate) const SEC_RANDOM_FAILED: Error = internal_error!(3);
141-
pub(crate) const RTL_GEN_RANDOM_FAILED: Error = internal_error!(4);
142-
pub(crate) const FAILED_RDRAND: Error = internal_error!(5);
143-
pub(crate) const NO_RDRAND: Error = internal_error!(6);
144-
pub(crate) const BINDGEN_CRYPTO_UNDEF: Error = internal_error!(7);
145-
pub(crate) const BINDGEN_GRV_UNDEF: Error = internal_error!(8);
146-
pub(crate) const STDWEB_NO_RNG: Error = internal_error!(9);
147-
pub(crate) const STDWEB_RNG_FAILED: Error = internal_error!(10);
148-
149149
fn internal_desc(error: Error) -> Option<&'static str> {
150150
match error {
151-
UNSUPPORTED => Some("getrandom: this target is not supported"),
152-
ERRNO_NOT_POSITIVE => Some("errno: did not return a positive value"),
153-
UNKNOWN_IO_ERROR => Some("Unknown std::io::Error"),
154-
SEC_RANDOM_FAILED => Some("SecRandomCopyBytes: call failed"),
155-
RTL_GEN_RANDOM_FAILED => Some("RtlGenRandom: call failed"),
156-
FAILED_RDRAND => Some("RDRAND: failed multiple times: CPU issue likely"),
157-
NO_RDRAND => Some("RDRAND: instruction not supported"),
158-
BINDGEN_CRYPTO_UNDEF => Some("wasm-bindgen: self.crypto is undefined"),
159-
BINDGEN_GRV_UNDEF => Some("wasm-bindgen: crypto.getRandomValues is undefined"),
160-
STDWEB_NO_RNG => Some("stdweb: no randomness source available"),
161-
STDWEB_RNG_FAILED => Some("stdweb: failed to get randomness"),
151+
Error::UNSUPPORTED => Some("getrandom: this target is not supported"),
152+
Error::ERRNO_NOT_POSITIVE => Some("errno: did not return a positive value"),
153+
Error::UNKNOWN_IO_ERROR => Some("Unknown std::io::Error"),
154+
Error::SEC_RANDOM_FAILED => Some("SecRandomCopyBytes: call failed"),
155+
Error::RTL_GEN_RANDOM_FAILED => Some("RtlGenRandom: call failed"),
156+
Error::FAILED_RDRAND => Some("RDRAND: failed multiple times: CPU issue likely"),
157+
Error::NO_RDRAND => Some("RDRAND: instruction not supported"),
158+
Error::BINDGEN_CRYPTO_UNDEF => Some("wasm-bindgen: self.crypto is undefined"),
159+
Error::BINDGEN_GRV_UNDEF => Some("wasm-bindgen: crypto.getRandomValues is undefined"),
160+
Error::STDWEB_NO_RNG => Some("stdweb: no randomness source available"),
161+
Error::STDWEB_RNG_FAILED => Some("stdweb: failed to get randomness"),
162162
_ => None,
163163
}
164164
}

src/error_impls.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
// except according to those terms.
88
extern crate std;
99

10-
use crate::{error::UNKNOWN_IO_ERROR, Error};
10+
use crate::Error;
1111
use core::convert::From;
1212
use core::num::NonZeroU32;
1313
use std::io;
@@ -19,7 +19,7 @@ impl From<io::Error> for Error {
1919
return Error::from(code);
2020
}
2121
}
22-
UNKNOWN_IO_ERROR
22+
Error::UNKNOWN_IO_ERROR
2323
}
2424
}
2525

src/ios.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
// except according to those terms.
88

99
//! Implementation for iOS
10-
use crate::{error::SEC_RANDOM_FAILED, Error};
10+
use crate::Error;
1111

1212
// TODO: Make extern once extern_types feature is stabilized. See:
1313
// https://github.com/rust-lang/rust/issues/43467
@@ -24,7 +24,7 @@ extern "C" {
2424
pub fn getrandom_inner(dest: &mut [u8]) -> Result<(), Error> {
2525
let ret = unsafe { SecRandomCopyBytes(kSecRandomDefault, dest.len(), dest.as_mut_ptr()) };
2626
if ret == -1 {
27-
Err(SEC_RANDOM_FAILED)
27+
Err(Error::SEC_RANDOM_FAILED)
2828
} else {
2929
Ok(())
3030
}

src/rdrand.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
// except according to those terms.
88

99
//! Implementation for SGX using RDRAND instruction
10-
use crate::error::{FAILED_RDRAND, NO_RDRAND};
1110
#[cfg(not(target_feature = "rdrand"))]
1211
use crate::util::LazyBool;
1312
use crate::Error;
@@ -37,7 +36,7 @@ unsafe fn rdrand() -> Result<[u8; WORD_SIZE], Error> {
3736
// Keep looping in case this was a false positive.
3837
}
3938
}
40-
Err(FAILED_RDRAND)
39+
Err(Error::FAILED_RDRAND)
4140
}
4241

4342
// "rdrand" target feature requires "+rdrnd" flag, see https://github.com/rust-lang/rust/issues/49653.
@@ -64,7 +63,7 @@ fn is_rdrand_supported() -> bool {
6463

6564
pub fn getrandom_inner(dest: &mut [u8]) -> Result<(), Error> {
6665
if !is_rdrand_supported() {
67-
return Err(NO_RDRAND);
66+
return Err(Error::NO_RDRAND);
6867
}
6968

7069
// SAFETY: After this point, rdrand is supported, so calling the rdrand

src/util_libc.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
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;
98
use crate::util::LazyUsize;
109
use crate::Error;
1110
use core::num::NonZeroU32;
@@ -30,7 +29,7 @@ pub fn last_os_error() -> Error {
3029
if errno > 0 {
3130
Error::from(NonZeroU32::new(errno as u32).unwrap())
3231
} else {
33-
ERRNO_NOT_POSITIVE
32+
Error::ERRNO_NOT_POSITIVE
3433
}
3534
}
3635

src/wasm32_bindgen.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ use std::thread_local;
1515

1616
use wasm_bindgen::prelude::*;
1717

18-
use crate::error::{BINDGEN_CRYPTO_UNDEF, BINDGEN_GRV_UNDEF};
1918
use crate::Error;
2019

2120
#[derive(Clone, Debug)]
@@ -66,13 +65,13 @@ fn getrandom_init() -> Result<RngSource, Error> {
6665

6766
let crypto = self_.crypto();
6867
if crypto.is_undefined() {
69-
return Err(BINDGEN_CRYPTO_UNDEF);
68+
return Err(Error::BINDGEN_CRYPTO_UNDEF);
7069
}
7170

7271
// Test if `crypto.getRandomValues` is undefined as well
7372
let crypto: BrowserCrypto = crypto.into();
7473
if crypto.get_random_values_fn().is_undefined() {
75-
return Err(BINDGEN_GRV_UNDEF);
74+
return Err(Error::BINDGEN_GRV_UNDEF);
7675
}
7776

7877
return Ok(RngSource::Browser(crypto));

src/wasm32_stdweb.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ use stdweb::js;
1515
use stdweb::unstable::TryInto;
1616
use stdweb::web::error::Error as WebError;
1717

18-
use crate::error::{STDWEB_NO_RNG, STDWEB_RNG_FAILED};
1918
use crate::Error;
2019
use std::sync::Once;
2120

@@ -71,7 +70,7 @@ fn getrandom_init() -> Result<RngSource, Error> {
7170
} else {
7271
let _err: WebError = js! { return @{ result }.error }.try_into().unwrap();
7372
error!("getrandom unavailable: {}", _err);
74-
Err(STDWEB_NO_RNG)
73+
Err(Error::STDWEB_NO_RNG)
7574
}
7675
}
7776

@@ -107,7 +106,7 @@ fn getrandom_fill(source: RngSource, dest: &mut [u8]) -> Result<(), Error> {
107106
if js! { return @{ result.as_ref() }.success } != true {
108107
let _err: WebError = js! { return @{ result }.error }.try_into().unwrap();
109108
error!("getrandom failed: {}", _err);
110-
return Err(STDWEB_RNG_FAILED);
109+
return Err(Error::STDWEB_RNG_FAILED);
111110
}
112111
}
113112
Ok(())

src/windows.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
// except according to those terms.
88

99
//! Implementation for Windows
10-
use crate::{error::RTL_GEN_RANDOM_FAILED, Error};
10+
use crate::Error;
1111

1212
extern "system" {
1313
#[link_name = "SystemFunction036"]
@@ -19,7 +19,7 @@ pub fn getrandom_inner(dest: &mut [u8]) -> Result<(), Error> {
1919
for chunk in dest.chunks_mut(u32::max_value() as usize) {
2020
let ret = unsafe { RtlGenRandom(chunk.as_mut_ptr(), chunk.len() as u32) };
2121
if ret == 0 {
22-
return Err(RTL_GEN_RANDOM_FAILED);
22+
return Err(Error::RTL_GEN_RANDOM_FAILED);
2323
}
2424
}
2525
Ok(())

0 commit comments

Comments
 (0)