@@ -20,44 +20,38 @@ use core::{fmt, num::NonZeroU32};
2020#[ derive( Copy , Clone , Eq , PartialEq ) ]
2121pub struct Error ( NonZeroU32 ) ;
2222
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-
2923impl Error {
3024 /// 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 ) ;
3226 /// 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 ) ;
3428 /// 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 ) ;
3630 /// Call to [`CCRandomGenerateBytes`](https://opensource.apple.com/source/CommonCrypto/CommonCrypto-60074/include/CommonRandom.h.auto.html) failed
3731 /// on iOS, tvOS, or waatchOS.
3832 // 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 ) ;
4034 /// 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 ) ;
4236 /// 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 ) ;
4438 /// 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 ) ;
4640 /// 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 ) ;
4842 /// 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 ) ;
5044 /// 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 ) ;
5246 /// 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 ) ;
5448 /// 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 ) ;
5650 /// Called from an ES module on Node.js. This is unsupported, see:
5751 /// <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 ) ;
5953 /// 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 ) ;
6155
6256 /// Codes below this point represent OS Errors (i.e. positive i32 values).
6357 /// Codes at or above this point, but below [`Error::CUSTOM_START`] are
@@ -108,13 +102,18 @@ impl Error {
108102 }
109103 }
110104
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) } )
118117 }
119118}
120119
@@ -153,12 +152,6 @@ impl fmt::Display for Error {
153152 }
154153}
155154
156- impl From < NonZeroU32 > for Error {
157- fn from ( code : NonZeroU32 ) -> Self {
158- Self ( code)
159- }
160- }
161-
162155fn internal_desc ( error : Error ) -> Option < & ' static str > {
163156 match error {
164157 Error :: UNSUPPORTED => Some ( "getrandom: this target is not supported" ) ,
0 commit comments