indexmap icon indicating copy to clipboard operation
indexmap copied to clipboard

General hashes-like relation

Open bluss opened this issue 8 years ago • 1 comments

  • Example: Hash table that is keyed by newtyped strings and lookup is case-insensitive.
  • Use case: Want to lookup using &str.

bluss avatar Oct 13 '17 15:10 bluss

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?

RReverser avatar Feb 19 '18 22:02 RReverser