macro_rules! bi_hash_map {
($($item:expr,)+) => { ... };
($($item:expr),*) => { ... };
($H:ty; $($item:expr,)+) => { ... };
($H:ty; $($item:expr),*) => { ... };
}
Expand description
Creates a BiHashMap
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::{BiHashItem, bi_hash_map, bi_upcast};
#[derive(Debug)]
struct User {
id: u32,
name: String,
}
impl BiHashItem for User {
type K1<'a> = u32;
type K2<'a> = &'a str;
fn key1(&self) -> Self::K1<'_> {
self.id
}
fn key2(&self) -> Self::K2<'_> {
&self.name
}
bi_upcast!();
}
let map = bi_hash_map! {
User { id: 1, name: "Alice".to_string() },
User { id: 2, name: "Bob".to_string() },
};
assert_eq!(map.get1(&1).unwrap().name, "Alice");
assert_eq!(map.get2("Bob").unwrap().id, 2);
// With a custom hasher:
let map = bi_hash_map! {
foldhash::quality::RandomState;
User { id: 3, name: "Charlie".to_string() },
User { id: 4, name: "Eve".to_string() },
};
assert_eq!(map.get1(&3).unwrap().name, "Charlie");
assert_eq!(map.get2("Eve").unwrap().id, 4);