diff options
| author | Cameron Steffen <cam.steffen94@gmail.com> | 2025-07-15 14:14:22 -0500 |
|---|---|---|
| committer | Cameron Steffen <cam.steffen94@gmail.com> | 2025-07-15 16:17:57 -0500 |
| commit | 1120cb2fe54fae7a1a8ccd5beb941f5b207084c0 (patch) | |
| tree | 29a255ea162ad49c1facff3188c04cfac64171e0 /library/std/src/thread/local.rs | |
| parent | 3014e79f9c8d5510ea7b3a3b70d171d0948b1e96 (diff) | |
| download | rust-1120cb2fe54fae7a1a8ccd5beb941f5b207084c0.tar.gz rust-1120cb2fe54fae7a1a8ccd5beb941f5b207084c0.zip | |
Add LocalKey<Cell>::update
Diffstat (limited to 'library/std/src/thread/local.rs')
| -rw-r--r-- | library/std/src/thread/local.rs | 23 |
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>> { |
