macro_rules! tri_hash_map {
($($item:expr,)+) => { ... };
($($item:expr),*) => { ... };
($H:ty; $($item:expr,)+) => { ... };
($H:ty; $($item:expr),*) => { ... };
}
Expand description
Creates a TriHashMap
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::{TriHashItem, tri_hash_map, tri_upcast};
#[derive(Debug)]
struct Person {
id: u32,
name: String,
email: String,
}
impl TriHashItem for Person {
type K1<'a> = u32;
type K2<'a> = &'a str;
type K3<'a> = &'a str;
fn key1(&self) -> Self::K1<'_> {
self.id
}
fn key2(&self) -> Self::K2<'_> {
&self.name
}
fn key3(&self) -> Self::K3<'_> {
&self.email
}
tri_upcast!();
}
let map = tri_hash_map! {
Person { id: 1, name: "Alice".to_string(), email: "alice@example.com".to_string() },
Person { id: 2, name: "Bob".to_string(), email: "bob@example.com".to_string() },
};
assert_eq!(map.get1(&1).unwrap().name, "Alice");
assert_eq!(map.get2("Bob").unwrap().id, 2);
assert_eq!(map.get3("alice@example.com").unwrap().name, "Alice");
// With a custom hasher:
let map = tri_hash_map! {
foldhash::quality::RandomState;
Person { id: 3, name: "Charlie".to_string(), email: "charlie@example.com".to_string() },
Person { id: 4, name: "Eve".to_string(), email: "eve@example.com".to_string() },
};
assert_eq!(map.get1(&3).unwrap().name, "Charlie");
assert_eq!(map.get2("Eve").unwrap().id, 4);