HashMap, BtreeMap and compound type Borrow


The HashMap and BTreeMap API use the Borrow trait to implement type equivalent with regard to Hashing and Ordering. It allows to lookup in the Map without passing a value of the actual key type.

This works great for simple types, but it is impossible to do something like

let mut map = HashMap::<(u32, u32), String>::new();
map.insert((42, 42), "Hey".to_string());
map.get(&(&42, &42))

despite the fact that (&42, &42) and (42, 42) are hashing the same.

Indeed the Borrow trait has a method borrown(&self) -> &(&u32, &u32) in this case, and one can’t implement that. But the actual implementation doesn’t use the borrow method for real code, it is only called for testing purpose.

A way to specify type equivalence without this restriction could be useful.