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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
use std::sync::atomic::{AtomicU32, AtomicU64, Ordering};
use std::sync::Arc;
pub struct Counter<A> {
value: Arc<A>,
}
impl<A> Clone for Counter<A> {
fn clone(&self) -> Self {
Self {
value: self.value.clone(),
}
}
}
impl<A: Atomic> Counter<A> {
pub fn new() -> Self {
Counter {
value: Arc::new(A::new()),
}
}
pub fn inc(&self) -> A::Number {
self.value.inc()
}
pub fn get(&self) -> A::Number {
self.value.get()
}
}
pub trait Atomic {
type Number;
fn new() -> Self;
fn inc(&self) -> Self::Number;
fn get(&self) -> Self::Number;
}
impl<A> Default for Counter<A>
where
A: Default,
{
fn default() -> Self {
Self {
value: Arc::new(A::default()),
}
}
}
impl Atomic for AtomicU64 {
type Number = u64;
fn new() -> Self {
AtomicU64::new(0)
}
fn inc(&self) -> Self::Number {
self.fetch_add(1, Ordering::Relaxed)
}
fn get(&self) -> Self::Number {
self.load(Ordering::Relaxed)
}
}
impl Atomic for AtomicU32 {
type Number = u32;
fn new() -> Self {
AtomicU32::new(0)
}
fn inc(&self) -> Self::Number {
self.fetch_add(1, Ordering::Relaxed)
}
fn get(&self) -> Self::Number {
self.load(Ordering::Relaxed)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn inc_and_get() {
let counter = Counter::<AtomicU64>::new();
assert_eq!(0, counter.inc());
assert_eq!(1, counter.get());
}
}