computation_types/
names.rs

1use std::collections::BTreeSet;
2
3pub type Name = &'static str;
4
5#[derive(Clone, Debug)]
6pub struct Names(BTreeSet<Name>);
7
8impl Default for Names {
9    fn default() -> Self {
10        Self::new()
11    }
12}
13
14#[macro_export]
15macro_rules! names {
16    ( ) => {
17        $crate::Names::new()
18    };
19    ( $name:literal ) => {
20        $crate::Names::singleton($name)
21    };
22    ( $name:literal, $( $rest:tt ),* ) => {
23        $crate::Names::singleton($name).union(names![$( $rest ),*])
24    };
25}
26
27impl Names {
28    pub fn new() -> Self {
29        Names(BTreeSet::new())
30    }
31
32    pub fn singleton(name: Name) -> Self {
33        Names(std::iter::once(name).collect())
34    }
35
36    pub fn union_many<'a>(names: impl IntoIterator<Item = &'a Names>) -> Self {
37        let mut set = BTreeSet::new();
38        for name in names.into_iter().flat_map(|names| names.iter()) {
39            set.insert(name);
40        }
41        Names(set)
42    }
43
44    pub fn union(mut self, mut other: Self) -> Self {
45        self.0.append(&mut other.0);
46        self
47    }
48
49    pub fn len(&self) -> usize {
50        self.0.len()
51    }
52
53    pub fn is_empty(&self) -> bool {
54        self.0.is_empty()
55    }
56
57    pub fn contains(&self, name: Name) -> bool {
58        self.0.contains(name)
59    }
60
61    pub fn iter(&self) -> impl Iterator<Item = Name> + '_ {
62        self.0.iter().copied()
63    }
64}