I tried to specialized HashMap’s Index impl for Copy types, for the sake of convenience (though there are probably good reasons not to do this). My attempts have failed and I know little about specialization, is it somehow possible to make specialization accept this?
// Original from std.
impl<'a, K, Q: ?Sized, V, S> Index<&'a Q> for HashMap<K, V, S>
where K: Eq + Hash + Borrow<Q>,
Q: Eq + Hash,
S: BuildHasher
{
type Output = V;
default fn index(&self, index: &Q) -> &V {
self.get(index).expect("no entry found for key")
}
}
// Trying to specialize for Copy types.
impl<K, V, S> Index<K> for HashMap<K, V, S>
where K: Copy + Eq + Hash,
S: BuildHasher
{
type Output = V;
fn index(&self, index: K) -> &V {
self.get(&index).expect("no entry found for key")
}
}