Skip to content

Commit 1120cb2

Browse files
committed
Add LocalKey<Cell>::update
1 parent 3014e79 commit 1120cb2

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

library/std/src/thread/local.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -469,6 +469,29 @@ impl<T: 'static> LocalKey<Cell<T>> {
469469
pub fn replace(&'static self, value: T) -> T {
470470
self.with(|cell| cell.replace(value))
471471
}
472+
473+
/// Updates the contained value using a function.
474+
///
475+
/// # Examples
476+
///
477+
/// ```
478+
/// #![feature(local_key_cell_update)]
479+
/// use std::cell::Cell;
480+
///
481+
/// thread_local! {
482+
/// static X: Cell<i32> = const { Cell::new(5) };
483+
/// }
484+
///
485+
/// X.update(|x| x + 1);
486+
/// assert_eq!(X.get(), 6);
487+
/// ```
488+
#[unstable(feature = "local_key_cell_update", issue = "143989")]
489+
pub fn update(&'static self, f: impl FnOnce(T) -> T)
490+
where
491+
T: Copy,
492+
{
493+
self.with(|cell| cell.update(f))
494+
}
472495
}
473496

474497
impl<T: 'static> LocalKey<RefCell<T>> {

0 commit comments

Comments
 (0)