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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
|
use crate::{DashMap, DashSet};
use core::fmt;
use core::hash::Hash;
use core::marker::PhantomData;
use serde::de::{Deserialize, MapAccess, SeqAccess, Visitor};
use serde::ser::{Serialize, SerializeMap, SerializeSeq, Serializer};
use serde::Deserializer;
pub struct DashMapVisitor<K, V> {
marker: PhantomData<fn() -> DashMap<K, V>>,
}
impl<K, V> DashMapVisitor<K, V>
where
K: Eq + Hash,
{
fn new() -> Self {
DashMapVisitor {
marker: PhantomData,
}
}
}
impl<'de, K, V> Visitor<'de> for DashMapVisitor<K, V>
where
K: Deserialize<'de> + Eq + Hash,
V: Deserialize<'de>,
{
type Value = DashMap<K, V>;
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
formatter.write_str("a DashMap")
}
fn visit_map<M>(self, mut access: M) -> Result<Self::Value, M::Error>
where
M: MapAccess<'de>,
{
let map = DashMap::with_capacity(access.size_hint().unwrap_or(0));
while let Some((key, value)) = access.next_entry()? {
map.insert(key, value);
}
Ok(map)
}
}
impl<'de, K, V> Deserialize<'de> for DashMap<K, V>
where
K: Deserialize<'de> + Eq + Hash,
V: Deserialize<'de>,
{
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
deserializer.deserialize_map(DashMapVisitor::<K, V>::new())
}
}
impl<K, V> Serialize for DashMap<K, V>
where
K: Serialize + Eq + Hash,
V: Serialize,
{
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
let mut map = serializer.serialize_map(Some(self.len()))?;
for ref_multi in self.iter() {
map.serialize_entry(ref_multi.key(), ref_multi.value())?;
}
map.end()
}
}
pub struct DashSetVisitor<K> {
marker: PhantomData<fn() -> DashSet<K>>,
}
impl<K> DashSetVisitor<K>
where
K: Eq + Hash,
{
fn new() -> Self {
DashSetVisitor {
marker: PhantomData,
}
}
}
impl<'de, K> Visitor<'de> for DashSetVisitor<K>
where
K: Deserialize<'de> + Eq + Hash,
{
type Value = DashSet<K>;
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
formatter.write_str("a DashSet")
}
fn visit_seq<M>(self, mut access: M) -> Result<Self::Value, M::Error>
where
M: SeqAccess<'de>,
{
let map = DashSet::with_capacity(access.size_hint().unwrap_or(0));
while let Some(key) = access.next_element()? {
map.insert(key);
}
Ok(map)
}
}
impl<'de, K> Deserialize<'de> for DashSet<K>
where
K: Deserialize<'de> + Eq + Hash,
{
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
deserializer.deserialize_seq(DashSetVisitor::<K>::new())
}
}
impl<K> Serialize for DashSet<K>
where
K: Serialize + Eq + Hash,
{
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
let mut seq = serializer.serialize_seq(Some(self.len()))?;
for ref_multi in self.iter() {
seq.serialize_element(ref_multi.key())?;
}
seq.end()
}
}
|