macro_rules! id_hash_map {
($($item:expr,)+) => { ... };
($($item:expr),*) => { ... };
($H:ty; $($item:expr,)+) => { ... };
($H:ty; $($item:expr),*) => { ... };
}
Expand description
Creates an IdHashMap
from a list of items.
An optional BuildHasher
that implements
Default
can be provided as the first argument, followed by a semicolon.
§Panics
Panics if the list of items has duplicate keys. For better error handling,
the item is required to implement Debug
.
§Examples
use iddqd::{IdHashItem, id_hash_map, id_upcast};
#[derive(Debug)]
struct User {
id: u32,
name: String,
}
impl IdHashItem for User {
type Key<'a> = u32;
fn key(&self) -> Self::Key<'_> {
self.id
}
id_upcast!();
}
let map = id_hash_map! {
User { id: 1, name: "Alice".to_string() },
User { id: 2, name: "Bob".to_string() },
};
assert_eq!(map.get(&1).unwrap().name, "Alice");
assert_eq!(map.get(&2).unwrap().name, "Bob");
// With a custom hasher:
let map = id_hash_map! {
foldhash::quality::RandomState;
User { id: 3, name: "Charlie".to_string() },
User { id: 4, name: "Eve".to_string() },
};
assert_eq!(map.get(&3).unwrap().name, "Charlie");
assert_eq!(map.get(&4).unwrap().name, "Eve");