Skip to content

Commit

Permalink
Add opt_in crate feature
Browse files Browse the repository at this point in the history
  • Loading branch information
newpavlov committed Jan 17, 2025
1 parent 6be4012 commit 6b34c80
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 28 deletions.
16 changes: 8 additions & 8 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -121,13 +121,13 @@ jobs:
- run: cargo build --target=${{ matrix.target }} --features=std
- env:
RUSTFLAGS: -Dwarnings --cfg getrandom_backend="linux_getrandom"
run: cargo build --target=${{ matrix.target }} --features=std
run: cargo build --target=${{ matrix.target }} --features=std,opt_in
- env:
RUSTFLAGS: -Dwarnings --cfg getrandom_test_linux_fallback
run: cargo build --features=std
- env:
RUSTFLAGS: -Dwarnings --cfg getrandom_backend="rdrand"
run: cargo build --features=std
run: cargo build --features=std,opt_in

web:
name: ${{ matrix.target.description }} ${{ matrix.feature.description }} ${{ matrix.atomic.description }}
Expand Down Expand Up @@ -180,10 +180,10 @@ jobs:
- uses: Swatinem/rust-cache@v2
- env:
RUSTFLAGS: -Dwarnings --cfg getrandom_backend="rdrand"
run: cargo build -Z build-std=core --target=${{ matrix.target }}
run: cargo build -Z build-std=core --target=${{ matrix.target }} --features opt_in
- env:
RUSTFLAGS: -Dwarnings --cfg getrandom_backend="rdrand"
run: cargo build -Z build-std=std --target=${{ matrix.target }} --features std
run: cargo build -Z build-std=std --target=${{ matrix.target }} --features std,opt_in

rndr:
name: RNDR
Expand All @@ -198,15 +198,15 @@ jobs:
- name: RNDR enabled at compile time (Linux)
env:
RUSTFLAGS: -Dwarnings --cfg getrandom_backend="rndr" -C target-feature=+rand
run: cargo build --target=aarch64-unknown-linux-gnu
run: cargo build --target=aarch64-unknown-linux-gnu --features opt_in
- name: Runtime RNDR detection without std (Linux)
env:
RUSTFLAGS: -Dwarnings --cfg getrandom_backend="rndr"
run: cargo build --target=aarch64-unknown-linux-gnu
run: cargo build --target=aarch64-unknown-linux-gnu --features opt_in
- name: Runtime RNDR detection with std (macOS)
env:
RUSTFLAGS: -Dwarnings --cfg getrandom_backend="rndr"
run: cargo build --target=aarch64-unknown-linux-gnu --features std
run: cargo build --target=aarch64-unknown-linux-gnu --features std,opt_in

no-atomics:
name: No Atomics
Expand All @@ -219,4 +219,4 @@ jobs:
- uses: Swatinem/rust-cache@v2
- env:
RUSTFLAGS: -Dwarnings --cfg getrandom_backend="custom"
run: cargo build --target riscv32i-unknown-none-elf
run: cargo build --target riscv32i-unknown-none-elf --features opt_in
8 changes: 4 additions & 4 deletions .github/workflows/nopanic.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -45,21 +45,21 @@ jobs:
- name: Build (linux_android.rs)
env:
RUSTFLAGS: -Dwarnings --cfg getrandom_backend="linux_getrandom"
run: cargo build --release
run: cargo build --release --features=opt_in
- name: Check (linux_android.rs)
run: (exit $( grep -c panic target/release/libgetrandom_wrapper.so ))

- name: Build (rdrand.rs)
env:
RUSTFLAGS: -Dwarnings --cfg getrandom_backend="rdrand"
run: cargo build --release
run: cargo build --release --features=opt_in
- name: Check (rdrand.rs)
run: (exit $( grep -c panic target/release/libgetrandom_wrapper.so ))

- name: Build (custom.rs)
env:
RUSTFLAGS: -Dwarnings --cfg getrandom_backend="custom"
run: cargo build --release
run: cargo build --release --features=opt_in
- name: Check (custom.rs)
run: (exit $( grep -c panic target/release/libgetrandom_wrapper.so ))

Expand Down Expand Up @@ -94,7 +94,7 @@ jobs:
- name: Build (rndr.rs)
env:
RUSTFLAGS: -Dwarnings --cfg getrandom_backend="rndr"
run: cross build --release --target=aarch64-unknown-linux-gnu
run: cross build --release --target=aarch64-unknown-linux-gnu --features=opt_in
- name: Check (rndr.rs)
run: (exit $( grep -c panic target/aarch64-unknown-linux-gnu/release/libgetrandom_wrapper.so ))

