I want a
Key trait would be kind of a mirror of the
Borrow trait, but it would live in
&'a T can also be represented in an hypothetical
Borrowed type as
Borrowed<'a, T>, that’s exactly what we do here. This allows huge amounts of flexibility.
Here’s an example
This could also be
#[derive(Eq, PartialEq, Hash, Key=MyKey)] struct MyStruct(String, u8); #[derive(Eq, PartialEq, Hash)] struct MyKey<'a>(&'a str, &'a u8); // need the & and only a single lifetime if we wanna use derive, I think
This would be extremely useful for some keys. Indeed,
(String, u8) is not hypothetical.
(This is not to be confused with “raw entry API”. I don’t want “raw entry API” I just want
HashMap.get to be better/more useful with custom key types. No,
Cow<'a, str> doesn’t work, I tried it and you just can’t move the map around (e.g. return it from a function) at all.)