use std::time::Instant;
#[derive(Clone)]
pub struct WatchEntry<K: std::fmt::Debug + Clone> {
key: K,
time: Instant,
}
impl<K: std::fmt::Debug + Clone> WatchEntry<K> {
pub fn new(key: K) -> Self {
Self {
key,
time: Instant::now(),
}
}
}
impl<K: std::fmt::Debug + Clone> std::fmt::Debug for WatchEntry<K> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("WatchEntry")
.field("key", &self.key)
.field("time", &self.time)
.finish()
}
}
pub struct LowBullWatcher<K: std::fmt::Debug + Clone> {
floating_index: usize,
history: Vec<Option<WatchEntry<K>>>,
}
impl<K: std::fmt::Debug + Clone> LowBullWatcher<K> {
pub fn new(history_size: usize) -> Self {
Self {
floating_index: 0,
history: vec![None; history_size],
}
}
pub fn watch(&mut self, key: K) {
if self.floating_index == self.history.len() {
for i in 0..(self.history.len() - 1) {
let after = self.history[i].take();
self.history[i] = self.history[i + 1].take();
self.history[i + 1] = after;
}
}
if self.floating_index < self.history.len() {
self.floating_index += 1;
}
self.history[self.floating_index - 1] = Some(WatchEntry::new(key.clone()));
}
pub fn debug_history(&self) {
for entry in &self.history {
println!("{:?}", entry);
}
}
}