Skip to content

Commit

Permalink
Small tweaks to the multimap!() macro (#30)
Browse files Browse the repository at this point in the history
* Small tweaks to the `multimap!()` macro

 1. Allow trailing comma.
 2. Prefix `MultiMap` with `$crate::` so there won't be name clash (see https://doc.rust-lang.org/reference/macros-by-example.html#hygiene).
 3. Instantiate the map with enough capacity to hold the elements, preventing further allocations (for the actual counting method, see https://danielkeep.github.io/tlborm/book/blk-counting.html#slice-length).

* Bump minor version
  • Loading branch information
ChayimFriedman2 authored Mar 8, 2021
1 parent 3add5f3 commit 594a516
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "multimap"
version = "0.8.2"
version = "0.8.3"
description = "A multimap implementation."
readme = "README.md"
repository = "https://github.com/havarnov/multimap"
Expand Down
7 changes: 5 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -875,9 +875,12 @@ impl<'a, K, V> ExactSizeIterator for IterMut<'a, K, V> {
///
/// ```
macro_rules! multimap{
($($key:expr => $value:expr),*)=>{
(@replace_with_unit $_t:tt) => { () };
(@count $($key:expr),*) => { <[()]>::len(&[$($crate::multimap! { @replace_with_unit $key }),*]) };

($($key:expr => $value:expr),* $(,)?)=>{
{
let mut map = MultiMap::new();
let mut map = $crate::MultiMap::with_capacity($crate::multimap! { @count $($key),* });
$(
map.insert($key,$value);
)*
Expand Down

0 comments on commit 594a516

Please sign in to comment.