dg_xch_core 2.1.0

Core library containing type/error definitions, CLVM tools, Consensus and Pool definitions
Documentation
use serde::Serialize;
use std::collections::VecDeque;
use std::time::{Duration, SystemTime};

#[derive(Default, Clone)]
pub struct RecentErrors<T: Clone + Serialize> {
    depth: usize,
    cache_duration: Duration,
    errors: VecDeque<(T, SystemTime)>,
}
impl<T: Clone + Serialize> RecentErrors<T> {
    #[must_use]
    pub fn new(depth: usize, cache_duration: Duration) -> Self {
        Self {
            depth,
            cache_duration,
            errors: VecDeque::default(),
        }
    }
    pub fn add(&mut self, t: T) {
        self.errors.push_front((t, SystemTime::now()));
        self.trim();
    }
    pub fn get(&mut self) -> Vec<(T, SystemTime)> {
        self.trim();
        self.errors.iter().cloned().collect()
    }
    pub fn trim(&mut self) {
        self.errors.truncate(self.depth);
        self.errors
            .retain(|(_, d)| match SystemTime::now().duration_since(*d) {
                Ok(dur) => dur < self.cache_duration,
                Err(_) => false,
            });
    }
}