about summary refs log tree commit diff
path: root/library/std/src/sys/random
diff options
context:
space:
mode:
authorJosh Triplett <josh@joshtriplett.org>2025-08-10 14:25:43 -0700
committerJosh Triplett <josh@joshtriplett.org>2025-08-16 05:28:31 -0700
commit1ae4a0cc3487f28b29f36afe8056535afad21e7b (patch)
treea408ec3e10388ca37142ab6456a8ec7b83b141c6 /library/std/src/sys/random
parent3507a749b365aae4eefa96ab700a9315d3280ee7 (diff)
library: Migrate from `cfg_if` to `cfg_select`
Migrate the standard library from using the external `cfg_if` crate to
using the now-built-in `cfg_select` macro.

This does not yet eliminate the dependency from
`library/std/Cargo.toml`, because while the standard library itself no
longer uses `cfg_if`, it also incorporates the `backtrace` crate, which
does.

Migration assisted by the following vim command (after selecting the
full `cfg_if!` invocation):

```
'<,'>s/\(cfg_if::\)\?cfg_if/cfg_select/ | '<,'>s/^\( *\)} else {/\1}\r\1_ => {/c | '<,'>s/^\( *\)} else if #\[cfg(\(.*\))\] /\1}\r\1\2 => /e | '<,'>s/if #\[cfg(\(.*\))\] {/\1 => {/e
```

This is imperfect, but substantially accelerated the process. This
prompts for confirmation on the `} else {` since that can also appear
inside one of the arms. This also requires manual intervention to handle
any multi-line conditions.
Diffstat (limited to 'library/std/src/sys/random')
-rw-r--r--library/std/src/sys/random/mod.rs68
-rw-r--r--library/std/src/sys/random/uefi.rs7
2 files changed, 48 insertions, 27 deletions
diff --git a/library/std/src/sys/random/mod.rs b/library/std/src/sys/random/mod.rs
index fc85797dcc2..a7fbae86099 100644
--- a/library/std/src/sys/random/mod.rs
+++ b/library/std/src/sys/random/mod.rs
@@ -1,16 +1,19 @@
-cfg_if::cfg_if! {
+cfg_select! {
     // Tier 1
-    if #[cfg(any(target_os = "linux", target_os = "android"))] {
+    any(target_os = "linux", target_os = "android") => {
         mod linux;
         pub use linux::{fill_bytes, hashmap_random_keys};
-    } else if #[cfg(target_os = "windows")] {
+    }
+    target_os = "windows" => {
         mod windows;
         pub use windows::fill_bytes;
-    } else if #[cfg(target_vendor = "apple")] {
+    }
+    target_vendor = "apple" => {
         mod apple;
         pub use apple::fill_bytes;
     // Others, in alphabetical ordering.
-    } else if #[cfg(any(
+    }
+    any(
         target_os = "dragonfly",
         target_os = "freebsd",
         target_os = "haiku",
@@ -21,69 +24,86 @@ cfg_if::cfg_if! {
         target_os = "solaris",
         target_os = "vita",
         target_os = "nuttx",
-    ))] {
+    ) => {
         mod arc4random;
         pub use arc4random::fill_bytes;
-    } else if #[cfg(target_os = "emscripten")] {
+    }
+    target_os = "emscripten" => {
         mod getentropy;
         pub use getentropy::fill_bytes;
-    } else if #[cfg(target_os = "espidf")] {
+    }
+    target_os = "espidf" => {
         mod espidf;
         pub use espidf::fill_bytes;
-    } else if #[cfg(target_os = "fuchsia")] {
+    }
+    target_os = "fuchsia" => {
         mod fuchsia;
         pub use fuchsia::fill_bytes;
-    } else if #[cfg(target_os = "hermit")] {
+    }
+    target_os = "hermit" => {
         mod hermit;
         pub use hermit::fill_bytes;
-    } else if #[cfg(any(target_os = "horizon", target_os = "cygwin"))] {
+    }
+    any(target_os = "horizon", target_os = "cygwin") => {
         // FIXME(horizon): add arc4random_buf to shim-3ds
         mod getrandom;
         pub use getrandom::fill_bytes;
-    } else if #[cfg(any(
+    }
+    any(
         target_os = "aix",
         target_os = "hurd",
         target_os = "l4re",
         target_os = "nto",
-    ))] {
+    ) => {
         mod unix_legacy;
         pub use unix_legacy::fill_bytes;
-    } else if #[cfg(target_os = "redox")] {
+    }
+    target_os = "redox" => {
         mod redox;
         pub use redox::fill_bytes;
-    } else if #[cfg(all(target_vendor = "fortanix", target_env = "sgx"))] {
+    }
+    all(target_vendor = "fortanix", target_env = "sgx") => {
         mod sgx;
         pub use sgx::fill_bytes;
-    } else if #[cfg(target_os = "solid_asp3")] {
+    }
+    target_os = "solid_asp3" => {
         mod solid;
         pub use solid::fill_bytes;
-    } else if #[cfg(target_os = "teeos")] {
+    }
+    target_os = "teeos" => {
         mod teeos;
         pub use teeos::fill_bytes;
-    } else if #[cfg(target_os = "trusty")] {
+    }
+    target_os = "trusty" => {
         mod trusty;
         pub use trusty::fill_bytes;
-    } else if #[cfg(target_os = "uefi")] {
+    }
+    target_os = "uefi" => {
         mod uefi;
         pub use uefi::fill_bytes;
-    } else if #[cfg(target_os = "vxworks")] {
+    }
+    target_os = "vxworks" => {
         mod vxworks;
         pub use vxworks::fill_bytes;
-    } else if #[cfg(target_os = "wasi")] {
+    }
+    target_os = "wasi" => {
         mod wasi;
         pub use wasi::fill_bytes;
-    } else if #[cfg(target_os = "zkvm")] {
+    }
+    target_os = "zkvm" => {
         mod zkvm;
         pub use zkvm::fill_bytes;
-    } else if #[cfg(any(
+    }
+    any(
         all(target_family = "wasm", target_os = "unknown"),
         target_os = "xous",
-    ))] {
+    ) => {
         // FIXME: finally remove std support for wasm32-unknown-unknown
         // FIXME: add random data generation to xous
         mod unsupported;
         pub use unsupported::{fill_bytes, hashmap_random_keys};
     }
+    _ => {}
 }
 
 #[cfg(not(any(
diff --git a/library/std/src/sys/random/uefi.rs b/library/std/src/sys/random/uefi.rs
index 4a71d32fffe..697933f197b 100644
--- a/library/std/src/sys/random/uefi.rs
+++ b/library/std/src/sys/random/uefi.rs
@@ -55,12 +55,13 @@ mod rng_protocol {
 /// Port from [getrandom](https://github.com/rust-random/getrandom/blob/master/src/backends/rdrand.rs)
 #[cfg(any(target_arch = "x86_64", target_arch = "x86"))]
 mod rdrand {
-    cfg_if::cfg_if! {
-        if #[cfg(target_arch = "x86_64")] {
+    cfg_select! {
+        target_arch = "x86_64" => {
             use crate::arch::x86_64 as arch;
             use arch::_rdrand64_step as rdrand_step;
             type Word = u64;
-        } else if #[cfg(target_arch = "x86")] {
+        }
+        target_arch = "x86" => {
             use crate::arch::x86 as arch;
             use arch::_rdrand32_step as rdrand_step;
             type Word = u32;