Expand Down
8 changes: 4 additions & 4 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,13 @@ jobs:
- run: cargo test --target=${{ matrix.target }} --features=std
- env:
RUSTFLAGS: -Dwarnings --cfg getrandom_backend="linux_getrandom"
run: cargo test --target=${{ matrix.target }} --features=std
run: cargo test --target=${{ matrix.target }} --features=std,opt_in
- env:
RUSTFLAGS: -Dwarnings --cfg getrandom_test_linux_fallback
run: cargo test --features=std
- env:
RUSTFLAGS: -Dwarnings --cfg getrandom_backend="rdrand"
run: cargo test --features=std
run: cargo test --features=std,opt_in

ios:
name: iOS Simulator
Expand Down Expand Up @@ -238,14 +238,14 @@ jobs:
description: Web,
version: stable,
flags: -Dwarnings --cfg getrandom_backend="wasm_js",
args: --features=std,
args: '--features=std,opt_in',
}
- {
description: Web with Atomics,
version: nightly,
components: rust-src,
flags: '-Dwarnings --cfg getrandom_backend="wasm_js" -Ctarget-feature=+atomics,+bulk-memory',
args: '--features=std -Zbuild-std=panic_abort,std',
args: '--features=std,opt_in -Zbuild-std=panic_abort,std',
}
steps:
- uses: actions/checkout@v4
Expand Down
8 changes: 4 additions & 4 deletions .github/workflows/workspace.yml
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,15 @@ jobs:
- name: Web WASM (wasm_js.rs)
env:
RUSTFLAGS: -Dwarnings --cfg getrandom_backend="wasm_js"
run: cargo clippy -Zbuild-std --target wasm32-unknown-unknown
run: cargo clippy -Zbuild-std --target wasm32-unknown-unknown --features=opt_in
- name: Web WASM with atomics (wasm_js.rs)
env:
RUSTFLAGS: -Dwarnings --cfg getrandom_backend="wasm_js" -Ctarget-feature=+atomics,+bulk-memory
run: cargo clippy -Zbuild-std --target wasm32-unknown-unknown
run: cargo clippy -Zbuild-std --target wasm32-unknown-unknown --features=opt_in
- name: Linux (linux_android.rs)
env:
RUSTFLAGS: -Dwarnings --cfg getrandom_backend="linux_getrandom"
run: cargo clippy --target x86_64-unknown-linux-gnu
run: cargo clippy --target x86_64-unknown-linux-gnu --features=opt_in
- name: Linux (linux_android_with_fallback.rs)
run: cargo clippy --target x86_64-unknown-linux-gnu
- name: NetBSD (netbsd.rs)
Expand All @@ -66,7 +66,7 @@ jobs:
- name: RNDR (rndr.rs)
env:
RUSTFLAGS: -Dwarnings --cfg getrandom_backend="rndr"
run: cargo clippy -Zbuild-std=core --target aarch64-unknown-linux-gnu
run: cargo clippy -Zbuild-std=core --target aarch64-unknown-linux-gnu --features=opt_in
- name: Solaris (solaris.rs)
run: cargo clippy -Zbuild-std=core --target x86_64-pc-solaris
- name: SOLID (solid.rs)
Expand Down
8 changes: 5 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ compiler_builtins = { version = "0.1", optional = true }
core = { version = "1.0", optional = true, package = "rustc-std-workspace-core" }

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

# apple-other
Expand Down Expand Up @@ -60,16 +60,18 @@ windows-targets = "0.52"

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

[features]
# Implement std::error::Error for getrandom::Error and
# use std to retrieve OS error descriptions
std = []
# Enable opt-in backends
opt_in = []
# Unstable feature to support being a libstd dependency
rustc-dep-of-std = ["dep:compiler_builtins", "dep:core"]

Expand Down
10 changes: 5 additions & 5 deletions src/backends.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,19 @@
//! regardless of what value it returns.
cfg_if! {
if #[cfg(getrandom_backend = "custom")] {
if #[cfg(all(feature = "opt_in",getrandom_backend = "custom"))] {
mod custom;
pub use custom::*;
} else if #[cfg(getrandom_backend = "linux_getrandom")] {
} else if #[cfg(all(feature = "opt_in", getrandom_backend = "linux_getrandom"))] {
mod linux_android;
pub use linux_android::*;
} else if #[cfg(getrandom_backend = "rdrand")] {
} else if #[cfg(all(feature = "opt_in", getrandom_backend = "rdrand"))] {
mod rdrand;
pub use rdrand::*;
} else if #[cfg(getrandom_backend = "rndr")] {
} else if #[cfg(all(feature = "opt_in", getrandom_backend = "rndr"))] {
mod rndr;
pub use rndr::*;
} else if #[cfg(getrandom_backend = "wasm_js")] {
} else if #[cfg(all(feature = "opt_in", getrandom_backend = "wasm_js"))] {
mod wasm_js;
pub use wasm_js::*;
} else if #[cfg(target_os = "espidf")] {
Expand Down

0 comments on commit 6b34c80

Please sign in to comment.