127
127
#![ no_std]
128
128
#![ cfg_attr( feature = "stdweb" , recursion_limit = "128" ) ]
129
129
130
- #[ cfg( feature = "log" ) ]
131
130
#[ macro_use]
132
- extern crate log;
133
- #[ cfg( not( feature = "log" ) ) ]
134
- #[ allow( unused) ]
135
- macro_rules! error {
136
- ( $( $x: tt) * ) => { } ;
131
+ extern crate cfg_if;
132
+
133
+ cfg_if ! {
134
+ if #[ cfg( feature = "log" ) ] {
135
+ #[ allow( unused) ]
136
+ #[ macro_use]
137
+ extern crate log;
138
+ } else {
139
+ #[ allow( unused) ]
140
+ macro_rules! error {
141
+ ( $( $x: tt) * ) => { } ;
142
+ }
143
+ }
137
144
}
138
145
139
- // temp fix for stdweb
140
- #[ cfg( target_arch = "wasm32" ) ]
146
+ #[ cfg( feature = "std" ) ]
141
147
extern crate std;
142
148
143
149
mod error;
@@ -149,27 +155,8 @@ mod util;
149
155
#[ allow( dead_code) ]
150
156
mod util_libc;
151
157
152
- // System-specific implementations.
153
- //
154
- // These should all provide getrandom_inner with the same signature as getrandom.
155
-
156
- macro_rules! mod_use {
157
- ( $cond: meta, $module: ident) => {
158
- #[ $cond]
159
- mod $module;
160
- #[ $cond]
161
- use crate :: $module:: { error_msg_inner, getrandom_inner} ;
162
- } ;
163
- }
164
-
165
- // These targets use std anyway, so we use the std declarations.
166
- #[ cfg( any(
167
- feature = "std" ,
168
- windows,
169
- unix,
170
- target_os = "redox" ,
171
- target_arch = "wasm32" ,
172
- ) ) ]
158
+ // std-only trait definitions (also need for use_file)
159
+ #[ cfg( any( feature = "std" , unix, target_os = "redox" ) ) ]
173
160
mod error_impls;
174
161
175
162
// These targets read from a file as a fallback method.
@@ -180,79 +167,69 @@ mod error_impls;
180
167
target_os = "solaris" ,
181
168
target_os = "illumos" ,
182
169
) ) ]
170
+ #[ allow( dead_code) ]
183
171
mod use_file;
184
172
185
- mod_use ! ( cfg( target_os = "android" ) , linux_android) ;
186
- mod_use ! ( cfg( target_os = "bitrig" ) , openbsd_bitrig) ;
187
- mod_use ! ( cfg( target_os = "cloudabi" ) , cloudabi) ;
188
- mod_use ! ( cfg( target_os = "dragonfly" ) , use_file) ;
189
- mod_use ! ( cfg( target_os = "emscripten" ) , use_file) ;
190
- mod_use ! ( cfg( target_os = "freebsd" ) , freebsd) ;
191
- mod_use ! ( cfg( target_os = "fuchsia" ) , fuchsia) ;
192
- mod_use ! ( cfg( target_os = "haiku" ) , use_file) ;
193
- mod_use ! ( cfg( target_os = "illumos" ) , solaris_illumos) ;
194
- mod_use ! ( cfg( target_os = "ios" ) , ios) ;
195
- mod_use ! ( cfg( target_os = "linux" ) , linux_android) ;
196
- mod_use ! ( cfg( target_os = "macos" ) , macos) ;
197
- mod_use ! ( cfg( target_os = "netbsd" ) , use_file) ;
198
- mod_use ! ( cfg( target_os = "openbsd" ) , openbsd_bitrig) ;
199
- mod_use ! ( cfg( target_os = "redox" ) , use_file) ;
200
- mod_use ! ( cfg( target_os = "solaris" ) , solaris_illumos) ;
201
- mod_use ! ( cfg( windows) , windows) ;
202
- mod_use ! ( cfg( target_env = "sgx" ) , rdrand) ;
203
- mod_use ! ( cfg( all( target_arch = "x86_64" , target_os = "uefi" ) ) , rdrand) ;
204
- mod_use ! ( cfg( target_os = "wasi" ) , wasi) ;
205
-
206
- mod_use ! (
207
- cfg( all(
208
- target_arch = "wasm32" ,
209
- not( target_os = "emscripten" ) ,
210
- not( target_os = "wasi" ) ,
211
- feature = "wasm-bindgen"
212
- ) ) ,
213
- wasm32_bindgen
214
- ) ;
215
-
216
- mod_use ! (
217
- cfg( all(
218
- target_arch = "wasm32" ,
219
- not( target_os = "emscripten" ) ,
220
- not( target_os = "wasi" ) ,
221
- not( feature = "wasm-bindgen" ) ,
222
- feature = "stdweb" ,
223
- ) ) ,
224
- wasm32_stdweb
225
- ) ;
226
-
227
- mod_use ! (
228
- cfg( not( any(
229
- target_os = "android" ,
230
- target_os = "bitrig" ,
231
- target_os = "cloudabi" ,
232
- target_os = "dragonfly" ,
233
- target_os = "emscripten" ,
234
- target_os = "freebsd" ,
235
- target_os = "fuchsia" ,
236
- target_os = "haiku" ,
237
- target_os = "illumos" ,
238
- target_os = "ios" ,
239
- target_os = "linux" ,
240
- target_os = "macos" ,
241
- target_os = "netbsd" ,
242
- target_os = "openbsd" ,
243
- target_os = "redox" ,
244
- target_os = "solaris" ,
245
- all( target_arch = "x86_64" , target_os = "uefi" ) ,
246
- target_os = "wasi" ,
247
- target_env = "sgx" ,
248
- windows,
249
- all(
250
- target_arch = "wasm32" ,
251
- any( feature = "wasm-bindgen" , feature = "stdweb" ) ,
252
- ) ,
253
- ) ) ) ,
254
- dummy
255
- ) ;
173
+ // System-specific implementations.
174
+ //
175
+ // These should all provide getrandom_inner with the same signature as getrandom.
176
+ cfg_if ! {
177
+ if #[ cfg( target_os = "android" ) ] {
178
+ #[ path = "linux_android.rs" ] mod imp;
179
+ } else if #[ cfg( target_os = "bitrig" ) ] {
180
+ #[ path = "openbsd_bitrig.rs" ] mod imp;
181
+ } else if #[ cfg( target_os = "cloudabi" ) ] {
182
+ #[ path = "cloudabi.rs" ] mod imp;
183
+ } else if #[ cfg( target_os = "dragonfly" ) ] {
184
+ #[ path = "use_file.rs" ] mod imp;
185
+ } else if #[ cfg( target_os = "emscripten" ) ] {
186
+ #[ path = "use_file.rs" ] mod imp;
187
+ } else if #[ cfg( target_os = "freebsd" ) ] {
188
+ #[ path = "freebsd.rs" ] mod imp;
189
+ } else if #[ cfg( target_os = "fuchsia" ) ] {
190
+ #[ path = "fuchsia.rs" ] mod imp;
191
+ } else if #[ cfg( target_os = "haiku" ) ] {
192
+ #[ path = "use_file.rs" ] mod imp;
193
+ } else if #[ cfg( target_os = "illumos" ) ] {
194
+ #[ path = "solaris_illumos.rs" ] mod imp;
195
+ } else if #[ cfg( target_os = "ios" ) ] {
196
+ #[ path = "ios.rs" ] mod imp;
197
+ } else if #[ cfg( target_os = "linux" ) ] {
198
+ #[ path = "linux_android.rs" ] mod imp;
199
+ } else if #[ cfg( target_os = "macos" ) ] {
200
+ #[ path = "macos.rs" ] mod imp;
201
+ } else if #[ cfg( target_os = "netbsd" ) ] {
202
+ #[ path = "use_file.rs" ] mod imp;
203
+ } else if #[ cfg( target_os = "openbsd" ) ] {
204
+ #[ path = "openbsd_bitrig.rs" ] mod imp;
205
+ } else if #[ cfg( target_os = "redox" ) ] {
206
+ #[ path = "use_file.rs" ] mod imp;
207
+ } else if #[ cfg( target_os = "solaris" ) ] {
208
+ #[ path = "solaris_illumos.rs" ] mod imp;
209
+ } else if #[ cfg( target_os = "wasi" ) ] {
210
+ #[ path = "wasi.rs" ] mod imp;
211
+ } else if #[ cfg( windows) ] {
212
+ #[ path = "windows.rs" ] mod imp;
213
+ } else if #[ cfg( target_env = "sgx" ) ] {
214
+ #[ path = "rdrand.rs" ] mod imp;
215
+ } else if #[ cfg( all( target_arch = "x86_64" , target_os = "uefi" ) ) ] {
216
+ #[ path = "rdrand.rs" ] mod imp;
217
+ } else if #[ cfg( target_arch = "wasm32" ) ] {
218
+ cfg_if! {
219
+ if #[ cfg( feature = "wasm-bindgen" ) ] {
220
+ #[ path = "wasm32_bindgen.rs" ] mod imp;
221
+ } else if #[ cfg( feature = "stdweb" ) ] {
222
+ // temp fix for stdweb
223
+ extern crate std;
224
+ #[ path = "wasm32_stdweb.rs" ] mod imp;
225
+ } else {
226
+ #[ path = "dummy.rs" ] mod imp;
227
+ }
228
+ }
229
+ } else {
230
+ #[ path = "dummy.rs" ] mod imp;
231
+ }
232
+ }
256
233
257
234
/// Fill `dest` with random bytes from the system's preferred random number
258
235
/// source.
@@ -266,5 +243,5 @@ mod_use!(
266
243
/// significantly slower than a user-space CSPRNG; for the latter consider
267
244
/// [`rand::thread_rng`](https://docs.rs/rand/*/rand/fn.thread_rng.html).
268
245
pub fn getrandom ( dest : & mut [ u8 ] ) -> Result < ( ) , error:: Error > {
269
- getrandom_inner ( dest)
246
+ imp :: getrandom_inner ( dest)
270
247
}
0 commit comments