ordermap/macros.rs
1
2#[macro_export]
3/// Create an `OrderMap` from a list of key-value pairs
4///
5/// ## Example
6///
7/// ```
8/// #[macro_use] extern crate ordermap;
9/// # fn main() {
10///
11/// let map = ordermap!{
12/// "a" => 1,
13/// "b" => 2,
14/// };
15/// assert_eq!(map["a"], 1);
16/// assert_eq!(map["b"], 2);
17/// assert_eq!(map.get("c"), None);
18///
19/// // "a" is the first key
20/// assert_eq!(map.keys().next(), Some(&"a"));
21/// # }
22/// ```
23macro_rules! ordermap {
24 (@single $($x:tt)*) => (());
25 (@count $($rest:expr),*) => (<[()]>::len(&[$(ordermap!(@single $rest)),*]));
26
27 ($($key:expr => $value:expr,)+) => { ordermap!($($key => $value),+) };
28 ($($key:expr => $value:expr),*) => {
29 {
30 let _cap = ordermap!(@count $($key),*);
31 let mut _map = $crate::OrderMap::with_capacity(_cap);
32 $(
33 _map.insert($key, $value);
34 )*
35 _map
36 }
37 };
38}
39
40#[macro_export]
41/// Create an `OrderSet` from a list of values
42///
43/// ## Example
44///
45/// ```
46/// #[macro_use] extern crate ordermap;
47/// # fn main() {
48///
49/// let set = orderset!{
50/// "a",
51/// "b",
52/// };
53/// assert!(set.contains("a"));
54/// assert!(set.contains("b"));
55/// assert!(!set.contains("c"));
56///
57/// // "a" is the first value
58/// assert_eq!(set.iter().next(), Some(&"a"));
59/// # }
60/// ```
61macro_rules! orderset {
62 (@single $($x:tt)*) => (());
63 (@count $($rest:expr),*) => (<[()]>::len(&[$(orderset!(@single $rest)),*]));
64
65 ($($value:expr,)+) => { orderset!($($value),+) };
66 ($($value:expr),*) => {
67 {
68 let _cap = orderset!(@count $($value),*);
69 let mut _set = $crate::OrderSet::with_capacity(_cap);
70 $(
71 _set.insert($value);
72 )*
73 _set
74 }
75 };
76}
77
78// generate all the Iterator methods by just forwarding to the underlying
79// self.iter and mapping its element.
80macro_rules! iterator_methods {
81 // $map_elt is the mapping function from the underlying iterator's element
82 // same mapping function for both options and iterators
83 ($map_elt:expr) => {
84 fn next(&mut self) -> Option<Self::Item> {
85 self.iter.next().map($map_elt)
86 }
87
88 fn size_hint(&self) -> (usize, Option<usize>) {
89 self.iter.size_hint()
90 }
91
92 fn count(self) -> usize {
93 self.iter.len()
94 }
95
96 fn nth(&mut self, n: usize) -> Option<Self::Item> {
97 self.iter.nth(n).map($map_elt)
98 }
99
100 fn last(mut self) -> Option<Self::Item> {
101 self.next_back()
102 }
103
104 fn collect<C>(self) -> C
105 where C: FromIterator<Self::Item>
106 {
107 // NB: forwarding this directly to standard iterators will
108 // allow it to leverage unstable traits like `TrustedLen`.
109 self.iter.map($map_elt).collect()
110 }
111 }
112}
113
114macro_rules! double_ended_iterator_methods {
115 // $map_elt is the mapping function from the underlying iterator's element
116 // same mapping function for both options and iterators
117 ($map_elt:expr) => {
118 fn next_back(&mut self) -> Option<Self::Item> {
119 self.iter.next_back().map($map_elt)
120 }
121 }
122}