reticulum-rs 0.1.3

Reticulum-rs is a Rust implementation of the Reticulum Network Stack - a cryptographic, decentralised, and resilient mesh networking protocol designed for communication over any physical layer. This project is open source and community-owned, focused on bringing Reticulum capabilities to the Rust ecosystem with clear APIs, reproducible behavior, and portable deployment options.
Documentation
use std::collections::{HashSet, VecDeque};

pub struct CacheSet<T: std::hash::Hash + Eq + Clone> {
    capacity: usize,
    set: HashSet<T>,
    queue: VecDeque<T>,
}

impl<T: std::hash::Hash + Eq + Clone> CacheSet<T> {
    pub fn new(capacity: usize) -> Self {
        Self {
            capacity,
            set: HashSet::new(),
            queue: VecDeque::new(),
        }
    }

    pub fn insert(&mut self, value: &T) -> bool {
        if self.set.contains(value) {
            return false;
        }

        if self.set.len() == self.capacity {
            if let Some(oldest) = self.queue.pop_front() {
                self.set.remove(&oldest);
            }
        }

        self.set.insert(value.clone());
        self.queue.push_back(value.clone());

        true
    }

    pub fn contains(&self, value: &T) -> bool {
        self.set.contains(value)
    }
}