Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 4 additions & 11 deletions library/core/src/cell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -544,7 +544,7 @@ impl<T: Copy> Cell<T> {
unsafe { *self.value.get() }
}

/// Updates the contained value using a function and returns the new value.
/// Updates the contained value using a function.
///
/// # Examples
///
Expand All @@ -554,21 +554,14 @@ impl<T: Copy> Cell<T> {
/// use std::cell::Cell;
///
/// let c = Cell::new(5);
/// let new = c.update(|x| x + 1);
///
/// assert_eq!(new, 6);
/// c.update(|x| x + 1);
/// assert_eq!(c.get(), 6);
/// ```
#[inline]
#[unstable(feature = "cell_update", issue = "50186")]
pub fn update<F>(&self, f: F) -> T
where
F: FnOnce(T) -> T,
{
pub fn update(&self, f: impl FnOnce(T) -> T) {
let old = self.get();
let new = f(old);
self.set(new);
new
self.set(f(old));
}
}

Expand Down
4 changes: 2 additions & 2 deletions library/coretests/tests/cell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,10 @@ fn smoketest_cell() {
fn cell_update() {
let x = Cell::new(10);

assert_eq!(x.update(|x| x + 5), 15);
x.update(|x| x + 5);
assert_eq!(x.get(), 15);

assert_eq!(x.update(|x| x / 3), 5);
x.update(|x| x / 3);
assert_eq!(x.get(), 5);
}

Expand Down
Loading