indexmap
indexmap copied to clipboard
General hashes-like relation
- Example: Hash table that is keyed by newtyped strings and lookup is case-insensitive.
- Use case: Want to lookup using
&str.
FWIW I'm currently doing this with newtype, which generally works pretty well:
pub struct ProductionSetItem(Production);
impl Deref for ProductionSetItem {
type Target = Production;
fn deref(&self) -> &Production {
&self.0
}
}
impl Borrow<str> for ProductionSetItem {
fn borrow(&self) -> &str {
&self.name
}
}
impl PartialEq for ProductionSetItem {
fn eq(&self, other: &Self) -> bool {
self.name == other.name
}
}
impl Eq for ProductionSetItem {}
impl Hash for ProductionSetItem {
fn hash<H: Hasher>(&self, state: &mut H) {
self.name.hash(state)
}
}
Maybe it's worth just providing some macro to generate them automatically?