From 37a6f7be285ae53c11f37a180bad497581e7923d Mon Sep 17 00:00:00 2001 From: Thom Date: Fri, 11 May 2018 02:03:09 -0700 Subject: [PATCH] Use Cell instead of AtomicUsize in RcCounter. (#646) r=rnewman --- core/src/counter.rs | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/core/src/counter.rs b/core/src/counter.rs index 8418ee49..b480b8e2 100644 --- a/core/src/counter.rs +++ b/core/src/counter.rs @@ -8,26 +8,22 @@ // CONDITIONS OF ANY KIND, either express or implied. See the License for the // specific language governing permissions and limitations under the License. +use std::cell::Cell; use std::rc::Rc; -use std::sync::atomic::{ - AtomicUsize, - Ordering, -}; - #[derive(Clone)] pub struct RcCounter { - c: Rc, + c: Rc>, } /// A simple shared counter. impl RcCounter { pub fn with_initial(value: usize) -> Self { - RcCounter { c: Rc::new(AtomicUsize::new(value)) } + RcCounter { c: Rc::new(Cell::new(value)) } } pub fn new() -> Self { - RcCounter { c: Rc::new(AtomicUsize::new(0)) } + RcCounter { c: Rc::new(Cell::new(0)) } } /// Return the next value in the sequence. @@ -43,7 +39,8 @@ impl RcCounter { /// assert_eq!(c.next(), 6); /// ``` pub fn next(&self) -> usize { - self.c.fetch_add(1, Ordering::SeqCst) + let current = self.c.get(); + self.c.replace(current + 1) } }