@@ -19,13 +19,36 @@ use core::num::NonZeroU32;
1919#[ derive( Copy , Clone , Eq , PartialEq ) ]
2020pub 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+
2229impl Error {
23- #[ deprecated( since = "0.1.7" ) ]
24- /// Unknown error.
25- pub const UNKNOWN : Error = UNSUPPORTED ;
26- #[ deprecated( since = "0.1.7" ) ]
27- /// System entropy source is unavailable.
28- pub const UNAVAILABLE : Error = UNSUPPORTED ;
30+ /// This target/platform is not supported by `getrandom`.
31+ pub const UNSUPPORTED : Error = internal_error ! ( 0 ) ;
32+ /// The platform-specific `errno` returned a non-positive value.
33+ pub const ERRNO_NOT_POSITIVE : Error = internal_error ! ( 1 ) ;
34+ /// Call to iOS [`SecRandomCopyBytes`](https://developer.apple.com/documentation/security/1399291-secrandomcopybytes) failed.
35+ pub const IOS_SEC_RANDOM : Error = internal_error ! ( 3 ) ;
36+ /// Call to Windows [`RtlGenRandom`](https://docs.microsoft.com/en-us/windows/win32/api/ntsecapi/nf-ntsecapi-rtlgenrandom) failed.
37+ pub const WINDOWS_RTL_GEN_RANDOM : Error = internal_error ! ( 4 ) ;
38+ /// RDRAND instruction failed due to a hardware issue.
39+ pub const FAILED_RDRAND : Error = internal_error ! ( 5 ) ;
40+ /// RDRAND instruction unsupported on this target.
41+ pub const NO_RDRAND : Error = internal_error ! ( 6 ) ;
42+ /// Using `wasm-bindgen`, browser does not support `self.crypto`.
43+ pub const BINDGEN_CRYPTO_UNDEF : Error = internal_error ! ( 7 ) ;
44+ /// Using `wasm-bindgen`, browser does not support `crypto.getRandomValues`.
45+ pub const BINDGEN_GRV_UNDEF : Error = internal_error ! ( 8 ) ;
46+ /// Using `stdweb`, no cryptographic RNG is available.
47+ pub const STDWEB_NO_RNG : Error = internal_error ! ( 9 ) ;
48+ /// Using `stdweb`, invoking a cryptographic RNG failed.
49+ pub const STDWEB_RNG_FAILED : Error = internal_error ! ( 10 ) ;
50+ /// On VxWorks, call to `randSecure` failed (random number generator is not yet initialized).
51+ pub const VXWORKS_RAND_SECURE : Error = internal_error ! ( 11 ) ;
2952
3053 /// Codes below this point represent OS Errors (i.e. positive i32 values).
3154 /// Codes at or above this point, but below [`Error::CUSTOM_START`] are
@@ -38,9 +61,11 @@ impl Error {
3861
3962 /// Extract the raw OS error code (if this error came from the OS)
4063 ///
41- /// This method is identical to `std::io::Error::raw_os_error()`, except
64+ /// This method is identical to [ `std::io::Error::raw_os_error()`][1] , except
4265 /// that it works in `no_std` contexts. If this method returns `None`, the
4366 /// error value can still be formatted via the `Display` implementation.
67+ ///
68+ /// [1]: https://doc.rust-lang.org/std/io/struct.Error.html#method.raw_os_error
4469 #[ inline]
4570 pub fn raw_os_error ( self ) -> Option < i32 > {
4671 if self . 0 . get ( ) < Self :: INTERNAL_START {
@@ -126,41 +151,19 @@ impl From<NonZeroU32> for Error {
126151 }
127152}
128153
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- pub ( crate ) const RAND_SECURE_FATAL : Error = internal_error ! ( 11 ) ;
149-
150154fn internal_desc ( error : Error ) -> Option < & ' static str > {
151155 match error {
152- UNSUPPORTED => Some ( "getrandom: this target is not supported" ) ,
153- ERRNO_NOT_POSITIVE => Some ( "errno: did not return a positive value" ) ,
154- UNKNOWN_IO_ERROR => Some ( "Unknown std::io::Error" ) ,
155- SEC_RANDOM_FAILED => Some ( "SecRandomCopyBytes: call failed" ) ,
156- RTL_GEN_RANDOM_FAILED => Some ( "RtlGenRandom: call failed" ) ,
157- FAILED_RDRAND => Some ( "RDRAND: failed multiple times: CPU issue likely" ) ,
158- NO_RDRAND => Some ( "RDRAND: instruction not supported" ) ,
159- BINDGEN_CRYPTO_UNDEF => Some ( "wasm-bindgen: self.crypto is undefined" ) ,
160- BINDGEN_GRV_UNDEF => Some ( "wasm-bindgen: crypto.getRandomValues is undefined" ) ,
161- STDWEB_NO_RNG => Some ( "stdweb: no randomness source available" ) ,
162- STDWEB_RNG_FAILED => Some ( "stdweb: failed to get randomness" ) ,
163- RAND_SECURE_FATAL => Some ( "randSecure: random number generator module is not initialized" ) ,
156+ Error :: UNSUPPORTED => Some ( "getrandom: this target is not supported" ) ,
157+ Error :: ERRNO_NOT_POSITIVE => Some ( "errno: did not return a positive value" ) ,
158+ Error :: IOS_SEC_RANDOM => Some ( "SecRandomCopyBytes: iOS Secuirty framework failure" ) ,
159+ Error :: WINDOWS_RTL_GEN_RANDOM => Some ( "RtlGenRandom: Windows system function failure" ) ,
160+ Error :: FAILED_RDRAND => Some ( "RDRAND: failed multiple times: CPU issue likely" ) ,
161+ Error :: NO_RDRAND => Some ( "RDRAND: instruction not supported" ) ,
162+ Error :: BINDGEN_CRYPTO_UNDEF => Some ( "wasm-bindgen: self.crypto is undefined" ) ,
163+ Error :: BINDGEN_GRV_UNDEF => Some ( "wasm-bindgen: crypto.getRandomValues is undefined" ) ,
164+ Error :: STDWEB_NO_RNG => Some ( "stdweb: no randomness source available" ) ,
165+ Error :: STDWEB_RNG_FAILED => Some ( "stdweb: failed to get randomness" ) ,
166+ Error :: VXWORKS_RAND_SECURE => Some ( "randSecure: VxWorks RNG module is not initialized" ) ,
164167 _ => None ,
165168 }
166169}
0 commit comments