Debian Codegraph @

debian/rust-vendor/hashbrown/tests/equivalent_trait.rs

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