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