@@ -27,26 +27,6 @@ impl Error {
2727 pub const ERRNO_NOT_POSITIVE : Error = Self :: new_internal ( 1 ) ;
2828 /// Encountered an unexpected situation which should not happen in practice.
2929 pub const UNEXPECTED : Error = Self :: new_internal ( 2 ) ;
30- /// Call to [`CCRandomGenerateBytes`](https://opensource.apple.com/source/CommonCrypto/CommonCrypto-60074/include/CommonRandom.h.auto.html) failed
31- /// on iOS, tvOS, or waatchOS.
32- // TODO: Update this constant name in the next breaking release.
33- pub const IOS_SEC_RANDOM : Error = Self :: new_internal ( 3 ) ;
34- /// Call to Windows [`RtlGenRandom`](https://docs.microsoft.com/en-us/windows/win32/api/ntsecapi/nf-ntsecapi-rtlgenrandom) failed.
35- pub const WINDOWS_RTL_GEN_RANDOM : Error = Self :: new_internal ( 4 ) ;
36- /// RDRAND instruction failed due to a hardware issue.
37- pub const FAILED_RDRAND : Error = Self :: new_internal ( 5 ) ;
38- /// RDRAND instruction unsupported on this target.
39- pub const NO_RDRAND : Error = Self :: new_internal ( 6 ) ;
40- /// The environment does not support the Web Crypto API.
41- pub const WEB_CRYPTO : Error = Self :: new_internal ( 7 ) ;
42- /// On VxWorks, call to `randSecure` failed (random number generator is not yet initialized).
43- pub const VXWORKS_RAND_SECURE : Error = Self :: new_internal ( 11 ) ;
44- /// Calling Windows ProcessPrng failed.
45- pub const WINDOWS_PROCESS_PRNG : Error = Self :: new_internal ( 12 ) ;
46- /// RNDR register read failed due to a hardware issue.
47- pub const RNDR_FAILURE : Error = Self :: new_internal ( 13 ) ;
48- /// RNDR register is not supported on this target.
49- pub const RNDR_NOT_AVAILABLE : Error = Self :: new_internal ( 14 ) ;
5030
5131 /// Codes below this point represent OS Errors (i.e. positive i32 values).
5232 /// Codes at or above this point, but below [`Error::CUSTOM_START`] are
@@ -101,11 +81,52 @@ impl Error {
10181 }
10282
10383 /// Creates a new instance of an `Error` from a particular internal error code.
104- const fn new_internal ( n : u16 ) -> Error {
84+ pub ( crate ) const fn new_internal ( n : u16 ) -> Error {
10585 // SAFETY: code > 0 as INTERNAL_START > 0 and adding n won't overflow a u32.
10686 let code = Error :: INTERNAL_START + ( n as u32 ) ;
10787 Error ( unsafe { NonZeroU32 :: new_unchecked ( code) } )
10888 }
89+
90+ fn internal_desc ( & self ) -> Option < & ' static str > {
91+ let desc = match * self {
92+ Error :: UNSUPPORTED => "getrandom: this target is not supported" ,
93+ Error :: ERRNO_NOT_POSITIVE => "errno: did not return a positive value" ,
94+ Error :: UNEXPECTED => "unexpected situation" ,
95+ #[ cfg( any(
96+ target_os = "ios" ,
97+ target_os = "visionos" ,
98+ target_os = "watchos" ,
99+ target_os = "tvos" ,
100+ ) ) ]
101+ Error :: IOS_RANDOM_GEN => "SecRandomCopyBytes: iOS Security framework failure" ,
102+ #[ cfg( all( windows, not( target_vendor = "win7" ) ) ) ]
103+ Error :: WINDOWS_PROCESS_PRNG => "ProcessPrng: Windows system function failure" ,
104+ #[ cfg( all( windows, target_vendor = "win7" ) ) ]
105+ Error :: WINDOWS_RTL_GEN_RANDOM => "RtlGenRandom: Windows system function failure" ,
106+ #[ cfg( getrandom_backend = "wasm_js" ) ]
107+ Error :: WEB_CRYPTO => "Web Crypto API is unavailable" ,
108+ #[ cfg( target_os = "vxworks" ) ]
109+ Error :: VXWORKS_RAND_SECURE => "randSecure: VxWorks RNG module is not initialized" ,
110+
111+ #[ cfg( any(
112+ getrandom_backend = "rdrand" ,
113+ all( target_arch = "x86_64" , target_env = "sgx" )
114+ ) ) ]
115+ Error :: FAILED_RDRAND => "RDRAND: failed multiple times: CPU issue likely" ,
116+ #[ cfg( any(
117+ getrandom_backend = "rdrand" ,
118+ all( target_arch = "x86_64" , target_env = "sgx" )
119+ ) ) ]
120+ Error :: NO_RDRAND => "RDRAND: instruction not supported" ,
121+
122+ #[ cfg( getrandom_backend = "rndr" ) ]
123+ Error :: RNDR_FAILURE => "RNDR: Could not generate a random number" ,
124+ #[ cfg( getrandom_backend = "rndr" ) ]
125+ Error :: RNDR_NOT_AVAILABLE => "RNDR: Register not supported" ,
126+ _ => return None ,
127+ } ;
128+ Some ( desc)
129+ }
109130}
110131
111132impl fmt:: Debug for Error {
@@ -115,7 +136,7 @@ impl fmt::Debug for Error {
115136 dbg. field ( "os_error" , & errno) ;
116137 #[ cfg( feature = "std" ) ]
117138 dbg. field ( "description" , & std:: io:: Error :: from_raw_os_error ( errno) ) ;
118- } else if let Some ( desc) = internal_desc ( * self ) {
139+ } else if let Some ( desc) = self . internal_desc ( ) {
119140 dbg. field ( "internal_code" , & self . 0 . get ( ) ) ;
120141 dbg. field ( "description" , & desc) ;
121142 } else {
@@ -135,33 +156,14 @@ impl fmt::Display for Error {
135156 write!( f, "OS Error: {}" , errno)
136157 }
137158 }
138- } else if let Some ( desc) = internal_desc ( * self ) {
159+ } else if let Some ( desc) = self . internal_desc ( ) {
139160 f. write_str ( desc)
140161 } else {
141162 write ! ( f, "Unknown Error: {}" , self . 0 . get( ) )
142163 }
143164 }
144165}
145166
146- fn internal_desc ( error : Error ) -> Option < & ' static str > {
147- let desc = match error {
148- Error :: UNSUPPORTED => "getrandom: this target is not supported" ,
149- Error :: ERRNO_NOT_POSITIVE => "errno: did not return a positive value" ,
150- Error :: UNEXPECTED => "unexpected situation" ,
151- Error :: IOS_SEC_RANDOM => "SecRandomCopyBytes: iOS Security framework failure" ,
152- Error :: WINDOWS_RTL_GEN_RANDOM => "RtlGenRandom: Windows system function failure" ,
153- Error :: FAILED_RDRAND => "RDRAND: failed multiple times: CPU issue likely" ,
154- Error :: NO_RDRAND => "RDRAND: instruction not supported" ,
155- Error :: WEB_CRYPTO => "Web Crypto API is unavailable" ,
156- Error :: VXWORKS_RAND_SECURE => "randSecure: VxWorks RNG module is not initialized" ,
157- Error :: WINDOWS_PROCESS_PRNG => "ProcessPrng: Windows system function failure" ,
158- Error :: RNDR_FAILURE => "RNDR: Could not generate a random number" ,
159- Error :: RNDR_NOT_AVAILABLE => "RNDR: Register not supported" ,
160- _ => return None ,
161- } ;
162- Some ( desc)
163- }
164-
165167#[ cfg( test) ]
166168mod tests {
167169 use super :: Error ;
0 commit comments