about summary refs log tree commit diff
path: root/library/std/src/thread
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/thread
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/thread')
-rw-r--r--library/std/src/thread/current.rs13
-rw-r--r--library/std/src/thread/mod.rs14
2 files changed, 16 insertions, 11 deletions
diff --git a/library/std/src/thread/current.rs b/library/std/src/thread/current.rs
index 5c879903526..7da1621da45 100644
--- a/library/std/src/thread/current.rs
+++ b/library/std/src/thread/current.rs
@@ -18,8 +18,8 @@ local_pointer! {
 pub(super) mod id {
     use super::*;
 
-    cfg_if::cfg_if! {
-        if #[cfg(target_thread_local)] {
+    cfg_select! {
+        target_thread_local => {
             use crate::cell::Cell;
 
             #[thread_local]
@@ -34,7 +34,8 @@ pub(super) mod id {
             pub(super) fn set(id: ThreadId) {
                 ID.set(Some(id))
             }
-        } else if #[cfg(target_pointer_width = "16")] {
+        }
+        target_pointer_width = "16" => {
             local_pointer! {
                 static ID0;
                 static ID16;
@@ -59,7 +60,8 @@ pub(super) mod id {
                 ID32.set(ptr::without_provenance_mut((val >> 32) as usize));
                 ID48.set(ptr::without_provenance_mut((val >> 48) as usize));
             }
-        } else if #[cfg(target_pointer_width = "32")] {
+        }
+        target_pointer_width = "32" => {
             local_pointer! {
                 static ID0;
                 static ID32;
@@ -78,7 +80,8 @@ pub(super) mod id {
                 ID0.set(ptr::without_provenance_mut(val as usize));
                 ID32.set(ptr::without_provenance_mut((val >> 32) as usize));
             }
-        } else {
+        }
+        _ => {
             local_pointer! {
                 static ID;
             }
diff --git a/library/std/src/thread/mod.rs b/library/std/src/thread/mod.rs
index 292323d0118..b6059c28cec 100644
--- a/library/std/src/thread/mod.rs
+++ b/library/std/src/thread/mod.rs
@@ -1212,8 +1212,8 @@ impl ThreadId {
             panic!("failed to generate unique thread ID: bitspace exhausted")
         }
 
-        cfg_if::cfg_if! {
-            if #[cfg(target_has_atomic = "64")] {
+        cfg_select! {
+            target_has_atomic = "64" => {
                 use crate::sync::atomic::{Atomic, AtomicU64};
 
                 static COUNTER: Atomic<u64> = AtomicU64::new(0);
@@ -1229,7 +1229,8 @@ impl ThreadId {
                         Err(id) => last = id,
                     }
                 }
-            } else {
+            }
+            _ => {
                 use crate::sync::{Mutex, PoisonError};
 
                 static COUNTER: Mutex<u64> = Mutex::new(0);
@@ -1318,8 +1319,8 @@ use thread_name_string::ThreadNameString;
 /// Note however that this also means that the name reported in pre-main functions
 /// will be incorrect, but that's just something we have to live with.
 pub(crate) mod main_thread {
-    cfg_if::cfg_if! {
-        if #[cfg(target_has_atomic = "64")] {
+    cfg_select! {
+        target_has_atomic = "64" => {
             use super::ThreadId;
             use crate::sync::atomic::{Atomic, AtomicU64};
             use crate::sync::atomic::Ordering::Relaxed;
@@ -1335,7 +1336,8 @@ pub(crate) mod main_thread {
             pub(crate) unsafe fn set(id: ThreadId) {
                 MAIN.store(id.as_u64().get(), Relaxed)
             }
-        } else {
+        }
+        _ => {
             use super::ThreadId;
             use crate::mem::MaybeUninit;
             use crate::sync::atomic::{Atomic, AtomicBool};