about summary refs log tree commit diff
diff options
context:
space:
mode:
authorMara Bos <m-ou.se@m-ou.se>2024-11-19 17:50:42 +0000
committerMara Bos <m-ou.se@m-ou.se>2024-11-19 18:55:52 +0100
commitb96f023dbd044e70eddd208cd21a295f62e5b28b (patch)
treebdb3aaeb9ee3b92107c43ca6c2b7023b3e63e658
parent38b9a448c9a00ca25c6fca352273d819cf92dacb (diff)
downloadrust-b96f023dbd044e70eddd208cd21a295f62e5b28b.tar.gz
rust-b96f023dbd044e70eddd208cd21a295f62e5b28b.zip
Address review comments.
Co-authored-by: waffle <waffle.lapkin@gmail.com>
-rw-r--r--library/std/src/thread/spawnhook.rs13
1 files changed, 6 insertions, 7 deletions
diff --git a/library/std/src/thread/spawnhook.rs b/library/std/src/thread/spawnhook.rs
index 1513a5036cb..b979db6bd60 100644
--- a/library/std/src/thread/spawnhook.rs
+++ b/library/std/src/thread/spawnhook.rs
@@ -1,4 +1,5 @@
 use crate::cell::Cell;
+use crate::iter;
 use crate::sync::Arc;
 use crate::thread::Thread;
 
@@ -91,9 +92,10 @@ where
 {
     SPAWN_HOOKS.with(|h| {
         let mut hooks = h.take();
+        let next = hooks.first.take();
         hooks.first = Some(Arc::new(SpawnHook {
             hook: Box::new(move |thread| Box::new(hook(thread))),
-            next: hooks.first.take(),
+            next,
         }));
         h.set(hooks);
     });
@@ -113,12 +115,9 @@ pub(super) fn run_spawn_hooks(thread: &Thread) -> ChildSpawnHooks {
         snapshot
     });
     // Iterate over the hooks, run them, and collect the results in a vector.
-    let mut next: &Option<Arc<SpawnHook>> = &hooks.first;
-    let mut to_run = Vec::new();
-    while let Some(hook) = next {
-        to_run.push((hook.hook)(thread));
-        next = &hook.next;
-    }
+    let to_run: Vec<_> = iter::successors(hooks.first.as_deref(), |hook| hook.next.as_deref())
+        .map(|hook| (hook.hook)(thread))
+        .collect();
     // Pass on the snapshot of the hooks and the results to the new thread,
     // which will then run SpawnHookResults::run().
     ChildSpawnHooks { hooks, to_run }