Skip to content

Commit 78f4ba1

Browse files
committed
Add opt_in crate feature
1 parent 6be4012 commit 78f4ba1

File tree

6 files changed

+30
-28
lines changed

6 files changed

+30
-28
lines changed

.github/workflows/build.yml

+10-10
Original file line numberDiff line numberDiff line change
@@ -121,13 +121,13 @@ jobs:
121121
- run: cargo build --target=${{ matrix.target }} --features=std
122122
- env:
123123
RUSTFLAGS: -Dwarnings --cfg getrandom_backend="linux_getrandom"
124-
run: cargo build --target=${{ matrix.target }} --features=std
124+
run: cargo build --target=${{ matrix.target }} --features=std,opt_in
125125
- env:
126126
RUSTFLAGS: -Dwarnings --cfg getrandom_test_linux_fallback
127127
run: cargo build --features=std
128128
- env:
129129
RUSTFLAGS: -Dwarnings --cfg getrandom_backend="rdrand"
130-
run: cargo build --features=std
130+
run: cargo build --features=std,opt_in
131131

132132
web:
133133
name: ${{ matrix.target.description }} ${{ matrix.feature.description }} ${{ matrix.atomic.description }}
@@ -142,8 +142,8 @@ jobs:
142142
{ description: WasmV1, target: wasm32v1-none },
143143
]
144144
feature: [
145-
{ description: no_std, feature: "", build-std: "core,alloc", std: false },
146-
{ feature: --features std, build-std: "panic_abort,std", std: true },
145+
{ description: no_std, feature: "--features opt_in", build-std: "core,alloc", std: false },
146+
{ feature: "--features std,opt_in", build-std: "panic_abort,std", std: true },
147147
]
148148
atomic: [
149149
{ flags: "" },
@@ -180,10 +180,10 @@ jobs:
180180
- uses: Swatinem/rust-cache@v2
181181
- env:
182182
RUSTFLAGS: -Dwarnings --cfg getrandom_backend="rdrand"
183-
run: cargo build -Z build-std=core --target=${{ matrix.target }}
183+
run: cargo build -Z build-std=core --target=${{ matrix.target }} --features opt_in
184184
- env:
185185
RUSTFLAGS: -Dwarnings --cfg getrandom_backend="rdrand"
186-
run: cargo build -Z build-std=std --target=${{ matrix.target }} --features std
186+
run: cargo build -Z build-std=std --target=${{ matrix.target }} --features std,opt_in
187187

188188
rndr:
189189
name: RNDR
@@ -198,15 +198,15 @@ jobs:
198198
- name: RNDR enabled at compile time (Linux)
199199
env:
200200
RUSTFLAGS: -Dwarnings --cfg getrandom_backend="rndr" -C target-feature=+rand
201-
run: cargo build --target=aarch64-unknown-linux-gnu
201+
run: cargo build --target=aarch64-unknown-linux-gnu --features opt_in
202202
- name: Runtime RNDR detection without std (Linux)
203203
env:
204204
RUSTFLAGS: -Dwarnings --cfg getrandom_backend="rndr"
205-
run: cargo build --target=aarch64-unknown-linux-gnu
205+
run: cargo build --target=aarch64-unknown-linux-gnu --features opt_in
206206
- name: Runtime RNDR detection with std (macOS)
207207
env:
208208
RUSTFLAGS: -Dwarnings --cfg getrandom_backend="rndr"
209-
run: cargo build --target=aarch64-unknown-linux-gnu --features std
209+
run: cargo build --target=aarch64-unknown-linux-gnu --features std,opt_in
210210

211211
no-atomics:
212212
name: No Atomics
@@ -219,4 +219,4 @@ jobs:
219219
- uses: Swatinem/rust-cache@v2
220220
- env:
221221
RUSTFLAGS: -Dwarnings --cfg getrandom_backend="custom"
222-
run: cargo build --target riscv32i-unknown-none-elf
222+
run: cargo build --target riscv32i-unknown-none-elf --features opt_in

.github/workflows/tests.yml

+4-4
Original file line numberDiff line numberDiff line change
@@ -55,13 +55,13 @@ jobs:
5555
- run: cargo test --target=${{ matrix.target }} --features=std
5656
- env:
5757
RUSTFLAGS: -Dwarnings --cfg getrandom_backend="linux_getrandom"
58-
run: cargo test --target=${{ matrix.target }} --features=std
58+
run: cargo test --target=${{ matrix.target }} --features=std,opt_in
5959
- env:
6060
RUSTFLAGS: -Dwarnings --cfg getrandom_test_linux_fallback
6161
run: cargo test --features=std
6262
- env:
6363
RUSTFLAGS: -Dwarnings --cfg getrandom_backend="rdrand"
64-
run: cargo test --features=std
64+
run: cargo test --features=std,opt_in
6565

6666
ios:
6767
name: iOS Simulator
@@ -238,14 +238,14 @@ jobs:
238238
description: Web,
239239
version: stable,
240240
flags: -Dwarnings --cfg getrandom_backend="wasm_js",
241-
args: --features=std,
241+
args: '--features=std,opt_in',
242242
}
243243
- {
244244
description: Web with Atomics,
245245
version: nightly,
246246
components: rust-src,
247247
flags: '-Dwarnings --cfg getrandom_backend="wasm_js" -Ctarget-feature=+atomics,+bulk-memory',
248-
args: '--features=std -Zbuild-std=panic_abort,std',
248+
args: '--features=std,opt_in -Zbuild-std=panic_abort,std',
249249
}
250250
steps:
251251
- uses: actions/checkout@v4

