use std::{ collections::HashSet, ops::{Add, RangeInclusive}, }; use crate::{BlockState, BlockStateIntegerRepr}; #[derive(Debug, Clone)] pub struct BlockStates { pub set: HashSet, } impl From> for BlockStates { fn from(range: RangeInclusive) -> Self { let mut set = HashSet::with_capacity((range.end() - range.start() + 1) as usize); for id in range { set.insert(BlockState { id }); } Self { set } } } impl IntoIterator for BlockStates { type Item = BlockState; type IntoIter = std::collections::hash_set::IntoIter; fn into_iter(self) -> Self::IntoIter { self.set.into_iter() } } impl BlockStates { pub fn contains(&self, state: &BlockState) -> bool { self.set.contains(state) } } impl Add for BlockStates { type Output = Self; fn add(self, rhs: Self) -> Self::Output { Self { set: self.set.union(&rhs.set).copied().collect(), } } } impl From> for BlockStates { fn from(set: HashSet) -> Self { Self { set: set.into_iter().map(|b| b.into()).collect(), } } } impl From<&HashSet> for BlockStates { fn from(set: &HashSet) -> Self { Self { set: set.iter().map(|&b| b.into()).collect(), } } }