1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
use serde::{Deserialize, Serialize};
use std::{
borrow::Borrow, cmp::Ordering, collections::{hash_map, HashMap}, fmt::{self, Debug}, hash::Hash
};
#[derive(Clone, Eq, Serialize, Deserialize)]
pub struct Map<K: Hash + Eq, V>(pub(crate) HashMap<K, V>);
impl<K, V> Map<K, V>
where
K: Hash + Eq,
{
pub fn get<Q: ?Sized>(&self, k: &Q) -> Option<&V>
where
K: Borrow<Q>,
Q: Hash + Eq,
{
self.0.get(k)
}
pub fn iter(&self) -> hash_map::Iter<'_, K, V> {
self.0.iter()
}
}
impl<K, V> IntoIterator for Map<K, V>
where
K: Hash + Eq,
{
type Item = (K, V);
type IntoIter = hash_map::IntoIter<K, V>;
fn into_iter(self) -> Self::IntoIter {
self.0.into_iter()
}
}
impl<K, V> From<HashMap<K, V>> for Map<K, V>
where
K: Hash + Eq,
{
fn from(hashmap: HashMap<K, V>) -> Self {
Self(hashmap)
}
}
impl<K, V> Into<HashMap<K, V>> for Map<K, V>
where
K: Hash + Eq,
{
fn into(self) -> HashMap<K, V> {
self.0
}
}
impl<K, V, V1> PartialEq<Map<K, V1>> for Map<K, V>
where
K: Eq + Hash,
V: PartialEq<V1>,
{
fn eq(&self, other: &Map<K, V1>) -> bool {
if self.0.len() != other.0.len() {
return false;
}
self.0
.iter()
.all(|(key, value)| other.0.get(key).map_or(false, |v| *value == *v))
}
}
impl<K, V, V1> PartialOrd<Map<K, V1>> for Map<K, V>
where
K: Eq + Hash,
V: PartialOrd<V1>,
{
fn partial_cmp(&self, other: &Map<K, V1>) -> Option<Ordering> {
if self.0.len() != other.0.len() {
return None;
}
None
}
}
impl<K, V> Debug for Map<K, V>
where
K: Hash + Eq + Debug,
V: Debug,
{
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.debug_map().entries(self.iter()).finish()
}
}