causal_hub/types/
states.rs1use fxhash::FxBuildHasher;
2use indexmap::{IndexMap, IndexSet};
3
4pub type Map<K, V> = IndexMap<K, V, FxBuildHasher>;
6pub type Set<T> = IndexSet<T, FxBuildHasher>;
8pub type Labels = Set<String>;
10pub type States = Map<String, Set<String>>;
12
13#[macro_export]
15macro_rules! set {
16 [] => { $crate::types::Set::default() };
17 [$($value:expr,)+] => { $crate::set!($($value),+) };
18 [$($value:expr),*] => {
19 {
20 const CAPACITY: usize = <[()]>::len(&[$({ stringify!($value); }),*]);
21 let mut set = $crate::types::Set::with_capacity_and_hasher(
22 CAPACITY,
23 fxhash::FxBuildHasher::default(),
24 );
25 $(
26 set.insert($value);
27 )*
28 set
29 }
30 };
31}
32
33#[macro_export]
35macro_rules! labels {
36 [] => { $crate::types::Labels::default() };
37 [$($label:expr),+ $(,)?] => {
38 {
39 const CAPACITY: usize = <[()]>::len(&[$({ stringify!($label); }),*]);
40 let mut labels = $crate::types::Labels::with_capacity_and_hasher(
41 CAPACITY,
42 fxhash::FxBuildHasher::default(),
43 );
44 $(
45 labels.insert(::std::string::ToString::to_string(&$label));
46 )*
47 labels
48 }
49 };
50}
51
52#[macro_export]
54macro_rules! map {
55 [] => { $crate::types::Map::default() };
56 [$(($key:expr, $value:expr),)+] => { $crate::map!($(($key, $value)),+) };
57 [$(($key:expr, $value:expr)),*] => {
58 {
59 const CAPACITY: usize = <[()]>::len(&[$({ stringify!($key); stringify!($value); }),*]);
60 let mut map = $crate::types::Map::with_capacity_and_hasher(
61 CAPACITY,
62 fxhash::FxBuildHasher::default(),
63 );
64 $(
65 map.insert($key, $value);
66 )*
67 map
68 }
69 };
70}
71
72#[macro_export]
74macro_rules! states {
75 [] => { $crate::types::States::default() };
76 [$(($label:expr, [$($state:expr),* $(,)?]),)+] => { $crate::states!($(($label, [$($state),*])),+) };
77 [$(($label:expr, [$($state:expr),* $(,)?])),*] => {
78 {
79 const CAPACITY: usize = <[()]>::len(&[$({ stringify!($label); $( stringify!($state); )* }),*]);
80 let mut states = $crate::types::States::with_capacity_and_hasher(
81 CAPACITY,
82 fxhash::FxBuildHasher::default(),
83 );
84 $(
85 let label = ::std::string::ToString::to_string(&$label);
87 let state = $crate::set![$(
89 ::std::string::ToString::to_string(&$state)
90 ),*];
91 states.insert(label, state);
93 )*
94 states
95 }
96 };
97}