Skip to content

Commit 038e96e

Browse files
authored
Tweak opt-in backends (#513)
Move `compile_error!`s from lib.rs into opt-in backend modules. Rename `js.rs` to `wasm_js.rs` and `espidf.rs` to `esp_idf.rs` for consistency with backend names.
1 parent 74ea595 commit 038e96e

File tree

7 files changed

+24
-21
lines changed

7 files changed

+24
-21
lines changed

.github/workflows/workspace.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ jobs:
4545
run: cargo clippy -Zbuild-std=core --target x86_64-unknown-freebsd
4646
- name: Hermit (hermit.rs)
4747
run: cargo clippy -Zbuild-std=core --target x86_64-unknown-hermit
48-
- name: Web WASM (js.rs)
48+
- name: Web WASM (wasm_js.rs)
4949
env:
5050
RUSTFLAGS: -Dwarnings --cfg getrandom_backend="wasm_js"
5151
run: cargo clippy -Zbuild-std --target wasm32-unknown-unknown

src/espidf.rs renamed to src/esp_idf.rs

+3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
use crate::Error;
33
use core::{ffi::c_void, mem::MaybeUninit};
44

5+
#[cfg(not(target_os = "espidf"))]
6+
compile_error!("`esp_idf` backend can be enabled only for ESP-IDF targets!");
7+
58
extern "C" {
69
fn esp_fill_random(buf: *mut c_void, len: usize) -> u32;
710
}

src/lib.rs

+2-14
Original file line numberDiff line numberDiff line change
@@ -284,29 +284,17 @@ cfg_if! {
284284
if #[cfg(getrandom_backend = "custom")] {
285285
#[path = "custom.rs"] mod imp;
286286
} else if #[cfg(getrandom_backend = "linux_getrandom")] {
287-
#[cfg(not(any(target_os = "android", target_os = "linux")))]
288-
compile_error!("`linux_getrandom` backend can be enabled only for Linux/Android targets!");
289287
mod util_libc;
290288
#[path = "linux_android.rs"] mod imp;
291289
} else if #[cfg(getrandom_backend = "rdrand")] {
292-
#[cfg(not(any(target_arch = "x86_64", target_arch = "x86")))]
293-
compile_error!("`rdrand` backend can be enabled only for x86 and x86-64 targets!");
294-
295290
mod lazy;
296291
#[path = "rdrand.rs"] mod imp;
297292
} else if #[cfg(getrandom_backend = "rndr")] {
298293
#[path = "rndr.rs"] mod imp;
299294
} else if #[cfg(getrandom_backend = "wasm_js")] {
300-
#[cfg(not(all(
301-
any(target_arch = "wasm32", target_arch = "wasm64"),
302-
target_os = "unknown",
303-
)))]
304-
compile_error!("`wasm_js` backend can be enabled only on OS-less WASM targets!");
305-
#[path = "js.rs"] mod imp;
295+
#[path = "wasm_js.rs"] mod imp;
306296
} else if #[cfg(getrandom_backend = "esp_idf")] {
307-
#[cfg(not(target_os = "espidf"))]
308-
compile_error!("`esp_idf` backend can be enabled only for ESP-IDF targets!");
309-
#[path = "espidf.rs"] mod imp;
297+
#[path = "esp_idf.rs"] mod imp;
310298
} else if #[cfg(any(
311299
target_os = "haiku",
312300
target_os = "redox",

src/linux_android.rs

+3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
use crate::{util_libc, Error};
33
use core::mem::MaybeUninit;
44

5+
#[cfg(not(any(target_os = "android", target_os = "linux")))]
6+
compile_error!("`linux_getrandom` backend can be enabled only for Linux/Android targets!");
7+
58
pub fn getrandom_inner(dest: &mut [MaybeUninit<u8>]) -> Result<(), Error> {
69
util_libc::sys_fill_exact(dest, getrandom_syscall)
710
}

src/rdrand.rs

+3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
use crate::{lazy::LazyBool, util::slice_as_uninit, Error};
33
use core::mem::{size_of, MaybeUninit};
44

5+
#[cfg(not(any(target_arch = "x86_64", target_arch = "x86")))]
6+
compile_error!("`rdrand` backend can be enabled only for x86 and x86-64 targets!");
7+
58
cfg_if! {
69
if #[cfg(target_arch = "x86_64")] {
710
use core::arch::x86_64 as arch;

src/rndr.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
//! RNDR register backend for aarch64 targets
2-
// Arm Architecture Reference Manual for A-profile architecture
3-
// ARM DDI 0487K.a, ID032224, D23.2.147 RNDR, Random Number
4-
5-
#[cfg(not(target_arch = "aarch64"))]
6-
compile_error!("the `rndr` backend can be enabled only for AArch64 targets!");
7-
2+
//!
3+
//! Arm Architecture Reference Manual for A-profile architecture:
4+
//! ARM DDI 0487K.a, ID032224, D23.2.147 RNDR, Random Number
85
use crate::{util::slice_as_uninit, Error};
96
use core::arch::asm;
107
use core::mem::{size_of, MaybeUninit};
118

9+
#[cfg(not(target_arch = "aarch64"))]
10+
compile_error!("the `rndr` backend can be enabled only for AArch64 targets!");
11+
1212
const RETRY_LIMIT: usize = 5;
1313

1414
/// Read a random number from the aarch64 RNDR register

src/js.rs renamed to src/wasm_js.rs

+6
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@ use crate::Error;
44
extern crate std;
55
use std::{mem::MaybeUninit, thread_local};
66

7+
#[cfg(not(all(
8+
any(target_arch = "wasm32", target_arch = "wasm64"),
9+
target_os = "unknown",
10+
)))]
11+
compile_error!("`wasm_js` backend can be enabled only for OS-less WASM targets!");
12+
713
use js_sys::{global, Function, Uint8Array};
814
use wasm_bindgen::{prelude::wasm_bindgen, JsCast, JsValue};
915

0 commit comments

Comments
 (0)