@@ -20,44 +20,38 @@ use core::{fmt, num::NonZeroU32};
20
20
#[ derive( Copy , Clone , Eq , PartialEq ) ]
21
21
pub struct Error ( NonZeroU32 ) ;
22
22
23
- const fn internal_error ( n : u16 ) -> Error {
24
- // SAFETY: code > 0 as INTERNAL_START > 0 and adding n won't overflow a u32.
25
- let code = Error :: INTERNAL_START + ( n as u32 ) ;
26
- Error ( unsafe { NonZeroU32 :: new_unchecked ( code) } )
27
- }
28
-
29
23
impl Error {
30
24
/// This target/platform is not supported by `getrandom`.
31
- pub const UNSUPPORTED : Error = internal_error ( 0 ) ;
25
+ pub const UNSUPPORTED : Error = Self :: new_internal ( 0 ) ;
32
26
/// The platform-specific `errno` returned a non-positive value.
33
- pub const ERRNO_NOT_POSITIVE : Error = internal_error ( 1 ) ;
27
+ pub const ERRNO_NOT_POSITIVE : Error = Self :: new_internal ( 1 ) ;
34
28
/// Encountered an unexpected situation which should not happen in practice.
35
- pub const UNEXPECTED : Error = internal_error ( 2 ) ;
29
+ pub const UNEXPECTED : Error = Self :: new_internal ( 2 ) ;
36
30
/// Call to [`CCRandomGenerateBytes`](https://opensource.apple.com/source/CommonCrypto/CommonCrypto-60074/include/CommonRandom.h.auto.html) failed
37
31
/// on iOS, tvOS, or waatchOS.
38
32
// TODO: Update this constant name in the next breaking release.
39
- pub const IOS_SEC_RANDOM : Error = internal_error ( 3 ) ;
33
+ pub const IOS_SEC_RANDOM : Error = Self :: new_internal ( 3 ) ;
40
34
/// Call to Windows [`RtlGenRandom`](https://docs.microsoft.com/en-us/windows/win32/api/ntsecapi/nf-ntsecapi-rtlgenrandom) failed.
41
- pub const WINDOWS_RTL_GEN_RANDOM : Error = internal_error ( 4 ) ;
35
+ pub const WINDOWS_RTL_GEN_RANDOM : Error = Self :: new_internal ( 4 ) ;
42
36
/// RDRAND instruction failed due to a hardware issue.
43
- pub const FAILED_RDRAND : Error = internal_error ( 5 ) ;
37
+ pub const FAILED_RDRAND : Error = Self :: new_internal ( 5 ) ;
44
38
/// RDRAND instruction unsupported on this target.
45
- pub const NO_RDRAND : Error = internal_error ( 6 ) ;
39
+ pub const NO_RDRAND : Error = Self :: new_internal ( 6 ) ;
46
40
/// The environment does not support the Web Crypto API.
47
- pub const WEB_CRYPTO : Error = internal_error ( 7 ) ;
41
+ pub const WEB_CRYPTO : Error = Self :: new_internal ( 7 ) ;
48
42
/// Calling Web Crypto API `crypto.getRandomValues` failed.
49
- pub const WEB_GET_RANDOM_VALUES : Error = internal_error ( 8 ) ;
43
+ pub const WEB_GET_RANDOM_VALUES : Error = Self :: new_internal ( 8 ) ;
50
44
/// On VxWorks, call to `randSecure` failed (random number generator is not yet initialized).
51
- pub const VXWORKS_RAND_SECURE : Error = internal_error ( 11 ) ;
45
+ pub const VXWORKS_RAND_SECURE : Error = Self :: new_internal ( 11 ) ;
52
46
/// Node.js does not have the `crypto` CommonJS module.
53
- pub const NODE_CRYPTO : Error = internal_error ( 12 ) ;
47
+ pub const NODE_CRYPTO : Error = Self :: new_internal ( 12 ) ;
54
48
/// Calling Node.js function `crypto.randomFillSync` failed.
55
- pub const NODE_RANDOM_FILL_SYNC : Error = internal_error ( 13 ) ;
49
+ pub const NODE_RANDOM_FILL_SYNC : Error = Self :: new_internal ( 13 ) ;
56
50
/// Called from an ES module on Node.js. This is unsupported, see:
57
51
/// <https://docs.rs/getrandom#nodejs-es-module-support>.
58
- pub const NODE_ES_MODULE : Error = internal_error ( 14 ) ;
52
+ pub const NODE_ES_MODULE : Error = Self :: new_internal ( 14 ) ;
59
53
/// Calling Windows ProcessPrng failed.
60
- pub const WINDOWS_PROCESS_PRNG : Error = internal_error ( 15 ) ;
54
+ pub const WINDOWS_PROCESS_PRNG : Error = Self :: new_internal ( 15 ) ;
61
55
62
56
/// Codes below this point represent OS Errors (i.e. positive i32 values).
63
57
/// Codes at or above this point, but below [`Error::CUSTOM_START`] are
@@ -108,13 +102,18 @@ impl Error {
108
102
}
109
103
}
110
104
111
- /// Extract the bare error code.
112
- ///
113
- /// This code can either come from the underlying OS, or be a custom error.
114
- /// Use [`Error::raw_os_error()`] to disambiguate.
115
- #[ inline]
116
- pub const fn code ( self ) -> NonZeroU32 {
117
- self . 0
105
+ /// Creates a new instance of an `Error` from a particular custom error code.
106
+ pub const fn new_custom ( n : u16 ) -> Error {
107
+ // SAFETY: code > 0 as CUSTOM_START > 0 and adding n won't overflow a u32.
108
+ let code = Error :: CUSTOM_START + ( n as u32 ) ;
109
+ Error ( unsafe { NonZeroU32 :: new_unchecked ( code) } )
110
+ }
111
+
112
+ /// Creates a new instance of an `Error` from a particular internal error code.
113
+ const fn new_internal ( n : u16 ) -> Error {
114
+ // SAFETY: code > 0 as INTERNAL_START > 0 and adding n won't overflow a u32.
115
+ let code = Error :: INTERNAL_START + ( n as u32 ) ;
116
+ Error ( unsafe { NonZeroU32 :: new_unchecked ( code) } )
118
117
}
119
118
}
120
119
@@ -153,12 +152,6 @@ impl fmt::Display for Error {
153
152
}
154
153
}
155
154
156
- impl From < NonZeroU32 > for Error {
157
- fn from ( code : NonZeroU32 ) -> Self {
158
- Self ( code)
159
- }
160
- }
161
-
162
155
fn internal_desc ( error : Error ) -> Option < & ' static str > {
163
156
match error {
164
157
Error :: UNSUPPORTED => Some ( "getrandom: this target is not supported" ) ,
0 commit comments