common/bucket/
pins.rs

1use std::collections::HashSet;
2use std::ops::Deref;
3
4use serde::{Deserialize, Serialize};
5
6use crate::linked_data::Hash;
7
8#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
9pub struct Pins(HashSet<Hash>);
10
11impl Deref for Pins {
12    type Target = HashSet<Hash>;
13    fn deref(&self) -> &Self::Target {
14        &self.0
15    }
16}
17
18impl Default for Pins {
19    fn default() -> Self {
20        Self::new()
21    }
22}
23
24impl Pins {
25    /// Create a new empty pin set
26    pub fn new() -> Self {
27        Pins(HashSet::new())
28    }
29
30    /// Insert a single hash into the pin set
31    pub fn insert(&mut self, hash: Hash) -> bool {
32        self.0.insert(hash)
33    }
34
35    /// Extend the pin set with an iterator of hashes
36    pub fn extend<I>(&mut self, hashes: I)
37    where
38        I: IntoIterator<Item = Hash>,
39    {
40        self.0.extend(hashes)
41    }
42
43    /// Get the number of pinned hashes
44    pub fn len(&self) -> usize {
45        self.0.len()
46    }
47
48    /// Check if the pin set is empty
49    pub fn is_empty(&self) -> bool {
50        self.0.is_empty()
51    }
52
53    /// Check if a hash is pinned
54    pub fn contains(&self, hash: &Hash) -> bool {
55        self.0.contains(hash)
56    }
57
58    /// Convert pins to a Vec for serialization
59    pub fn to_vec(&self) -> Vec<Hash> {
60        self.0.iter().copied().collect()
61    }
62
63    /// Create pins from a Vec
64    pub fn from_vec(hashes: Vec<Hash>) -> Self {
65        Pins(hashes.into_iter().collect())
66    }
67
68    /// Get an iterator over the hashes
69    pub fn iter(&self) -> impl Iterator<Item = &Hash> {
70        self.0.iter()
71    }
72}