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};

use crate::hash::AddressHash;

const DEFAULT_DISCOVERY_CACHE_SIZE: usize = 1024;

pub struct DiscoveryCache {
    max_size: usize,
    order: VecDeque<AddressHash>,
    set: HashSet<AddressHash>,
}

impl DiscoveryCache {
    pub fn new(max_size: usize) -> Self {
        Self {
            max_size,
            order: VecDeque::new(),
            set: HashSet::new(),
        }
    }

    pub fn seen(&self, hash: &AddressHash) -> bool {
        self.set.contains(hash)
    }

    pub fn mark_seen(&mut self, hash: AddressHash) -> bool {
        if self.set.contains(&hash) {
            return false;
        }

        self.set.insert(hash);
        self.order.push_back(hash);

        if self.order.len() > self.max_size {
            if let Some(old) = self.order.pop_front() {
                self.set.remove(&old);
            }
        }

        true
    }

    pub fn len(&self) -> usize {
        self.set.len()
    }

    pub fn is_empty(&self) -> bool {
        self.set.is_empty()
    }
}

impl Default for DiscoveryCache {
    fn default() -> Self {
        Self::new(DEFAULT_DISCOVERY_CACHE_SIZE)
    }
}