1 | use core::borrow::Borrow; |
2 | |
3 | /// Key equivalence trait. |
4 | /// |
5 | /// This trait allows hash table lookup to be customized. |
6 | /// It has one blanket implementation that uses the regular `Borrow` solution, |
7 | /// just like `HashMap` and `BTreeMap` do, so that you can pass `&str` to lookup |
8 | /// into a map with `String` keys and so on. |
9 | /// |
10 | /// # Contract |
11 | /// |
12 | /// The implementor **must** hash like `K`, if it is hashable. |
13 | pub trait Equivalent<K: ?Sized> { |
14 | /// Compare self to `key` and return `true` if they are equal. |
15 | fn equivalent(&self, key: &K) -> bool; |
16 | } |
17 | |
18 | impl<Q: ?Sized, K: ?Sized> Equivalent<K> for Q |
19 | where |
20 | Q: Eq, |
21 | K: Borrow<Q>, |
22 | { |
23 | #[inline ] |
24 | fn equivalent(&self, key: &K) -> bool { |
25 | *self == *key.borrow() |
26 | } |
27 | } |
28 | |