use crossbeam_utils::atomic::AtomicCell;
use std::{sync::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<AtomicCell<u64>>,
}
impl Mock {
pub(crate) fn new() -> Self {
Self {
offset: Arc::new(AtomicCell::new(0)),
}
}
pub fn increment<N: IntoNanoseconds>(&self, amount: N) {
let amount = amount.into_nanos();
self.offset
.fetch_update(|current| Some(current + amount))
.expect("should never return an error");
}
pub fn decrement<N: IntoNanoseconds>(&self, amount: N) {
let amount = amount.into_nanos();
self.offset
.fetch_update(|current| Some(current - amount))
.expect("should never return an error");
}
pub fn value(&self) -> u64 {
self.offset.load()
}
}