use std::{cell::Cell, ops::Add};
thread_local! {
pub static STABLE_TIME: Cell<i64> = const { Cell::new(1514817556) };
pub static STEP: Cell<Step> = Cell::new(Step::default());
}
#[derive(Clone, Copy)]
struct Step(i64);
impl Default for Step {
fn default() -> Self {
Self(1)
}
}
impl Add<Step> for i64 {
type Output = i64;
fn add(self, rhs: Step) -> Self::Output {
self + rhs.0
}
}
impl Add<i64> for Step {
type Output = Step;
fn add(self, rhs: i64) -> Self::Output {
Step(self.0 + rhs)
}
}
#[allow(clippy::unwrap_used)]
pub fn read_timestamp() -> i64 {
STABLE_TIME.get()
}
#[allow(clippy::unwrap_used)]
pub fn with_advanced_timestamp<F, T>(f: F) -> T
where
F: FnOnce() -> T,
{
let step = STEP.get();
let original = read_timestamp();
STABLE_TIME.replace(original + step);
let result = f();
STEP.replace(step + 1);
result
}