graphplan/
macros.rs

1#[macro_export]
2/// Create a **HashSet** from a list of elements. Implementation
3/// copied from the maplit library https://github.com/bluss/maplit
4///
5/// ## Example
6///
7/// ```
8/// #[macro_use] extern crate graphplan;
9/// # fn main() {
10///
11/// let set = hashset!{"a", "b"};
12/// assert!(set.contains("a"));
13/// assert!(set.contains("b"));
14/// assert!(!set.contains("c"));
15/// # }
16/// ```
17macro_rules! hashset {
18    (@single $($x:tt)*) => (());
19    (@count $($rest:expr),*) => (<[()]>::len(&[$(hashset!(@single $rest)),*]));
20
21    ($($key:expr,)+) => { hashset!($($key),+) };
22    ($($key:expr),*) => {
23        {
24            let _cap = hashset!(@count $($key),*);
25            let mut _set = ::std::collections::HashSet::with_capacity(_cap);
26            $(
27                let _ = _set.insert($key);
28            )*
29                _set
30        }
31    };
32}
33
34#[macro_export]
35/// Create a **BTreeSet** from a list of elements. Implementation
36/// copied from the maplit library https://github.com/bluss/maplit
37///
38/// ## Example
39///
40/// ```
41/// #[macro_use] extern crate graphplan;
42/// # fn main() {
43///
44/// let set = btreeset!{"a", "b"};
45/// assert!(set.contains("a"));
46/// assert!(set.contains("b"));
47/// assert!(!set.contains("c"));
48/// # }
49/// ```
50macro_rules! btreeset {
51    ($($key:expr,)+) => (btreeset!($($key),+));
52
53    ( $($key:expr),* ) => {
54        {
55            let mut _set = ::std::collections::BTreeSet::new();
56            $(
57                _set.insert($key);
58            )*
59            _set
60        }
61    };
62}
63
64#[macro_export]
65/// Create a **HashMap** from a list of key-value pairs. Implementation
66/// copied from the maplit library https://github.com/bluss/maplit
67///
68/// ## Example
69///
70/// ```
71/// #[macro_use] extern crate graphplan;
72/// # fn main() {
73///
74/// let map = hashmap!{
75///     "a" => 1,
76///     "b" => 2,
77/// };
78/// assert_eq!(map["a"], 1);
79/// assert_eq!(map["b"], 2);
80/// assert_eq!(map.get("c"), None);
81/// # }
82/// ```
83macro_rules! hashmap {
84    (@single $($x:tt)*) => (());
85    (@count $($rest:expr),*) => (<[()]>::len(&[$(hashmap!(@single $rest)),*]));
86
87    ($($key:expr => $value:expr,)+) => { hashmap!($($key => $value),+) };
88    ($($key:expr => $value:expr),*) => {
89        {
90            let _cap = hashmap!(@count $($key),*);
91            let mut _map = ::std::collections::HashMap::with_capacity(_cap);
92            $(
93                let _ = _map.insert($key, $value);
94            )*
95            _map
96        }
97    };
98}