Skip to content

Commit 479d036

Browse files
josephlrnewpavlov
authored andcommitted
Use cfg-if to simplify lib.rs (#55)
1 parent 0c72017 commit 479d036

File tree

8 files changed

+89
-111
lines changed

8 files changed

+89
-111
lines changed

Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ members = ["tests/wasm_bindgen"]
1919

2020
[dependencies]
2121
log = { version = "0.4", optional = true }
22+
cfg-if = "0.1"
2223

2324
[target.'cfg(any(unix, target_os = "redox", target_os = "wasi"))'.dependencies]
2425
libc = "0.2.54"

src/error.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ impl Error {
3838
}
3939

4040
pub(crate) fn msg(&self) -> Option<&'static str> {
41-
if let Some(msg) = super::error_msg_inner(self.0) {
41+
if let Some(msg) = crate::imp::error_msg_inner(self.0) {
4242
Some(msg)
4343
} else {
4444
match *self {

src/ios.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ extern crate std;
1111

1212
use crate::Error;
1313
use core::num::NonZeroU32;
14-
use std::io;
1514

1615
// TODO: Make extern once extern_types feature is stabilized. See:
1716
// https://github.com/rust-lang/rust/issues/43467
@@ -29,7 +28,7 @@ pub fn getrandom_inner(dest: &mut [u8]) -> Result<(), Error> {
2928
let ret = unsafe { SecRandomCopyBytes(kSecRandomDefault, dest.len(), dest.as_mut_ptr()) };
3029
if ret == -1 {
3130
error!("SecRandomCopyBytes call failed");
32-
Err(io::Error::last_os_error().into())
31+
Err(Error::UNKNOWN)
3332
} else {
3433
Ok(())
3534
}

src/lib.rs

+78-101
Original file line numberDiff line numberDiff line change
@@ -127,17 +127,23 @@
127127
#![no_std]
128128
#![cfg_attr(feature = "stdweb", recursion_limit = "128")]
129129

130-
#[cfg(feature = "log")]
131130
#[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+
}
137144
}
138145

139-
// temp fix for stdweb
140-
#[cfg(target_arch = "wasm32")]
146+
#[cfg(feature = "std")]
141147
extern crate std;
142148

143149
mod error;
@@ -149,27 +155,8 @@ mod util;
149155
#[allow(dead_code)]
150156
mod util_libc;
151157

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"))]
173160
mod error_impls;
174161

175162
// These targets read from a file as a fallback method.
@@ -180,79 +167,69 @@ mod error_impls;
180167
target_os = "solaris",
181168
target_os = "illumos",
182169
))]
170+
#[allow(dead_code)]
183171
mod use_file;
184172

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+
}
256233

257234
/// Fill `dest` with random bytes from the system's preferred random number
258235
/// source.
@@ -266,5 +243,5 @@ mod_use!(
266243
/// significantly slower than a user-space CSPRNG; for the latter consider
267244
/// [`rand::thread_rng`](https://docs.rs/rand/*/rand/fn.thread_rng.html).
268245
pub fn getrandom(dest: &mut [u8]) -> Result<(), error::Error> {
269-
getrandom_inner(dest)
246+
imp::getrandom_inner(dest)
270247
}

src/rdrand.rs

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
// except according to those terms.
88

99
//! Implementation for SGX using RDRAND instruction
10+
#[cfg(not(target_feature = "rdrand"))]
1011
use crate::util::LazyBool;
1112
use crate::Error;
1213
use core::arch::x86_64::_rdrand64_step;

src/wasi.rs

+4-5
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,15 @@
99
//! Implementation for WASI
1010
use crate::Error;
1111
use core::num::NonZeroU32;
12-
use std::io;
1312

1413
pub fn getrandom_inner(dest: &mut [u8]) -> Result<(), Error> {
1514
let ret =
1615
unsafe { libc::__wasi_random_get(dest.as_mut_ptr() as *mut libc::c_void, dest.len()) };
17-
if ret == libc::__WASI_ESUCCESS {
18-
Ok(())
16+
if let Some(code) = NonZeroU32::new(ret as u32) {
17+
error!("WASI: __wasi_random_get failed with return value {}", code);
18+
Err(Error::from(code))
1919
} else {
20-
error!("WASI: __wasi_random_get failed with return value {}", ret);
21-
Err(io::Error::last_os_error().into())
20+
Ok(()) // Zero means success for WASI
2221
}
2322
}
2423

src/wasm32_bindgen.rs

+2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
// except according to those terms.
88

99
//! Implementation for WASM via wasm-bindgen
10+
extern crate std;
11+
1012
use core::cell::RefCell;
1113
use core::mem;
1214
use core::num::NonZeroU32;

src/windows.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ extern crate std;
1111

1212
use crate::Error;
1313
use core::num::NonZeroU32;
14-
use std::io;
1514

1615
extern "system" {
1716
#[link_name = "SystemFunction036"]
@@ -24,7 +23,7 @@ pub fn getrandom_inner(dest: &mut [u8]) -> Result<(), Error> {
2423
let ret = unsafe { RtlGenRandom(chunk.as_mut_ptr(), chunk.len() as u32) };
2524
if ret == 0 {
2625
error!("RtlGenRandom call failed");
27-
return Err(io::Error::last_os_error().into());
26+
return Err(Error::UNKNOWN);
2827
}
2928
}
3029
Ok(())

0 commit comments

Comments
 (0)