Skip to content

Commit b62ff85

Browse files
committed
Implement PartialEq, Eq and Debug
1 parent c7317b2 commit b62ff85

File tree

1 file changed

+33
-9
lines changed

1 file changed

+33
-9
lines changed

src/lib.rs

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,9 @@ use std::collections::HashMap;
4343
use std::collections::hash_map::Iter;
4444
use std::hash::Hash;
4545
use std::borrow::Borrow;
46+
use std::fmt::{self, Debug};
4647

48+
#[derive(Eq)]
4749
pub struct MultiMap<K1: Eq + Hash, K2: Eq + Hash, V> {
4850
value_map: HashMap<K1, (K2, V)>,
4951
key_map: HashMap<K2, K1>,
@@ -170,6 +172,18 @@ impl<K1: Eq + Hash + Clone, K2: Eq + Hash + Clone, V> MultiMap<K1, K2, V> {
170172
}
171173
}
172174

175+
impl<K1: Eq + Hash, K2: Eq + Hash, V: Eq> PartialEq for MultiMap<K1, K2, V> {
176+
fn eq(&self, other: &MultiMap<K1, K2, V>) -> bool {
177+
self.value_map.eq(&other.value_map)
178+
}
179+
}
180+
181+
impl<K1: Eq + Hash + Debug, K2: Eq + Hash + Debug, V: Debug> fmt::Debug for MultiMap<K1, K2, V> {
182+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
183+
f.debug_map().entries(self.value_map.iter().map(|(key_one, &(ref key_two, ref value))| ((key_one, key_two), value))).finish()
184+
}
185+
}
186+
173187
#[macro_export]
174188
/// Create a `MultiMap` from a list of key-value tuples
175189
///
@@ -258,26 +272,36 @@ mod test {
258272
fn macro_test() {
259273
use ::MultiMap;
260274

261-
let _: MultiMap<i32, &str, String> = multimap!{};
275+
let map: MultiMap<i32, &str, String> = MultiMap::new();
276+
277+
assert_eq!(map, multimap!{});
262278

263-
multimap!{
279+
let mut map = MultiMap::new();
280+
map.insert(1, "One", String::from("Eins"));
281+
282+
assert_eq!(map, multimap!{
264283
1, "One" => String::from("Eins"),
265-
};
284+
});
266285

267-
multimap!{
286+
assert_eq!(map, multimap!{
268287
1, "One" => String::from("Eins")
269-
};
288+
});
289+
290+
let mut map = MultiMap::new();
291+
map.insert(1, "One", String::from("Eins"));
292+
map.insert(2, "Two", String::from("Zwei"));
293+
map.insert(3, "Three", String::from("Drei"));
270294

271-
multimap!{
295+
assert_eq!(map, multimap!{
272296
1, "One" => String::from("Eins"),
273297
2, "Two" => String::from("Zwei"),
274298
3, "Three" => String::from("Drei"),
275-
};
299+
});
276300

277-
multimap!{
301+
assert_eq!(map, multimap!{
278302
1, "One" => String::from("Eins"),
279303
2, "Two" => String::from("Zwei"),
280304
3, "Three" => String::from("Drei")
281-
};
305+
});
282306
}
283307
}

0 commit comments

Comments
 (0)