Apply requested API changes to cell_update
Do the following: * Switch to `impl FnOnce` rather than a generic `F`. * Change `update` to return nothing. This was discussed at a libs-api meeting [1]. Tracking issue: https://github.com/rust-lang/rust/issues/50186 [1]: https://github.com/rust-lang/rust/pull/134446#issuecomment-2770842949
This commit is contained in:
parent
4f0de4c81d
commit
072aa9e66f
2 changed files with 6 additions and 13 deletions
|
@ -544,7 +544,7 @@ impl<T: Copy> Cell<T> {
|
||||||
unsafe { *self.value.get() }
|
unsafe { *self.value.get() }
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Updates the contained value using a function and returns the new value.
|
/// Updates the contained value using a function.
|
||||||
///
|
///
|
||||||
/// # Examples
|
/// # Examples
|
||||||
///
|
///
|
||||||
|
@ -554,21 +554,14 @@ impl<T: Copy> Cell<T> {
|
||||||
/// use std::cell::Cell;
|
/// use std::cell::Cell;
|
||||||
///
|
///
|
||||||
/// let c = Cell::new(5);
|
/// let c = Cell::new(5);
|
||||||
/// let new = c.update(|x| x + 1);
|
/// c.update(|x| x + 1);
|
||||||
///
|
|
||||||
/// assert_eq!(new, 6);
|
|
||||||
/// assert_eq!(c.get(), 6);
|
/// assert_eq!(c.get(), 6);
|
||||||
/// ```
|
/// ```
|
||||||
#[inline]
|
#[inline]
|
||||||
#[unstable(feature = "cell_update", issue = "50186")]
|
#[unstable(feature = "cell_update", issue = "50186")]
|
||||||
pub fn update<F>(&self, f: F) -> T
|
pub fn update(&self, f: impl FnOnce(T) -> T) {
|
||||||
where
|
|
||||||
F: FnOnce(T) -> T,
|
|
||||||
{
|
|
||||||
let old = self.get();
|
let old = self.get();
|
||||||
let new = f(old);
|
self.set(f(old));
|
||||||
self.set(new);
|
|
||||||
new
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -50,10 +50,10 @@ fn smoketest_cell() {
|
||||||
fn cell_update() {
|
fn cell_update() {
|
||||||
let x = Cell::new(10);
|
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.get(), 15);
|
||||||
|
|
||||||
assert_eq!(x.update(|x| x / 3), 5);
|
x.update(|x| x / 3);
|
||||||
assert_eq!(x.get(), 5);
|
assert_eq!(x.get(), 5);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue