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 9, 2025
1 parent 9fb4a9a commit f42fc31
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 28 deletions.
20 changes: 10 additions & 10 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -120,13 +120,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 @@ -160,7 +160,7 @@ jobs:
components: rust-src
- uses: Swatinem/rust-cache@v2
- name: Build
run: cargo build --target ${{ matrix.target.target }} ${{ matrix.feature.feature }} -Zbuild-std=${{ matrix.feature.build-std }}
run: cargo build --features=opt_in --target ${{ matrix.target.target }} ${{ matrix.feature.feature }} -Zbuild-std=${{ matrix.feature.build-std }}

rdrand-uefi:
name: RDRAND UEFI
Expand All @@ -179,10 +179,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 --features=opt_in -Z build-std=core --target=${{ matrix.target }}
- env:
RUSTFLAGS: -Dwarnings --cfg getrandom_backend="rdrand"
run: cargo build -Z build-std=std --target=${{ matrix.target }} --features std
run: cargo build --features=opt_in -Z build-std=std --target=${{ matrix.target }} --features std

rndr:
name: RNDR
Expand All @@ -197,15 +197,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 --features=opt_in --target=aarch64-unknown-linux-gnu
- 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 --features=opt_in --target=aarch64-unknown-linux-gnu
- 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 --features=opt_in --target=aarch64-unknown-linux-gnu --features std

esp-idf:
name: ESP-IDF
Expand All @@ -218,7 +218,7 @@ jobs:
- uses: Swatinem/rust-cache@v2
- env:
RUSTFLAGS: -Dwarnings --cfg getrandom_backend="esp_idf"
run: cargo build -Z build-std=core --target=riscv32imc-esp-espidf
run: cargo build --features=opt_in -Z build-std=core --target=riscv32imc-esp-espidf

no-atomics:
name: No Atomics
Expand All @@ -231,4 +231,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 --features=opt_in --target riscv32i-unknown-none-elf
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 --features=opt_in --target=aarch64-unknown-linux-gnu
- 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
10 changes: 6 additions & 4 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 @@ -59,17 +59,19 @@ wasi = { version = "0.13", default-features = false }
windows-targets = "0.52"

# wasm_js
[target.'cfg(all(getrandom_backend = "wasm_js", target_arch = "wasm32", any(target_os = "unknown", target_os = "none")))'.dependencies]
[target.'cfg(all(feature = "opt_in", getrandom_backend = "wasm_js", target_arch = "wasm32", any(target_os = "unknown", target_os = "none")))'.dependencies]
wasm-bindgen = { version = "0.2.98", default-features = false }
[target.'cfg(all(getrandom_backend = "wasm_js", target_arch = "wasm32", any(target_os = "unknown", target_os = "none"), target_feature = "atomics"))'.dependencies]
[target.'cfg(all(feature = "opt_in", getrandom_backend = "wasm_js", target_arch = "wasm32", any(target_os = "unknown", target_os = "none"), target_feature = "atomics"))'.dependencies]
js-sys = { version = "0.3.75", default-features = false }
[target.'cfg(all(getrandom_backend = "wasm_js", target_arch = "wasm32", any(target_os = "unknown", target_os = "none")))'.dev-dependencies]
[target.'cfg(all(feature = "opt_in", 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
12 changes: 6 additions & 6 deletions src/backends.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,22 @@
//! 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(getrandom_backend = "esp_idf")] {
} else if #[cfg(all(feature = "opt_in", getrandom_backend = "esp_idf"))] {
mod esp_idf;
pub use esp_idf::*;
} else if #[cfg(any(
Expand Down

0 comments on commit f42fc31

Please sign in to comment.