Skip to content

Commit d9a16b4

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

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

library/std/src/thread/local.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -469,6 +469,28 @@ 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+
/// use std::cell::Cell;
479+
///
480+
/// thread_local! {
481+
/// static X: Cell<i32> = const { Cell::new(5) };
482+
/// }
483+
///
484+
/// X.update(|x| x + 1);
485+
/// assert_eq!(X.get(), 6);
486+
/// ```
487+
#[unstable(feature = "local_key_cell_update", issue = "143989")]
488+
pub fn update(&'static self, f: impl FnOnce(T) -> T)
489+
where
490+
T: Copy,
491+
{
492+
self.with(|cell| cell.update(f))
493+
}
472494
}
473495

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

0 commit comments

Comments
 (0)