use iddqd::{TriHashItem, TriHashMap, tri_upcast};
use std::path::{Path, PathBuf};
#[derive(Clone, Debug, PartialEq, Eq)]
struct MyStruct {
a: String,
b: usize,
c: PathBuf,
d: Vec<usize>,
}
#[derive(Clone, Debug, Hash, Eq, PartialEq)]
struct MyKey1<'a> {
b: usize,
c: &'a Path,
}
#[derive(Clone, Debug, Hash, Eq, PartialEq)]
struct MyKey2<'a> {
c: &'a Path,
d: &'a [usize],
}
impl TriHashItem for MyStruct {
type K1<'a> = MyKey1<'a>;
type K2<'a> = MyKey2<'a>;
type K3<'a> = String;
fn key1(&self) -> Self::K1<'_> {
MyKey1 { b: self.b, c: &self.c }
}
fn key2(&self) -> Self::K2<'_> {
MyKey2 { c: &self.c, d: &self.d }
}
fn key3(&self) -> Self::K3<'_> {
self.a.clone()
}
tri_upcast!();
}
fn main() {
let mut map = TriHashMap::new();
let item = MyStruct {
a: "example".to_owned(),
b: 20,
c: PathBuf::from("/"),
d: Vec::new(),
};
map.insert_unique(item.clone()).unwrap();
map.insert_unique(MyStruct {
a: "example".to_owned(),
b: 30,
c: PathBuf::from("/xyz"),
d: vec![0],
})
.unwrap_err();
assert_eq!(map.get1(&MyKey1 { b: 20, c: Path::new("/") }), Some(&item));
assert_eq!(map.get3("example"), Some(&item));
for item in &map {
println!("item: {item:?}");
}
}