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 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586
/// Macro for creating a [`HashMap`](::std::collections::HashMap).
///
/// Syntactic sugar for [`HashMap::from`](::std::collections::HashMap::from).
///
/// # Examples
///
/// ```rust
/// use map_macro::hash_map;
///
/// let goodbye = hash_map! {
/// "en" => "Goodbye",
/// "de" => "Auf Wiedersehen",
/// "fr" => "Au revoir",
/// "es" => "Adios",
/// "cat" => "Adéu",
/// };
/// ```
///
#[macro_export]
macro_rules! hash_map {
{$($k: expr => $v: expr),* $(,)?} => {
::std::collections::HashMap::from([$(($k, $v),)*])
};
}
/// Explicitly typed equivalent of [`hash_map!`].
///
/// See the [explicity typed macros](crate#explicitly-typed-macros) section.
///
/// # Examples
///
/// ```rust
/// use std::collections::HashMap;
/// use std::fmt::Debug;
///
/// use map_macro::hash_map_e;
///
/// let goodbye: HashMap<&str, &dyn Debug> = hash_map_e! {
/// "en" => &"Goodbye",
/// "de" => &"Auf Wiedersehen",
/// "fr" => &"Au revoir",
/// "es" => &"Adios",
/// "cat" => &"Adéu",
/// };
///
/// println!("{:?}", goodbye);
/// ```
///
#[macro_export]
macro_rules! hash_map_e {
{$($k: expr => $v: expr),* $(,)?} => {
::std::collections::HashMap::from([$(($k as _, $v as _),)*])
};
}
/// Macro for creating a [`BTreeMap`](::std::collections::BTreeMap).
///
/// Syntactic sugar for [`BTreeMap::from`](::std::collections::BTreeMap::from).
///
/// # Examples
///
/// ```rust
/// use map_macro::btree_map;
///
/// let goodbye = btree_map! {
/// "en" => "Goodbye",
/// "de" => "Auf Wiedersehen",
/// "fr" => "Au revoir",
/// "es" => "Adios",
/// "cat" => "Adéu",
/// };
/// ```
///
#[macro_export]
macro_rules! btree_map {
{$($k: expr => $v: expr),* $(,)?} => {
::std::collections::BTreeMap::from([$(($k, $v),)*])
};
}
/// Explicitly typed equivalent of [`btree_map!`].
///
/// See the [explicity typed macros](crate#explicitly-typed-macros) section.
///
/// # Examples
///
/// ```rust
/// use std::collections::BTreeMap;
/// use std::fmt::Debug;
///
/// use map_macro::btree_map_e;
///
/// let goodbye: BTreeMap<&str, &dyn Debug> = btree_map_e! {
/// "en" => &"Goodbye",
/// "de" => &"Auf Wiedersehen",
/// "fr" => &"Au revoir",
/// "es" => &"Adios",
/// "cat" => &"Adéu",
/// };
/// ```
///
#[macro_export]
macro_rules! btree_map_e {
{$($k: expr => $v: expr),* $(,)?} => {
::std::collections::BTreeMap::from([$(($k as _, $v as _),)*])
};
}
/// Macro for creating a [`HashSet`](::std::collections::HashSet).
///
/// Syntactic sugar for [`HashSet::from`](::std::collections::HashSet::from).
///
/// # Examples
///
/// ```rust
/// use map_macro::hash_set;
///
/// let x = hash_set! { 1, 2, 3, 3, 4 };
///
/// assert_eq!(x.len(), 4);
/// ```
///
#[macro_export]
macro_rules! hash_set {
{$($v: expr),* $(,)?} => {
::std::collections::HashSet::from([$($v,)*])
};
}
/// Explicitly typed equivalent of [`hash_set!`].
///
/// See the [explicity typed macros](crate#explicitly-typed-macros) section.
///
/// # Examples
///
/// ```rust
/// use std::collections::HashSet;
///
/// use map_macro::hash_set_e;
///
/// enum Foo { A, B, C, D }
///
/// let x: HashSet<u8> = hash_set_e! { Foo::A, Foo::B, Foo::C, Foo::C, Foo::D };
///
/// assert_eq!(x.len(), 4);
/// ```
///
#[macro_export]
macro_rules! hash_set_e {
{$($v: expr),* $(,)?} => {
::std::collections::HashSet::from([$($v as _,)*])
};
}
/// Macro for creating a [`BTreeSet`](::std::collections::BTreeSet).
///
/// Syntactic sugar for [`BTreeSet::from`](::std::collections::BTreeSet::from).
///
/// # Examples
///
/// ```rust
/// use map_macro::btree_set;
///
/// let x = btree_set! { 1, 2, 3, 3, 4 };
///
/// assert_eq!(x.len(), 4);
/// ```
///
#[macro_export]
macro_rules! btree_set {
{$($v: expr),* $(,)?} => {
::std::collections::BTreeSet::from([$($v,)*])
};
}
/// Explicitly typed equivalent of [`btree_set!`].
///
/// See the [explicity typed macros](crate#explicitly-typed-macros) section.
///
/// # Examples
///
/// ```rust
/// use std::collections::BTreeSet;
///
/// use map_macro::btree_set_e;
///
/// enum Foo { A, B, C, D }
///
/// let x: BTreeSet<u8> = btree_set_e! { Foo::A, Foo::B, Foo::C, Foo::C, Foo::D };
///
/// assert_eq!(x.len(), 4);
/// ```
///
#[macro_export]
macro_rules! btree_set_e {
{$($v: expr),* $(,)?} => {
::std::collections::BTreeSet::from([$($v as _,)*])
};
}
/// Macro for creating a [`VecDeque`](::std::collections::VecDeque).
///
/// Follows the same syntax as the [`vec!`](::std::vec!) macro.
///
/// # Examples
///
/// ```
/// use map_macro::vec_deque;
///
/// let v = vec_deque![0, 1, 2, 3];
/// let v = vec_deque![0; 4];
/// ```
///
#[macro_export]
macro_rules! vec_deque {
{$v: expr; $c: expr} => {
{
let mut vec = ::std::collections::VecDeque::with_capacity($c);
for _ in 0..$c {
vec.push_back($v);
}
vec
}
};
{$($v: expr),* $(,)?} => {
::std::collections::VecDeque::from([$($v,)*])
};
}
/// Explicitly typed equivalent of [`vec_deque!`].
///
/// See the [explicity typed macros](crate#explicitly-typed-macros) section.
///
/// # Examples
///
/// ```
/// use std::collections::VecDeque;
/// use std::fmt::Debug;
///
/// use map_macro::vec_deque_e;
///
/// let v: VecDeque<&dyn Debug> = vec_deque_e![&0, &1, &2, &3];
/// let v: VecDeque<&dyn Debug> = vec_deque_e![&0; 4];
/// ```
///
#[macro_export]
macro_rules! vec_deque_e {
{$v: expr; $c: expr} => {
{
let mut vec = ::std::collections::VecDeque::with_capacity($c);
for _ in 0..$c {
vec.push_back($v as _);
}
vec
}
};
{$($v: expr),* $(,)?} => {
::std::collections::VecDeque::from([$($v as _,)*])
};
}
/// Macro for creating a [`LinkedList`](::std::collections::LinkedList).
///
/// Follows the same syntax as the [`vec!`](::std::vec!) macro.
///
/// # Examples
///
/// ```
/// use map_macro::linked_list;
///
/// let v = linked_list![0, 1, 2, 3];
/// let v = linked_list![0; 4];
/// ```
///
#[macro_export]
macro_rules! linked_list {
{$v: expr; $c: expr} => {
{
let mut ll = ::std::collections::LinkedList::new();
for _ in 0..$c {
ll.push_back($v);
}
ll
}
};
{$($v: expr),* $(,)?} => {
::std::collections::LinkedList::from([$($v,)*])
};
}
/// Explicitly typed equivalent of [`linked_list!`].
///
/// See the [explicity typed macros](crate#explicitly-typed-macros) section.
///
/// # Examples
///
/// ```
/// use std::collections::LinkedList;
/// use std::fmt::Debug;
///
/// use map_macro::linked_list_e;
///
/// let v: LinkedList<&dyn Debug> = linked_list_e![&0, &1, &2, &3];
/// let v: LinkedList<&dyn Debug> = linked_list_e![&0; 4];
/// ```
///
#[macro_export]
macro_rules! linked_list_e {
{$v: expr; $c: expr} => {
{
let mut ll = ::std::collections::LinkedList::new();
for _ in 0..$c {
ll.push_back($v as _);
}
ll
}
};
{$($v: expr),* $(,)?} => {
::std::collections::LinkedList::from([$($v as _,)*])
};
}
/// Macro for creating a [`BinaryHeap`](::std::collections::BinaryHeap).
///
/// Follows the same syntax as the [`vec!`](::std::vec!) macro.
///
/// # Examples
///
/// ```
/// use map_macro::binary_heap;
///
/// let v = binary_heap![0, 1, 2, 3];
/// let v = binary_heap![0; 4];
/// ```
///
#[macro_export]
macro_rules! binary_heap {
{$v: expr; $c: expr} => {
{
let mut bh = ::std::collections::BinaryHeap::with_capacity($c);
for _ in 0..$c {
bh.push($v);
}
bh
}
};
{$($v: expr),* $(,)?} => {
::std::collections::BinaryHeap::from([$($v,)*])
};
}
/// Explicitly typed equivalent of [`binary_heap!`].
///
/// See the [explicity typed macros](crate#explicitly-typed-macros) section.
///
/// # Examples
///
/// ```
/// use std::collections::BinaryHeap;
///
/// use map_macro::binary_heap_e;
///
/// enum Foo { A, B, C, D }
///
/// let v: BinaryHeap<u8> = binary_heap_e![Foo::A, Foo::B, Foo::C, Foo::D];
/// let v: BinaryHeap<u8> = binary_heap_e![Foo::A; 4];
/// ```
///
#[macro_export]
macro_rules! binary_heap_e {
{$v: expr; $c: expr} => {
{
let mut bh = ::std::collections::BinaryHeap::with_capacity($c);
for _ in 0..$c {
bh.push($v as _);
}
bh
}
};
{$($v: expr),* $(,)?} => {
::std::collections::BinaryHeap::from([$($v as _,)*])
};
}
/// Version of the [`vec!`](::std::vec!) macro where the value does not have to implement [`Clone`].
///
/// Useful for unclonable types or where `Clone` is exerting undesired behaviour.
///
/// # Uncloneable Types
///
/// When using `vec![x; count]`, the type of `x` has to implement `Clone`, because
/// `x` is cloned `count - 1` times into all the vector elements except the first one.
/// For example, calling `vec!` will result in a panic during compile time here,
/// because `UnclonableWrapper` is not cloneable:
///
/// ```compile_fail
/// struct UnclonableWrapper(u8);
///
/// let x = vec![UnclonableWrapper(0); 5];
/// ```
///
/// The `vec_no_clone!` macro takes a different approach.
/// Instead of cloning `UnclonableWrapper(0)`, it treats it as an
/// [expression](https://doc.rust-lang.org/reference/expressions.html) which is
/// called 5 times in this case.
/// So 5 independent `UnclonableWrapper` objects, each with its own location in
/// memory, are created:
///
/// ```rust
/// use map_macro::vec_no_clone;
///
/// struct UnclonableWrapper(u8);
///
/// let x = vec_no_clone![UnclonableWrapper(0); 5];
///
/// assert_eq!(x.len(), 5);
/// ```
///
/// A real-world example where `vec_no_clone!` is a useful drop-in replacement
/// for `vec!` are [atomic types](::std::sync::atomic), which are not clonable:
///
/// ```rust
/// use std::sync::atomic::AtomicU8;
///
/// use map_macro::vec_no_clone;
///
/// let x = vec_no_clone![AtomicU8::new(0); 5];
///
/// assert_eq!(x.len(), 5);
/// ```
///
/// # Types where `Clone` exerts the wrong Behaviour
///
/// `vec_no_clone!` is not only useful for unclonable types, but also for types
/// where cloning them is not what you want.
/// The best example would be a reference counted pointer [`Rc`](::std::rc::Rc).
/// When you clone an `Rc`, a new instance referencing the same location in memory
/// is created.
/// If you'd rather have multiple independent reference counted pointers to
/// different memory locations, you can use `vec_no_clone!` as well:
///
/// ```rust
/// use map_macro::vec_no_clone;
///
/// use std::cell::RefCell;
/// use std::rc::Rc;
///
/// // simply clones the reference counted pointer for each element that
/// // is not the first
/// let shared_vec = vec![Rc::new(RefCell::new(0)); 2];
/// {
/// let mut first = shared_vec[0].borrow_mut();
/// *first += 1;
/// }
///
/// assert_eq!(*shared_vec[0].borrow(), 1);
///
/// // the second element is a clone of the reference counted pointer at
/// // the first element of the vector, referencing the same address in
/// // memory, therefore being mutated as well
/// assert_eq!(*shared_vec[1].borrow(), 1);
///
/// // the `vec_no_clone!` macro does not clone the object created by the
/// // first expression but instead calls the expression for each element
/// // in the vector, creating two independent objects, each with their
/// // own address in memory
/// let unshared_vec = vec_no_clone![Rc::new(RefCell::new(0)); 2];
///
/// {
/// let mut first = unshared_vec[0].borrow_mut();
/// *first += 1;
/// }
///
/// assert_eq!(*unshared_vec[0].borrow(), 1);
///
/// // the second element is not the same cloned reference counted
/// // pointer as it would be if it were constructed with the `vec!` macro
/// // from the standard library like it was above, therefore it is not
/// // mutated
/// assert_eq!(*unshared_vec[1].borrow(), 0);
/// ```
///
/// # Drawbacks of using Expressions
///
/// Since `vec_no_clone!` treats the value as an expression, you must provide the
/// initialization as input directly.
/// This, for example, won't work:
///
/// ```compile_fail
/// use map_macro::vec_no_clone;
///
/// struct UnclonableWrapper(u8);
///
/// let a = UnclonableWrapper(0);
///
/// // a will have moved into the first element of x, raising a compile
/// // time error for the second element.
/// let x = vec_no_clone![a; 5];
/// ```
///
/// # Processing Lists of Elements
///
/// You can also use the macro with a list of elements, like `vec!`.
/// In fact, `vec_no_clone!` falls back to `vec!` in this case:
///
/// ```rust
/// use map_macro::vec_no_clone;
///
/// let v1 = vec_no_clone![0, 1, 2, 3];
/// let v2 = vec![0, 1, 2, 3];
///
/// assert_eq!(v1, v2);
///
/// let v1: Vec<u8> = vec_no_clone![];
/// let v2: Vec<u8> = vec![];
///
/// assert_eq!(v1, v2);
/// ```
///
#[macro_export]
macro_rules! vec_no_clone {
{$v: expr; $c: expr} => {
{
let mut vec = Vec::with_capacity($c);
for _ in 0..$c {
vec.push($v);
}
vec
}
};
{$($v: expr),* $(,)?} => {
{
vec![$($v),*]
}
};
}
/// Explicitly typed equivalent of [`vec_no_clone!`].
///
/// See the [explicity typed macros](crate#explicitly-typed-macros) section.
///
/// # Examples
///
/// ```
/// use std::fmt::Display;
///
/// use map_macro::vec_no_clone_e;
///
/// let v: Vec<&dyn Display> = vec_no_clone_e![&0; 4];
/// ```
///
#[macro_export]
macro_rules! vec_no_clone_e {
{$v: expr; $c: expr} => {
{
let mut vec = Vec::with_capacity($c);
for _ in 0..$c {
vec.push($v as _);
}
vec
}
};
{$($v: expr),* $(,)?} => {
{
vec![$($v as _),*]
}
};
}