-
Notifications
You must be signed in to change notification settings - Fork 172
/
counter.rs
55 lines (47 loc) · 1.27 KB
/
counter.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
use std::sync::atomic::{AtomicU64, Ordering};
use std::sync::Arc;
use crate::numerics::MOST_POSITIVE_EXACT_FLOAT;
const MAX_ID: u64 = (MOST_POSITIVE_EXACT_FLOAT - 1) as u64;
#[derive(Clone, Debug)]
pub struct Counter {
next: Arc<AtomicU64>,
}
impl Default for Counter {
fn default() -> Self {
Self {
next: Arc::new(AtomicU64::new(1)),
}
}
}
impl Counter {
/// Create a new counter starting at `start`.
#[cfg(test)]
pub fn with_start(start: u64) -> Self {
Self {
next: Arc::new(AtomicU64::new(start)),
}
}
/// Return a monotonically increasing integer ID.
///
/// Wraps around at 52 bits of precision so that it can be safely
/// coerced to an IEEE-754 double-float (f64).
pub fn next(&self) -> u64 {
if self
.next
.compare_exchange(MAX_ID, 1, Ordering::SeqCst, Ordering::SeqCst)
== Ok(MAX_ID)
{
MAX_ID
} else {
self.next.fetch_add(1, Ordering::SeqCst)
}
}
}
#[test]
fn test_id_wrapping() {
let counter = Counter::with_start(MAX_ID - 1);
assert_eq!(MAX_ID - 1, counter.next());
assert_eq!(MAX_ID, counter.next());
assert_eq!(1, counter.next());
assert_eq!(2, counter.next());
}