use std::collections::HashSet;
use std::ops::Deref;
use serde::{Deserialize, Serialize};
use crate::linked_data::Hash;
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct Pins(HashSet<Hash>);
impl Deref for Pins {
type Target = HashSet<Hash>;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl Default for Pins {
fn default() -> Self {
Self::new()
}
}
impl Pins {
pub fn new() -> Self {
Pins(HashSet::new())
}
pub fn insert(&mut self, hash: Hash) -> bool {
self.0.insert(hash)
}
pub fn extend<I>(&mut self, hashes: I)
where
I: IntoIterator<Item = Hash>,
{
self.0.extend(hashes)
}
pub fn len(&self) -> usize {
self.0.len()
}
pub fn is_empty(&self) -> bool {
self.0.is_empty()
}
pub fn contains(&self, hash: &Hash) -> bool {
self.0.contains(hash)
}
pub fn to_vec(&self) -> Vec<Hash> {
self.0.iter().copied().collect()
}
pub fn from_vec(hashes: Vec<Hash>) -> Self {
Pins(hashes.into_iter().collect())
}
pub fn iter(&self) -> impl Iterator<Item = &Hash> {
self.0.iter()
}
}