1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
#[macro_export] /// Create an `OrderMap` from a list of key-value pairs /// /// ## Example /// /// ``` /// #[macro_use] extern crate ordermap; /// # fn main() { /// /// let map = ordermap!{ /// "a" => 1, /// "b" => 2, /// }; /// assert_eq!(map["a"], 1); /// assert_eq!(map["b"], 2); /// assert_eq!(map.get("c"), None); /// /// // "a" is the first key /// assert_eq!(map.keys().next(), Some(&"a")); /// # } /// ``` macro_rules! ordermap { (@single $($x:tt)*) => (()); (@count $($rest:expr),*) => (<[()]>::len(&[$(ordermap!(@single $rest)),*])); ($($key:expr => $value:expr,)+) => { ordermap!($($key => $value),+) }; ($($key:expr => $value:expr),*) => { { let _cap = ordermap!(@count $($key),*); let mut _map = $crate::OrderMap::with_capacity(_cap); $( _map.insert($key, $value); )* _map } }; }