Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add opt_in crate feature #580

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 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 All @@ -142,8 +142,8 @@ jobs:
{ description: WasmV1, target: wasm32v1-none },
]
feature: [
{ description: no_std, feature: "", build-std: "core,alloc", std: false },
{ feature: --features std, build-std: "panic_abort,std", std: true },
{ description: no_std, feature: "--features opt_in", build-std: "core,alloc", std: false },
{ feature: "--features std,opt_in", build-std: "panic_abort,std", std: true },
]
atomic: [
{ flags: "" },
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/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: 5 additions & 5 deletions .github/workflows/workspace.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ jobs:
- name: custom backend
env:
RUSTFLAGS: -Dwarnings --cfg getrandom_backend="custom"
run: cargo clippy -Zbuild-std=core --target riscv32i-unknown-none-elf
run: cargo clippy -Zbuild-std=core --target riscv32i-unknown-none-elf --features=opt_in
- name: iOS (apple-other.rs)
run: cargo clippy -Zbuild-std=core --target x86_64-apple-ios
- name: ESP-IDF (espidf.rs)
Expand All @@ -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 = ["dep:wasm-bindgen", "dep:js-sys"]
# Unstable feature to support being a libstd dependency
rustc-dep-of-std = ["dep:compiler_builtins", "dep:core"]

Expand Down
2 changes: 1 addition & 1 deletion nopanic_check/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ name = "getrandom_wrapper"
crate-type = ["cdylib"]

[dependencies]
getrandom = { path = ".." }
getrandom = { path = "..", features = ["opt_in"] }

[profile.release]
panic = "abort"
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