about summary refs log tree commit diff
diff options
context:
space:
mode:
authorSamuel Tardieu <sam@rfc1149.net>2025-07-16 17:06:44 +0200
committerGitHub <noreply@github.com>2025-07-16 17:06:44 +0200
commit96a9df4cf9c51c656af501a9c82deb700c16e45a (patch)
tree6bc49365c6fab679afe2ab7fac13b5e86db1c5e9
parent2a8f97910a794f852b893213e059cab2ca12da7e (diff)
parent1120cb2fe54fae7a1a8ccd5beb941f5b207084c0 (diff)
downloadrust-96a9df4cf9c51c656af501a9c82deb700c16e45a.tar.gz
rust-96a9df4cf9c51c656af501a9c82deb700c16e45a.zip
Rollup merge of #143990 - camsteffen:localkey-cell, r=tgross35
Add LocalKey<Cell>::update

Tracking issue: rust-lang/rust#143989
-rw-r--r--library/std/src/thread/local.rs23
1 files changed, 23 insertions, 0 deletions
diff --git a/library/std/src/thread/local.rs b/library/std/src/thread/local.rs
index 7cd44873313..0ad014ccd3e 100644
--- a/library/std/src/thread/local.rs
+++ b/library/std/src/thread/local.rs
@@ -469,6 +469,29 @@ impl<T: 'static> LocalKey<Cell<T>> {
     pub fn replace(&'static self, value: T) -> T {
         self.with(|cell| cell.replace(value))
     }
+
+    /// Updates the contained value using a function.
+    ///
+    /// # Examples
+    ///
+    /// ```
+    /// #![feature(local_key_cell_update)]
+    /// use std::cell::Cell;
+    ///
+    /// thread_local! {
+    ///     static X: Cell<i32> = const { Cell::new(5) };
+    /// }
+    ///
+    /// X.update(|x| x + 1);
+    /// assert_eq!(X.get(), 6);
+    /// ```
+    #[unstable(feature = "local_key_cell_update", issue = "143989")]
+    pub fn update(&'static self, f: impl FnOnce(T) -> T)
+    where
+        T: Copy,
+    {
+        self.with(|cell| cell.update(f))
+    }
 }
 
 impl<T: 'static> LocalKey<RefCell<T>> {