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
#![allow(dead_code)]
use crate::ClockSource;
use std::{
sync::{
atomic::{AtomicU64, Ordering},
Arc,
},
time::Duration,
};
pub trait IntoNanoseconds {
fn into_nanos(self) -> u64;
}
impl IntoNanoseconds for u64 {
fn into_nanos(self) -> u64 {
self
}
}
impl IntoNanoseconds for Duration {
fn into_nanos(self) -> u64 {
self.as_nanos() as u64
}
}
#[derive(Debug, Clone)]
pub struct Mock {
offset: Arc<AtomicU64>,
}
impl Mock {
pub(crate) fn new() -> Self {
Self {
offset: Arc::new(AtomicU64::new(0)),
}
}
pub fn increment<N: IntoNanoseconds>(&self, amount: N) {
self.offset
.fetch_add(amount.into_nanos(), Ordering::Release);
}
pub fn decrement<N: IntoNanoseconds>(&self, amount: N) {
self.offset
.fetch_sub(amount.into_nanos(), Ordering::Release);
}
pub fn value(&self) -> u64 {
self.offset.load(Ordering::Acquire)
}
}
impl ClockSource for Mock {
fn now(&self) -> u64 {
self.offset.load(Ordering::Acquire)
}
fn start(&self) -> u64 {
self.now()
}
fn end(&self) -> u64 {
self.now()
}
}