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 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275
//! Collection macros.
/// A macro that counts the number of times a pattern is repeated.
///
/// # Examples
///
/// ```
/// use bomboni_common::count_repeating;
///
/// let count = count_repeating!(1, 1, 2, 3, 5);
/// assert_eq!(count, 5);
/// ```
#[macro_export(local_inner_macros)]
macro_rules! count_repeating {
    (@single $($x:tt)*) => (());
    ($($rest:expr),*) => (<[()]>::len(&[$(count_repeating!(@single $rest)),*]));
}
/// A macro that creates a new `BTreeMap` instance with the given key-value pairs.
///
/// # Examples
///
/// Create a map of key-value pairs.
///
/// ```
/// use bomboni_common::btree_map;
///
/// let map = btree_map! {
///     1 => "first",
///     2 => "second",
/// };
/// assert_eq!(map.get(&1), Some(&"first"));
/// assert_eq!(map.get(&2), Some(&"second"));
/// ```
#[macro_export(local_inner_macros)]
macro_rules! btree_map {
    () => {
        ::std::collections::BTreeMap::new()
    };
    ($($key:expr => $value:expr),* $(,)?) => {{
        let mut _map = btree_map!();
        $(
            _map.insert($key, $value);
        )*
        _map
    }};
}
/// A macro that creates a new `BTreeMap` instance with the given key-value pairs.
/// The same as `btree_map!`, but converts keys and values to the target type.
///
/// # Examples
///
/// Create a map of key-value pairs.
///
/// ```
/// # use std::collections::BTreeMap;
/// use bomboni_common::btree_map_into;
///
/// let map: BTreeMap<i32, String> = btree_map_into! {
///     1 => "first",
///     2 => "second",
/// };
/// assert_eq!(map.get(&1), Some(&"first".to_string()));
/// assert_eq!(map.get(&2), Some(&"second".to_string()));
/// ```
#[macro_export(local_inner_macros)]
macro_rules! btree_map_into {
    ($($key:expr => $value:expr),* $(,)?) => {
        btree_map!($($key.into() => $value.into()),*)
    };
}
/// A macro that creates a new `HashMap` instance with the given key-value pairs.
///
/// # Examples
///
/// Create a map of key-value pairs.
///
/// ```
/// use bomboni_common::hash_map;
///
/// let map = hash_map! {
///     1 => "first",
///     2 => "second",
/// };
/// assert_eq!(map.get(&1), Some(&"first"));
/// assert_eq!(map.get(&2), Some(&"second"));
/// ```
///
/// Create a map with a given capacity.
///
/// ```
/// use bomboni_common::hash_map;
/// # use std::collections::HashMap;
///
/// let _map: HashMap<i32, String> = hash_map!(100);
/// ```
#[macro_export(local_inner_macros)]
macro_rules! hash_map {
    () => {
        ::std::collections::HashMap::new()
    };
    ($capacity:expr) => {
        ::std::collections::HashMap::with_capacity($capacity)
    };
    ($($key:expr => $value:expr),* $(,)?) => {{
        let mut _map = hash_map!(count_repeating!($($key),*));
        $(
            _map.insert($key, $value);
        )*
        _map
    }};
}
/// A macro that creates a new `HashMap` instance with the given key-value pairs.
/// The same as `hash_map!`, but converts keys and values to the target type.
///
/// # Examples
///
/// Create a map of key-value pairs.
///
/// ```
/// # use std::collections::HashMap;
/// use bomboni_common::hash_map_into;
///
/// let map: HashMap<i32, String> = hash_map_into! {
///     1 => "first",
///     2 => "second",
/// };
/// assert_eq!(map.get(&1), Some(&"first".to_string()));
/// assert_eq!(map.get(&2), Some(&"second".to_string()));
/// ```
#[macro_export(local_inner_macros)]
macro_rules! hash_map_into {
    ($($key:expr => $value:expr),* $(,)?) => {
        hash_map!($($key.into() => $value.into()),*)
    };
}
/// A macro that creates a new `BTreeSet` and inserts the given values into it.
///
/// # Examples
///
/// ```
/// use bomboni_common::btree_set;
///
/// let set = btree_set![1, 2, 3];
/// assert!(set.contains(&1));
/// assert!(set.contains(&2));
/// assert_eq!(set.len(), 3);
/// ```
#[macro_export(local_inner_macros)]
macro_rules! btree_set {
    () => {
        ::std::collections::BTreeSet::new()
    };
    ($($value:expr),* $(,)?) => {{
        let mut _set = btree_set!();
        $(
            _set.insert($value);
        )*
        _set
    }};
}
/// A macro that creates a new `BTreeSet` and inserts the given values into it.
/// The same as `btree_set!`, but converts values to the target type.
///
/// # Examples
///
/// ```
/// # use std::collections::BTreeSet;
/// use bomboni_common::btree_set_into;
///
/// let set: BTreeSet<i32> = btree_set_into![1, 2, 3];
/// assert!(set.contains(&1));
/// assert!(set.contains(&2));
/// assert_eq!(set.len(), 3);
/// ```
#[macro_export(local_inner_macros)]
macro_rules! btree_set_into {
    ($($value:expr),* $(,)?) => {
        btree_set!($($value.into()),*)
    };
}
/// A macro that creates a new `HashSet` and inserts the given values into it.
///
/// # Examples
///
/// ```
/// use bomboni_common::hash_set;
///
/// let set = hash_set![1, 2, 3];
/// assert!(set.contains(&1));
/// assert!(set.contains(&2));
/// assert_eq!(set.len(), 3);
/// ```
#[macro_export(local_inner_macros)]
macro_rules! hash_set {
    () => {
        ::std::collections::HashSet::new()
    };
    ($($value:expr),* $(,)?) => {{
        let mut _set = hash_set!();
        $(
            _set.insert($value);
        )*
        _set
    }};
}
/// A macro that creates a new `HashSet` and inserts the given values into it.
/// The same as `hash_set!`, but converts values to the target type.
///
/// # Examples
///
/// ```
/// # use std::collections::HashSet;
/// use bomboni_common::hash_set_into;
///
/// let set: HashSet<i32> = hash_set_into![1, 2, 3];
/// assert!(set.contains(&1));
/// assert!(set.contains(&2));
/// assert_eq!(set.len(), 3);
/// ```
#[macro_export(local_inner_macros)]
macro_rules! hash_set_into {
    ($($value:expr),* $(,)?) => {
        hash_set!($($value.into()),*)
    };
}
/// A macro that creates a new `VecDeque` instance with the given values.
///
/// # Examples
///
/// ```
/// # use std::collections::VecDeque;
/// use bomboni_common::vec_deque;
///
/// let deque = vec_deque![1, 2, 3];
/// assert_eq!(deque, VecDeque::from(vec![1, 2, 3]));
/// ```
#[macro_export(local_inner_macros)]
macro_rules! vec_deque {
    () => {
        ::std::collections::VecDeque::new()
    };
    ($elem:expr; $n:expr) => { ::std::vec![$elem; $n] };
    ($($value:expr),* $(,)?) => {{
        ::std::collections::VecDeque::from([
            $($value),*
        ])
    }};
}
/// A macro that creates a new `VecDeque` instance with the given values.
/// The same as `vec_deque!`, but converts values to the target type.
///
/// # Examples
///
/// ```
/// # use std::collections::VecDeque;
/// use bomboni_common::vec_deque_into;
///
/// let deque: VecDeque<i32> = vec_deque_into![1, 2, 3];
/// assert_eq!(deque, VecDeque::from(vec![1, 2, 3]));
/// ```
#[macro_export(local_inner_macros)]
macro_rules! vec_deque_into {
    ($($value:expr),* $(,)?) => {
        vec_deque!($($value.into()),*)
    };
}