| 1 |
use indexmap::indexmap; |
| 2 |
use indexmap::Equivalent; |
| 3 |
|
| 4 |
use std::hash::Hash; |
| 5 |
|
| 6 |
#[derive(Debug, Hash)] |
| 7 |
pub struct Pair<A, B>(pub A, pub B); |
| 8 |
|
| 9 |
impl<A, B, C, D> PartialEq<(A, B)> for Pair<C, D> |
| 10 |
where |
| 11 |
C: PartialEq<A>, |
| 12 |
D: PartialEq<B>, |
| 13 |
{ |
| 14 |
fn eq(&self, rhs: &(A, B)) -> bool { |
| 15 |
self.0 == rhs.0 && self.1 == rhs.1 |
| 16 |
} |
| 17 |
} |
| 18 |
|
| 19 |
impl<A, B, X> Equivalent<X> for Pair<A, B> |
| 20 |
where |
| 21 |
Pair<A, B>: PartialEq<X>, |
| 22 |
A: Hash + Eq, |
| 23 |
B: Hash + Eq, |
| 24 |
{ |
| 25 |
fn equivalent(&self, other: &X) -> bool { |
| 26 |
*self == *other |
| 27 |
} |
| 28 |
} |
| 29 |
|
| 30 |
#[test] |
| 31 |
fn test_lookup() { |
| 32 |
let s = String::from; |
| 33 |
let map = indexmap! { |
| 34 |
(s("a"), s("b")) => 1, |
| 35 |
(s("a"), s("x")) => 2, |
| 36 |
}; |
| 37 |
|
| 38 |
assert!(map.contains_key(&Pair("a", "b"))); |
| 39 |
assert!(!map.contains_key(&Pair("b", "a"))); |
| 40 |
} |
| 41 |
|
| 42 |
#[test] |
| 43 |
fn test_string_str() { |
| 44 |
let s = String::from; |
| 45 |
let mut map = indexmap! { |
| 46 |
s("a") => 1, s("b") => 2, |
| 47 |
s("x") => 3, s("y") => 4, |
| 48 |
}; |
| 49 |
|
| 50 |
assert!(map.contains_key("a")); |
| 51 |
assert!(!map.contains_key("z")); |
| 52 |
assert_eq!(map.swap_remove("b"), Some(2)); |
| 53 |
} |