From d3a82975cb460fdc03cfe119e6f7e2e64b0c1e74 Mon Sep 17 00:00:00 2001 From: Zac Harrold Date: Wed, 11 Feb 2026 14:35:43 +1100 Subject: [PATCH 1/2] Refactor backends.rs Refactored to ensure the main cfg_if consists of getrandom_backends, then selection exclusively by target_os. This means ordering of branches is far less consequential. Instead, consequential ordering is nested within the arm that selects a particular OS. --- src/backends.rs | 220 +++++++++++++++++++++++++++++++----------------- 1 file changed, 144 insertions(+), 76 deletions(-) diff --git a/src/backends.rs b/src/backends.rs index 95547d9d3..1d93307a4 100644 --- a/src/backends.rs +++ b/src/backends.rs @@ -35,9 +35,45 @@ cfg_if! { } else if #[cfg(getrandom_backend = "extern_impl")] { pub(crate) mod extern_impl; pub use extern_impl::*; - } else if #[cfg(all(target_os = "linux", target_env = ""))] { - mod linux_raw; - pub use linux_raw::*; + } else if #[cfg(target_os = "linux")] { + cfg_if! { + if #[cfg(target_env = "")] { + mod linux_raw; + pub use linux_raw::*; + } else if #[cfg( + // Only on these `target_arch`es Rust supports Linux kernel versions (3.2+) + // that precede the version (3.17) in which `getrandom(2)` was added: + // https://doc.rust-lang.org/stable/rustc/platform-support.html + any( + target_arch = "aarch64", + target_arch = "arm", + target_arch = "powerpc", + target_arch = "powerpc64", + target_arch = "s390x", + target_arch = "x86", + target_arch = "x86_64", + // Minimum supported Linux kernel version for MUSL targets + // is not specified explicitly (as of Rust 1.77) and they + // are used in practice to target pre-3.17 kernels. + all( + target_env = "musl", + not( + any( + target_arch = "riscv64", + target_arch = "riscv32", + ), + ), + ), + ), + )] { + mod use_file; + mod linux_android_with_fallback; + pub use linux_android_with_fallback::*; + } else { + mod getrandom; + pub use getrandom::*; + } + } } else if #[cfg(target_os = "espidf")] { mod esp_idf; pub use esp_idf::*; @@ -57,68 +93,61 @@ cfg_if! { ))] { mod getentropy; pub use getentropy::*; - } else if #[cfg(any( - // Rust supports Android API level 19 (KitKat) [0] and the next upgrade targets - // level 21 (Lollipop) [1], while `getrandom(2)` was added only in - // level 23 (Marshmallow). Note that it applies only to the "old" `target_arch`es, - // RISC-V Android targets sufficiently new API level, same will apply for potential - // new Android `target_arch`es. - // [0]: https://blog.rust-lang.org/2023/01/09/android-ndk-update-r25.html - // [1]: https://github.com/rust-lang/rust/pull/120593 - all( - target_os = "android", - any( - target_arch = "aarch64", - target_arch = "arm", - target_arch = "x86", - target_arch = "x86_64", - ), - ), - // Only on these `target_arch`es Rust supports Linux kernel versions (3.2+) - // that precede the version (3.17) in which `getrandom(2)` was added: - // https://doc.rust-lang.org/stable/rustc/platform-support.html - all( - target_os = "linux", - any( - target_arch = "aarch64", - target_arch = "arm", - target_arch = "powerpc", - target_arch = "powerpc64", - target_arch = "s390x", - target_arch = "x86", - target_arch = "x86_64", - // Minimum supported Linux kernel version for MUSL targets - // is not specified explicitly (as of Rust 1.77) and they - // are used in practice to target pre-3.17 kernels. - all( - target_env = "musl", - not( - any( - target_arch = "riscv64", - target_arch = "riscv32", - ), - ), + } else if #[cfg(target_os = "android")] { + cfg_if! { + if #[cfg( + // Rust supports Android API level 19 (KitKat) [0] and the next upgrade targets + // level 21 (Lollipop) [1], while `getrandom(2)` was added only in + // level 23 (Marshmallow). Note that it applies only to the "old" `target_arch`es, + // RISC-V Android targets sufficiently new API level, same will apply for potential + // new Android `target_arch`es. + // [0]: https://blog.rust-lang.org/2023/01/09/android-ndk-update-r25.html + // [1]: https://github.com/rust-lang/rust/pull/120593 + any( + target_arch = "aarch64", + target_arch = "arm", + target_arch = "x86", + target_arch = "x86_64", ), - ), - ) - ))] { - mod use_file; - mod linux_android_with_fallback; - pub use linux_android_with_fallback::*; + )] { + mod use_file; + mod linux_android_with_fallback; + pub use linux_android_with_fallback::*; + } else { + mod getrandom; + pub use getrandom::*; + } + } } else if #[cfg(any( - target_os = "android", - target_os = "linux", target_os = "dragonfly", target_os = "freebsd", target_os = "hurd", target_os = "illumos", target_os = "cygwin", - // Check for target_arch = "arm" to only include the 3DS. Does not - // include the Nintendo Switch (which is target_arch = "aarch64"). - all(target_os = "horizon", target_arch = "arm"), ))] { mod getrandom; pub use getrandom::*; + } else if #[cfg(target_os = "horizon")] { + cfg_if! { + // Check for target_arch = "arm" to only include the 3DS. Does not + // include the Nintendo Switch (which is target_arch = "aarch64") + if #[cfg(target_arch = "arm")] { + mod getrandom; + pub use getrandom::*; + } else if #[cfg(target_arch = "aarch64")] { + compile_error!(concat!( + "The horizon operating system is not supported by default on the Nintendo Switch; \ + You may need to define a custom backend see: \ + https://docs.rs/getrandom/", env!("CARGO_PKG_VERSION"), "/#custom-backend" + )); + } else { + compile_error!(concat!( + "The horizon operating system is only supported by default on the 3DS; \ + You may need to define a custom backend see: \ + https://docs.rs/getrandom/", env!("CARGO_PKG_VERSION"), "/#custom-backend" + )); + } + } } else if #[cfg(target_os = "solaris")] { mod solaris; pub use solaris::*; @@ -136,7 +165,7 @@ cfg_if! { ))] { mod apple_other; pub use apple_other::*; - } else if #[cfg(all(target_arch = "wasm32", target_os = "wasi"))] { + } else if #[cfg(target_os = "wasi")] { cfg_if! { if #[cfg(target_env = "p1")] { mod wasi_p1; @@ -149,35 +178,74 @@ cfg_if! { } else if #[cfg(target_os = "hermit")] { mod hermit; pub use hermit::*; - } else if #[cfg(all(target_arch = "x86_64", target_os = "motor"))] { - mod rdrand; - pub use rdrand::*; + } else if #[cfg(target_os = "motor")] { + cfg_if! { + if #[cfg(target_arch = "x86_64")] { + mod rdrand; + pub use rdrand::*; + } else { + compile_error!(concat!( + "The motor operating system is only supported by default on x86_64; \ + You may need to define a custom backend see: \ + https://docs.rs/getrandom/", env!("CARGO_PKG_VERSION"), "/#custom-backend" + )); + } + } } else if #[cfg(target_os = "vxworks")] { mod vxworks; pub use vxworks::*; } else if #[cfg(target_os = "solid_asp3")] { mod solid; pub use solid::*; - } else if #[cfg(all(windows, target_vendor = "win7"))] { - mod windows_legacy; - pub use windows_legacy::*; - } else if #[cfg(windows)] { - mod windows; - pub use windows::*; - } else if #[cfg(all(target_arch = "x86_64", target_env = "sgx"))] { - mod rdrand; - pub use rdrand::*; - } else if #[cfg(all(target_arch = "wasm32", any(target_os = "unknown", target_os = "none")))] { + } else if #[cfg(target_os = "windows")] { + cfg_if! { + if #[cfg(target_vendor = "win7")] { + mod windows_legacy; + pub use windows_legacy::*; + } else { + mod windows; + pub use windows::*; + } + } + } else if #[cfg(target_os = "uefi")] { + compile_error!(concat!( + "UEFI is not supported by default; \ + Consider enabling the efi_rng backend see: \ + https://docs.rs/getrandom/", env!("CARGO_PKG_VERSION"), "/#opt-in-backends" + )); + } else if #[cfg(any( + target_os = "unknown", + target_os = "none" + ))] { cfg_if! { - if #[cfg(feature = "wasm_js")] { - mod wasm_js; - pub use wasm_js::*; + if #[cfg(any( + target_arch = "x86" + target_arch = "x86_64", + ))] { + mod rdrand; + pub use rdrand::*; + } else if #[cfg(target_arch = "aarch64")] { + mod rndr; + pub use rndr::*; + } else if #[cfg(target_arch = "wasm32")] { + cfg_if! { + if #[cfg(feature = "wasm_js")] { + mod wasm_js; + pub use wasm_js::*; + } else { + compile_error!(concat!( + "The wasm32-unknown-unknown targets are not supported by default; \ + you may need to enable the \"wasm_js\" crate feature. \ + For more information see: \ + https://docs.rs/getrandom/", env!("CARGO_PKG_VERSION"), "/#webassembly-support" + )); + } + } } else { compile_error!(concat!( - "The wasm32-unknown-unknown targets are not supported by default; \ - you may need to enable the \"wasm_js\" crate feature. \ - For more information see: \ - https://docs.rs/getrandom/", env!("CARGO_PKG_VERSION"), "/#webassembly-support" + "Architecture is not supported by default without a supported operating system; \ + You may need to define a custom backend see: \ + https://docs.rs/getrandom/", env!("CARGO_PKG_VERSION"), "/#custom-backend" )); } } From 67038b8a9511f7eece9e59675bf85baa8cf53474 Mon Sep 17 00:00:00 2001 From: Zac Harrold Date: Wed, 11 Feb 2026 15:15:39 +1100 Subject: [PATCH 2/2] Fix a couple of commas --- src/backends.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/backends.rs b/src/backends.rs index 1d93307a4..415b906e9 100644 --- a/src/backends.rs +++ b/src/backends.rs @@ -64,7 +64,7 @@ cfg_if! { ), ), ), - ), + ) )] { mod use_file; mod linux_android_with_fallback; @@ -108,7 +108,7 @@ cfg_if! { target_arch = "arm", target_arch = "x86", target_arch = "x86_64", - ), + ) )] { mod use_file; mod linux_android_with_fallback; @@ -219,7 +219,7 @@ cfg_if! { ))] { cfg_if! { if #[cfg(any( - target_arch = "x86" + target_arch = "x86", target_arch = "x86_64", ))] { mod rdrand;