.github/workflows/workspace.yml

+5-5
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ jobs:
3030
- name: custom backend
3131
env:
3232
RUSTFLAGS: -Dwarnings --cfg getrandom_backend="custom"
33-
run: cargo clippy -Zbuild-std=core --target riscv32i-unknown-none-elf
33+
run: cargo clippy -Zbuild-std=core --target riscv32i-unknown-none-elf --features=opt_in
3434
- name: iOS (apple-other.rs)
3535
run: cargo clippy -Zbuild-std=core --target x86_64-apple-ios
3636
- name: ESP-IDF (espidf.rs)
@@ -48,15 +48,15 @@ jobs:
4848
- name: Web WASM (wasm_js.rs)
4949
env:
5050
RUSTFLAGS: -Dwarnings --cfg getrandom_backend="wasm_js"
51-
run: cargo clippy -Zbuild-std --target wasm32-unknown-unknown
51+
run: cargo clippy -Zbuild-std --target wasm32-unknown-unknown --features=opt_in
5252
- name: Web WASM with atomics (wasm_js.rs)
5353
env:
5454
RUSTFLAGS: -Dwarnings --cfg getrandom_backend="wasm_js" -Ctarget-feature=+atomics,+bulk-memory
55-
run: cargo clippy -Zbuild-std --target wasm32-unknown-unknown
55+
run: cargo clippy -Zbuild-std --target wasm32-unknown-unknown --features=opt_in
5656
- name: Linux (linux_android.rs)
5757
env:
5858
RUSTFLAGS: -Dwarnings --cfg getrandom_backend="linux_getrandom"
59-
run: cargo clippy --target x86_64-unknown-linux-gnu
59+
run: cargo clippy --target x86_64-unknown-linux-gnu --features=opt_in
6060
- name: Linux (linux_android_with_fallback.rs)
6161
run: cargo clippy --target x86_64-unknown-linux-gnu
6262
- name: NetBSD (netbsd.rs)
@@ -66,7 +66,7 @@ jobs:
6666
- name: RNDR (rndr.rs)
6767
env:
6868
RUSTFLAGS: -Dwarnings --cfg getrandom_backend="rndr"
69-
run: cargo clippy -Zbuild-std=core --target aarch64-unknown-linux-gnu
69+
run: cargo clippy -Zbuild-std=core --target aarch64-unknown-linux-gnu --features=opt_in
7070
- name: Solaris (solaris.rs)
7171
run: cargo clippy -Zbuild-std=core --target x86_64-pc-solaris
7272
- name: SOLID (solid.rs)

