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 pub fn new() -> Self {
27 Pins(HashSet::new())
28 }
29
30 pub fn insert(&mut self, hash: Hash) -> bool {
32 self.0.insert(hash)
33 }
34
35 pub fn extend<I>(&mut self, hashes: I)
37 where
38 I: IntoIterator<Item = Hash>,
39 {
40 self.0.extend(hashes)
41 }
42
43 pub fn len(&self) -> usize {
45 self.0.len()
46 }
47
48 pub fn is_empty(&self) -> bool {
50 self.0.is_empty()
51 }
52
53 pub fn contains(&self, hash: &Hash) -> bool {
55 self.0.contains(hash)
56 }
57
58 pub fn to_vec(&self) -> Vec<Hash> {
60 self.0.iter().copied().collect()
61 }
62
63 pub fn from_vec(hashes: Vec<Hash>) -> Self {
65 Pins(hashes.into_iter().collect())
66 }
67
68 pub fn iter(&self) -> impl Iterator<Item = &Hash> {
70 self.0.iter()
71 }
72}