Pre: comment RcCounter.

This commit is contained in:
Richard Newman 2017-04-12 10:56:59 -07:00
parent 1636134a72
commit e984e02529

View file

@ -20,11 +20,24 @@ pub struct RcCounter {
c: Rc<AtomicUsize>, c: Rc<AtomicUsize>,
} }
/// A simple shared counter.
impl RcCounter { impl RcCounter {
pub fn new() -> Self { pub fn new() -> Self {
RcCounter { c: Rc::new(AtomicUsize::new(0)) } RcCounter { c: Rc::new(AtomicUsize::new(0)) }
} }
/// Return the next value in the sequence.
///
/// ```
/// use mentat_core::counter::RcCounter;
///
/// let c = RcCounter::with_initial(3);
/// assert_eq!(c.next(), 3);
/// assert_eq!(c.next(), 4);
/// let d = c.clone();
/// assert_eq!(d.next(), 5);
/// assert_eq!(c.next(), 6);
/// ```
pub fn next(&self) -> usize { pub fn next(&self) -> usize {
self.c.fetch_add(1, Ordering::SeqCst) self.c.fetch_add(1, Ordering::SeqCst)
} }