Cargo.toml

+5-3
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ compiler_builtins = { version = "0.1", optional = true }
1919
core = { version = "1.0", optional = true, package = "rustc-std-workspace-core" }
2020

2121
# linux_android / linux_android_with_fallback
22-
[target.'cfg(all(any(target_os = "linux", target_os = "android"), not(any(target_env = "", getrandom_backend = "custom"))))'.dependencies]
22+
[target.'cfg(any(target_os = "linux", target_os = "android"))'.dependencies]
2323
libc = { version = "0.2.154", default-features = false }
2424

2525
# apple-other
@@ -60,16 +60,18 @@ windows-targets = "0.52"
6060

6161
# wasm_js
6262
[target.'cfg(all(getrandom_backend = "wasm_js", target_arch = "wasm32", any(target_os = "unknown", target_os = "none")))'.dependencies]
63-
wasm-bindgen = { version = "0.2.98", default-features = false }
63+
wasm-bindgen = { version = "0.2.98", default-features = false, optional = true }
6464
[target.'cfg(all(getrandom_backend = "wasm_js", target_arch = "wasm32", any(target_os = "unknown", target_os = "none"), target_feature = "atomics"))'.dependencies]
65-
js-sys = { version = "0.3.77", default-features = false }
65+
js-sys = { version = "0.3.77", default-features = false, optional = true }
6666
[target.'cfg(all(getrandom_backend = "wasm_js", target_arch = "wasm32", any(target_os = "unknown", target_os = "none")))'.dev-dependencies]
6767
wasm-bindgen-test = "0.3"
6868

6969
[features]
7070
# Implement std::error::Error for getrandom::Error and
7171
# use std to retrieve OS error descriptions
7272
std = []
73+
# Enable opt-in backends
74+
opt_in = ["dep:wasm-bindgen", "dep:js-sys"]
7375
# Unstable feature to support being a libstd dependency
7476
rustc-dep-of-std = ["dep:compiler_builtins", "dep:core"]
7577

nopanic_check/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ name = "getrandom_wrapper"
1212
crate-type = ["cdylib"]
1313

1414
[dependencies]
15-
getrandom = { path = ".." }
15+
getrandom = { path = "..", features = ["opt_in"] }
1616

1717
[profile.release]
1818
panic = "abort"

src/backends.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,19 @@
77
//! regardless of what value it returns.
88
99
cfg_if! {
10-
if #[cfg(getrandom_backend = "custom")] {
10+
if #[cfg(all(feature = "opt_in",getrandom_backend = "custom"))] {
1111
mod custom;
1212
pub use custom::*;
13-
} else if #[cfg(getrandom_backend = "linux_getrandom")] {
13+
} else if #[cfg(all(feature = "opt_in", getrandom_backend = "linux_getrandom"))] {
1414
mod linux_android;
1515
pub use linux_android::*;
16-
} else if #[cfg(getrandom_backend = "rdrand")] {
16+
} else if #[cfg(all(feature = "opt_in", getrandom_backend = "rdrand"))] {
1717
mod rdrand;
1818
pub use rdrand::*;
19-
} else if #[cfg(getrandom_backend = "rndr")] {
19+
} else if #[cfg(all(feature = "opt_in", getrandom_backend = "rndr"))] {
2020
mod rndr;
2121
pub use rndr::*;
22-
} else if #[cfg(getrandom_backend = "wasm_js")] {
22+
} else if #[cfg(all(feature = "opt_in", getrandom_backend = "wasm_js"))] {
2323
mod wasm_js;
2424
pub use wasm_js::*;
2525
} else if #[cfg(target_os = "espidf")] {

0 commit comments

Comments
 (0)