Skip to content

Commit 065b6ee

Browse files
author
Tommaso Checchi
committed
Added Allocator support to HashMap Deserialize, and HashSet Serialize and Deserialize
1 parent 83597d3 commit 065b6ee

File tree

1 file changed

+40
-22
lines changed

1 file changed

+40
-22
lines changed

src/external_trait_impls/serde.rs

Lines changed: 40 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ mod size_hint {
1111
}
1212

1313
mod map {
14+
use crate::raw::Allocator;
1415
use core::fmt;
1516
use core::hash::{BuildHasher, Hash};
1617
use core::marker::PhantomData;
@@ -26,7 +27,7 @@ mod map {
2627
K: Serialize + Eq + Hash,
2728
V: Serialize,
2829
H: BuildHasher,
29-
A: crate::raw::Allocator + Clone,
30+
A: Allocator + Clone,
3031
{
3132
#[cfg_attr(feature = "inline-more", inline)]
3233
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
@@ -37,40 +38,46 @@ mod map {
3738
}
3839
}
3940

40-
impl<'de, K, V, S> Deserialize<'de> for HashMap<K, V, S>
41+
impl<'de, K, V, S, A> Deserialize<'de> for HashMap<K, V, S, A>
4142
where
4243
K: Deserialize<'de> + Eq + Hash,
4344
V: Deserialize<'de>,
4445
S: BuildHasher + Default,
46+
A: Allocator + Clone + Default,
4547
{
4648
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
4749
where
4850
D: Deserializer<'de>,
4951
{
50-
struct MapVisitor<K, V, S> {
51-
marker: PhantomData<HashMap<K, V, S>>,
52+
struct MapVisitor<K, V, S, A>
53+
where
54+
A: Allocator + Clone,
55+
{
56+
marker: PhantomData<HashMap<K, V, S, A>>,
5257
}
5358

54-
impl<'de, K, V, S> Visitor<'de> for MapVisitor<K, V, S>
59+
impl<'de, K, V, S, A> Visitor<'de> for MapVisitor<K, V, S, A>
5560
where
5661
K: Deserialize<'de> + Eq + Hash,
5762
V: Deserialize<'de>,
5863
S: BuildHasher + Default,
64+
A: Allocator + Clone + Default,
5965
{
60-
type Value = HashMap<K, V, S>;
66+
type Value = HashMap<K, V, S, A>;
6167

6268
fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
6369
formatter.write_str("a map")
6470
}
6571

6672
#[cfg_attr(feature = "inline-more", inline)]
67-
fn visit_map<A>(self, mut map: A) -> Result<Self::Value, A::Error>
73+
fn visit_map<M>(self, mut map: M) -> Result<Self::Value, M::Error>
6874
where
69-
A: MapAccess<'de>,
75+
M: MapAccess<'de>,
7076
{
71-
let mut values = HashMap::with_capacity_and_hasher(
77+
let mut values = HashMap::with_capacity_and_hasher_in(
7278
size_hint::cautious(map.size_hint()),
7379
S::default(),
80+
A::default(),
7481
);
7582

7683
while let Some((key, value)) = map.next_entry()? {
@@ -90,6 +97,7 @@ mod map {
9097
}
9198

9299
mod set {
100+
use crate::raw::Allocator;
93101
use core::fmt;
94102
use core::hash::{BuildHasher, Hash};
95103
use core::marker::PhantomData;
@@ -100,10 +108,11 @@ mod set {
100108

101109
use super::size_hint;
102110

103-
impl<T, H> Serialize for HashSet<T, H>
111+
impl<T, H, A> Serialize for HashSet<T, H, A>
104112
where
105113
T: Serialize + Eq + Hash,
106114
H: BuildHasher,
115+
A: Allocator + Clone,
107116
{
108117
#[cfg_attr(feature = "inline-more", inline)]
109118
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
@@ -114,38 +123,44 @@ mod set {
114123
}
115124
}
116125

117-
impl<'de, T, S> Deserialize<'de> for HashSet<T, S>
126+
impl<'de, T, S, A> Deserialize<'de> for HashSet<T, S, A>
118127
where
119128
T: Deserialize<'de> + Eq + Hash,
120129
S: BuildHasher + Default,
130+
A: Allocator + Clone + Default,
121131
{
122132
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
123133
where
124134
D: Deserializer<'de>,
125135
{
126-
struct SeqVisitor<T, S> {
127-
marker: PhantomData<HashSet<T, S>>,
136+
struct SeqVisitor<T, S, A>
137+
where
138+
A: Allocator + Clone,
139+
{
140+
marker: PhantomData<HashSet<T, S, A>>,
128141
}
129142

130-
impl<'de, T, S> Visitor<'de> for SeqVisitor<T, S>
143+
impl<'de, T, S, A> Visitor<'de> for SeqVisitor<T, S, A>
131144
where
132145
T: Deserialize<'de> + Eq + Hash,
133146
S: BuildHasher + Default,
147+
A: Allocator + Clone + Default,
134148
{
135-
type Value = HashSet<T, S>;
149+
type Value = HashSet<T, S, A>;
136150

137151
fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
138152
formatter.write_str("a sequence")
139153
}
140154

141155
#[cfg_attr(feature = "inline-more", inline)]
142-
fn visit_seq<A>(self, mut seq: A) -> Result<Self::Value, A::Error>
156+
fn visit_seq<M>(self, mut seq: M) -> Result<Self::Value, M::Error>
143157
where
144-
A: SeqAccess<'de>,
158+
M: SeqAccess<'de>,
145159
{
146-
let mut values = HashSet::with_capacity_and_hasher(
160+
let mut values = HashSet::with_capacity_and_hasher_in(
147161
size_hint::cautious(seq.size_hint()),
148162
S::default(),
163+
A::default(),
149164
);
150165

151166
while let Some(value) = seq.next_element()? {
@@ -167,12 +182,15 @@ mod set {
167182
where
168183
D: Deserializer<'de>,
169184
{
170-
struct SeqInPlaceVisitor<'a, T, S>(&'a mut HashSet<T, S>);
185+
struct SeqInPlaceVisitor<'a, T, S, A>(&'a mut HashSet<T, S, A>)
186+
where
187+
A: Allocator + Clone;
171188

172-
impl<'a, 'de, T, S> Visitor<'de> for SeqInPlaceVisitor<'a, T, S>
189+
impl<'a, 'de, T, S, A> Visitor<'de> for SeqInPlaceVisitor<'a, T, S, A>
173190
where
174191
T: Deserialize<'de> + Eq + Hash,
175192
S: BuildHasher + Default,
193+
A: Allocator + Clone,
176194
{
177195
type Value = ();
178196

@@ -181,9 +199,9 @@ mod set {
181199
}
182200

183201
#[cfg_attr(feature = "inline-more", inline)]
184-
fn visit_seq<A>(self, mut seq: A) -> Result<Self::Value, A::Error>
202+
fn visit_seq<M>(self, mut seq: M) -> Result<Self::Value, M::Error>
185203
where
186-
A: SeqAccess<'de>,
204+
M: SeqAccess<'de>,
187205
{
188206
self.0.clear();
189207
self.0.reserve(size_hint::cautious(seq.size_hint()));

0 commit comments

Comments
 (0)