From f86deef5b6059b9a0a76d9f0dafb95ced85f7449 Mon Sep 17 00:00:00 2001 From: Stjepan Glavina Date: Fri, 6 Apr 2018 15:02:21 +0200 Subject: [PATCH 1/5] Add Cell::update --- src/libcore/cell.rs | 24 ++++++++++++++++++++++++ src/libcore/tests/cell.rs | 11 +++++++++++ 2 files changed, 35 insertions(+) diff --git a/src/libcore/cell.rs b/src/libcore/cell.rs index c8ee166fee3e9..2ea1b84d03401 100644 --- a/src/libcore/cell.rs +++ b/src/libcore/cell.rs @@ -256,6 +256,30 @@ impl Cell { pub fn get(&self) -> T { unsafe{ *self.value.get() } } + + /// Applies a function to the contained value. + /// + /// # Examples + /// + /// ``` + /// use std::cell::Cell; + /// + /// let c = Cell::new(5); + /// c.update(|x| x + 1); + /// + /// assert_eq!(c.get(), 6); + /// ``` + #[inline] + #[unstable(feature = "cell_update", issue = "0")] // TODO: issue + pub fn update(&self, f: F) -> T + where + F: FnOnce(T) -> T, + { + let old = self.get(); + let new = f(old); + self.set(new); + new + } } #[stable(feature = "rust1", since = "1.0.0")] diff --git a/src/libcore/tests/cell.rs b/src/libcore/tests/cell.rs index cc0ef6a6f17e0..962fb2f0e027b 100644 --- a/src/libcore/tests/cell.rs +++ b/src/libcore/tests/cell.rs @@ -26,6 +26,17 @@ fn smoketest_cell() { assert!(y.get() == (30, 40)); } +#[test] +fn cell_update() { + let x = Cell::new(10); + + assert_eq!(x.update(|x| x + 5), 15); + assert_eq!(x.get(), 15); + + assert_eq!(x.update(|x| x / 3), 5); + assert_eq!(x.get(), 5); +} + #[test] fn cell_has_sensible_show() { let x = Cell::new("foo bar"); From 9377340ee3f638fcb011afd5a818d6e6b66c2d27 Mon Sep 17 00:00:00 2001 From: Stjepan Glavina Date: Fri, 6 Apr 2018 15:29:10 +0200 Subject: [PATCH 2/5] Change TODO to FIXME --- src/libcore/cell.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libcore/cell.rs b/src/libcore/cell.rs index 2ea1b84d03401..c0f7183b06c74 100644 --- a/src/libcore/cell.rs +++ b/src/libcore/cell.rs @@ -270,7 +270,7 @@ impl Cell { /// assert_eq!(c.get(), 6); /// ``` #[inline] - #[unstable(feature = "cell_update", issue = "0")] // TODO: issue + #[unstable(feature = "cell_update", issue = "0")] // FIXME: issue pub fn update(&self, f: F) -> T where F: FnOnce(T) -> T, From 5dcce519469690d09704b82d53417b67915adb94 Mon Sep 17 00:00:00 2001 From: Stjepan Glavina Date: Fri, 6 Apr 2018 22:45:31 +0200 Subject: [PATCH 3/5] Fix the failing tests --- src/libcore/cell.rs | 2 ++ src/libcore/tests/lib.rs | 1 + 2 files changed, 3 insertions(+) diff --git a/src/libcore/cell.rs b/src/libcore/cell.rs index c0f7183b06c74..e4a52d9c4a405 100644 --- a/src/libcore/cell.rs +++ b/src/libcore/cell.rs @@ -262,6 +262,8 @@ impl Cell { /// # Examples /// /// ``` + /// #![feature(cell_update)] + /// /// use std::cell::Cell; /// /// let c = Cell::new(5); diff --git a/src/libcore/tests/lib.rs b/src/libcore/tests/lib.rs index de7211e718c9f..13c53f47b6963 100644 --- a/src/libcore/tests/lib.rs +++ b/src/libcore/tests/lib.rs @@ -12,6 +12,7 @@ #![feature(ascii_ctype)] #![feature(box_syntax)] +#![feature(cell_update)] #![feature(core_float)] #![feature(core_private_bignum)] #![feature(core_private_diy_float)] From 1c0db245e0c87c2158f4a4456bf8e0a7aa14b677 Mon Sep 17 00:00:00 2001 From: Stjepan Glavina Date: Mon, 23 Apr 2018 20:23:04 +0200 Subject: [PATCH 4/5] Clarify the docs for Cell::update --- src/libcore/cell.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/libcore/cell.rs b/src/libcore/cell.rs index e4a52d9c4a405..64dbcbf7ae439 100644 --- a/src/libcore/cell.rs +++ b/src/libcore/cell.rs @@ -257,7 +257,7 @@ impl Cell { unsafe{ *self.value.get() } } - /// Applies a function to the contained value. + /// Updates the contained value using a function and returns the new value. /// /// # Examples /// @@ -267,8 +267,9 @@ impl Cell { /// use std::cell::Cell; /// /// let c = Cell::new(5); - /// c.update(|x| x + 1); + /// let new = c.update(|x| x + 1); /// + /// assert_eq!(new, 6); /// assert_eq!(c.get(), 6); /// ``` #[inline] From 29e9de85d6ae185d7d66c7ba0f2c418082d2df5f Mon Sep 17 00:00:00 2001 From: Stjepan Glavina Date: Mon, 23 Apr 2018 20:34:49 +0200 Subject: [PATCH 5/5] Assign the tracking issue --- src/libcore/cell.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libcore/cell.rs b/src/libcore/cell.rs index 64dbcbf7ae439..1ff187ed3f109 100644 --- a/src/libcore/cell.rs +++ b/src/libcore/cell.rs @@ -273,7 +273,7 @@ impl Cell { /// assert_eq!(c.get(), 6); /// ``` #[inline] - #[unstable(feature = "cell_update", issue = "0")] // FIXME: issue + #[unstable(feature = "cell_update", issue = "50186")] pub fn update(&self, f: F) -> T where F: FnOnce(T) -> T,