3 /// Create an `OrderMap` from a list of key-value pairs
8 /// #[macro_use] extern crate ordermap;
11 /// let map = ordermap!{
15 /// assert_eq!(map["a"], 1);
16 /// assert_eq!(map["b"], 2);
17 /// assert_eq!(map.get("c"), None);
19 /// // "a" is the first key
20 /// assert_eq!(map.keys().next(), Some(&"a"));
23 macro_rules
! ordermap
{
24 (@single $
($x
:tt
)*) => (());
25 (@count $
($rest
:expr
),*) => (<[()]>::len(&[$
(ordermap
!(@single $rest
)),*]));
27 ($
($key
:expr
=> $value
:expr
,)+) => { ordermap!($($key => $value),+) }
;
28 ($
($key
:expr
=> $value
:expr
),*) => {
30 let _cap
= ordermap
!(@count $
($key
),*);
31 let mut _map
= $
crate::OrderMap
::with_capacity(_cap
);
33 _map
.insert($key
, $value
);
41 /// Create an `OrderSet` from a list of values
46 /// #[macro_use] extern crate ordermap;
49 /// let set = orderset!{
53 /// assert!(set.contains("a"));
54 /// assert!(set.contains("b"));
55 /// assert!(!set.contains("c"));
57 /// // "a" is the first value
58 /// assert_eq!(set.iter().next(), Some(&"a"));
61 macro_rules
! orderset
{
62 (@single $
($x
:tt
)*) => (());
63 (@count $
($rest
:expr
),*) => (<[()]>::len(&[$
(orderset
!(@single $rest
)),*]));
65 ($
($value
:expr
,)+) => { orderset!($($value),+) }
;
66 ($
($value
:expr
),*) => {
68 let _cap
= orderset
!(@count $
($value
),*);
69 let mut _set
= $
crate::OrderSet
::with_capacity(_cap
);
78 // generate all the Iterator methods by just forwarding to the underlying
79 // self.iter and mapping its element.
80 macro_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
84 fn next(&mut self) -> Option
<Self::Item
> {
85 self.iter
.next().map($map_elt
)
88 fn size_hint(&self) -> (usize, Option
<usize>) {
92 fn count(self) -> usize {
96 fn nth(&mut self, n
: usize) -> Option
<Self::Item
> {
97 self.iter
.nth(n
).map($map_elt
)
100 fn last(mut self) -> Option
<Self::Item
> {
104 fn collect
<C
>(self) -> C
105 where C
: FromIterator
<Self::Item
>
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()
114 macro_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
118 fn next_back(&mut self) -> Option
<Self::Item
> {
119 self.iter
.next_back().map($map_elt
)