azalea_block/
range.rs

1use std::{
2    collections::{hash_set, HashSet},
3    ops::{Add, RangeInclusive},
4};
5
6use crate::{block_state::BlockStateIntegerRepr, BlockState};
7
8#[derive(Debug, Clone)]
9pub struct BlockStates {
10    pub set: HashSet<BlockState>,
11}
12
13impl From<RangeInclusive<BlockStateIntegerRepr>> for BlockStates {
14    fn from(range: RangeInclusive<BlockStateIntegerRepr>) -> Self {
15        let mut set = HashSet::with_capacity((range.end() - range.start() + 1) as usize);
16        for id in range {
17            set.insert(BlockState { id });
18        }
19        Self { set }
20    }
21}
22
23impl IntoIterator for BlockStates {
24    type Item = BlockState;
25    type IntoIter = hash_set::IntoIter<BlockState>;
26
27    fn into_iter(self) -> Self::IntoIter {
28        self.set.into_iter()
29    }
30}
31
32impl BlockStates {
33    pub fn contains(&self, state: &BlockState) -> bool {
34        self.set.contains(state)
35    }
36}
37
38impl Add for BlockStates {
39    type Output = Self;
40
41    fn add(self, rhs: Self) -> Self::Output {
42        Self {
43            set: self.set.union(&rhs.set).copied().collect(),
44        }
45    }
46}
47
48impl From<HashSet<azalea_registry::Block>> for BlockStates {
49    fn from(set: HashSet<azalea_registry::Block>) -> Self {
50        Self {
51            set: set.into_iter().map(|b| b.into()).collect(),
52        }
53    }
54}
55
56impl From<&HashSet<azalea_registry::Block>> for BlockStates {
57    fn from(set: &HashSet<azalea_registry::Block>) -> Self {
58        Self {
59            set: set.iter().map(|&b| b.into()).collect(),
60        }
61    }
62}