ordermap/macros.rs
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 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
#[cfg(feature = "std")]
#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
#[macro_export]
/// Create an [`OrderMap`][crate::OrderMap] from a list of key-value pairs
///
/// ## Example
///
/// ```
/// use ordermap::ordermap;
///
/// 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 {
($($key:expr => $value:expr,)+) => { $crate::ordermap!($($key => $value),+) };
($($key:expr => $value:expr),*) => {
{
// Note: `stringify!($key)` is just here to consume the repetition,
// but we throw away that string literal during constant evaluation.
const CAP: usize = <[()]>::len(&[$({ stringify!($key); }),*]);
let mut map = $crate::OrderMap::with_capacity(CAP);
$(
map.insert($key, $value);
)*
map
}
};
}
#[cfg(feature = "std")]
#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
#[macro_export]
/// Create an [`OrderSet`][crate::OrderSet] from a list of values
///
/// ## Example
///
/// ```
/// use ordermap::orderset;
///
/// let set = orderset!{
/// "a",
/// "b",
/// };
/// assert!(set.contains("a"));
/// assert!(set.contains("b"));
/// assert!(!set.contains("c"));
///
/// // "a" is the first value
/// assert_eq!(set.iter().next(), Some(&"a"));
/// ```
macro_rules! orderset {
($($value:expr,)+) => { $crate::orderset!($($value),+) };
($($value:expr),*) => {
{
// Note: `stringify!($value)` is just here to consume the repetition,
// but we throw away that string literal during constant evaluation.
const CAP: usize = <[()]>::len(&[$({ stringify!($value); }),*]);
let mut set = $crate::OrderSet::with_capacity(CAP);
$(
set.insert($value);
)*
set
}
};
}