Pre: comment RcCounter.
This commit is contained in:
parent
1636134a72
commit
e984e02529
1 changed files with 13 additions and 0 deletions
|
@ -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)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue