Skip to content

Commit fe81cad

Browse files
committed
Auto merge of #468 - JustForFun88:make_alloc_not_clone, r=Amanieu
Make allocator not `Clone` `@Amanieu` Implements the idea proposed in #465 (comment). The `A: Allocator + Clone` requirement remains only for `Clone` implementations. ~~**The pull request is basically complete, just a final review (and maybe some tweaks if needed). Also, I haven't documented the functions yet. This can be done after the initial approval of this pull request.**~~ Main changes made: 1. Allacator moved from `RawTableInner` to `RawTable`; 2. `IS_ZERO_SIZED_TYPE` and `DATA_NEEDS_DROP` constants moved from separate structures to `SizedTypeProperties` trait; 3. For ease of implementation and avoidance of duplication, some functions are downgraded from `RawTable` to `RawTableInner`. These are: `with_capacity`, `iter`, `drop_elements`. Additionally, the `drop_inner_table` function has been created through which `impl Drop for RawTable<T, A>` is now implemented.
2 parents a33acbe + 91d3675 commit fe81cad

File tree

8 files changed

+776
-448
lines changed

8 files changed

+776
-448
lines changed

src/external_trait_impls/rayon/map.rs

Lines changed: 15 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -232,11 +232,11 @@ impl<K: Eq + Hash, V: fmt::Debug> fmt::Debug for ParValuesMut<'_, K, V> {
232232
/// [`into_par_iter`]: /hashbrown/struct.HashMap.html#method.into_par_iter
233233
/// [`HashMap`]: /hashbrown/struct.HashMap.html
234234
/// [`IntoParallelIterator`]: https://docs.rs/rayon/1.0/rayon/iter/trait.IntoParallelIterator.html
235-
pub struct IntoParIter<K, V, A: Allocator + Clone = Global> {
235+
pub struct IntoParIter<K, V, A: Allocator = Global> {
236236
inner: RawIntoParIter<(K, V), A>,
237237
}
238238

239-
impl<K: Send, V: Send, A: Allocator + Clone + Send> ParallelIterator for IntoParIter<K, V, A> {
239+
impl<K: Send, V: Send, A: Allocator + Send> ParallelIterator for IntoParIter<K, V, A> {
240240
type Item = (K, V);
241241

242242
#[cfg_attr(feature = "inline-more", inline)]
@@ -248,9 +248,7 @@ impl<K: Send, V: Send, A: Allocator + Clone + Send> ParallelIterator for IntoPar
248248
}
249249
}
250250

251-
impl<K: fmt::Debug + Eq + Hash, V: fmt::Debug, A: Allocator + Clone> fmt::Debug
252-
for IntoParIter<K, V, A>
253-
{
251+
impl<K: fmt::Debug + Eq + Hash, V: fmt::Debug, A: Allocator> fmt::Debug for IntoParIter<K, V, A> {
254252
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
255253
ParIter {
256254
inner: unsafe { self.inner.par_iter() },
@@ -267,11 +265,11 @@ impl<K: fmt::Debug + Eq + Hash, V: fmt::Debug, A: Allocator + Clone> fmt::Debug
267265
///
268266
/// [`par_drain`]: /hashbrown/struct.HashMap.html#method.par_drain
269267
/// [`HashMap`]: /hashbrown/struct.HashMap.html
270-
pub struct ParDrain<'a, K, V, A: Allocator + Clone = Global> {
268+
pub struct ParDrain<'a, K, V, A: Allocator = Global> {
271269
inner: RawParDrain<'a, (K, V), A>,
272270
}
273271

274-
impl<K: Send, V: Send, A: Allocator + Clone + Sync> ParallelIterator for ParDrain<'_, K, V, A> {
272+
impl<K: Send, V: Send, A: Allocator + Sync> ParallelIterator for ParDrain<'_, K, V, A> {
275273
type Item = (K, V);
276274

277275
#[cfg_attr(feature = "inline-more", inline)]
@@ -283,9 +281,7 @@ impl<K: Send, V: Send, A: Allocator + Clone + Sync> ParallelIterator for ParDrai
283281
}
284282
}
285283

286-
impl<K: fmt::Debug + Eq + Hash, V: fmt::Debug, A: Allocator + Clone> fmt::Debug
287-
for ParDrain<'_, K, V, A>
288-
{
284+
impl<K: fmt::Debug + Eq + Hash, V: fmt::Debug, A: Allocator> fmt::Debug for ParDrain<'_, K, V, A> {
289285
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
290286
ParIter {
291287
inner: unsafe { self.inner.par_iter() },
@@ -295,7 +291,7 @@ impl<K: fmt::Debug + Eq + Hash, V: fmt::Debug, A: Allocator + Clone> fmt::Debug
295291
}
296292
}
297293

298-
impl<K: Sync, V: Sync, S, A: Allocator + Clone> HashMap<K, V, S, A> {
294+
impl<K: Sync, V: Sync, S, A: Allocator> HashMap<K, V, S, A> {
299295
/// Visits (potentially in parallel) immutably borrowed keys in an arbitrary order.
300296
#[cfg_attr(feature = "inline-more", inline)]
301297
pub fn par_keys(&self) -> ParKeys<'_, K, V> {
@@ -315,7 +311,7 @@ impl<K: Sync, V: Sync, S, A: Allocator + Clone> HashMap<K, V, S, A> {
315311
}
316312
}
317313

318-
impl<K: Send, V: Send, S, A: Allocator + Clone> HashMap<K, V, S, A> {
314+
impl<K: Send, V: Send, S, A: Allocator> HashMap<K, V, S, A> {
319315
/// Visits (potentially in parallel) mutably borrowed values in an arbitrary order.
320316
#[cfg_attr(feature = "inline-more", inline)]
321317
pub fn par_values_mut(&mut self) -> ParValuesMut<'_, K, V> {
@@ -340,7 +336,7 @@ where
340336
K: Eq + Hash + Sync,
341337
V: PartialEq + Sync,
342338
S: BuildHasher + Sync,
343-
A: Allocator + Clone + Sync,
339+
A: Allocator + Sync,
344340
{
345341
/// Returns `true` if the map is equal to another,
346342
/// i.e. both maps contain the same keys mapped to the same values.
@@ -354,9 +350,7 @@ where
354350
}
355351
}
356352

357-
impl<K: Send, V: Send, S, A: Allocator + Clone + Send> IntoParallelIterator
358-
for HashMap<K, V, S, A>
359-
{
353+
impl<K: Send, V: Send, S, A: Allocator + Send> IntoParallelIterator for HashMap<K, V, S, A> {
360354
type Item = (K, V);
361355
type Iter = IntoParIter<K, V, A>;
362356

@@ -368,9 +362,7 @@ impl<K: Send, V: Send, S, A: Allocator + Clone + Send> IntoParallelIterator
368362
}
369363
}
370364

371-
impl<'a, K: Sync, V: Sync, S, A: Allocator + Clone> IntoParallelIterator
372-
for &'a HashMap<K, V, S, A>
373-
{
365+
impl<'a, K: Sync, V: Sync, S, A: Allocator> IntoParallelIterator for &'a HashMap<K, V, S, A> {
374366
type Item = (&'a K, &'a V);
375367
type Iter = ParIter<'a, K, V>;
376368

@@ -383,9 +375,7 @@ impl<'a, K: Sync, V: Sync, S, A: Allocator + Clone> IntoParallelIterator
383375
}
384376
}
385377

386-
impl<'a, K: Sync, V: Send, S, A: Allocator + Clone> IntoParallelIterator
387-
for &'a mut HashMap<K, V, S, A>
388-
{
378+
impl<'a, K: Sync, V: Send, S, A: Allocator> IntoParallelIterator for &'a mut HashMap<K, V, S, A> {
389379
type Item = (&'a K, &'a mut V);
390380
type Iter = ParIterMut<'a, K, V>;
391381

@@ -424,7 +414,7 @@ where
424414
K: Eq + Hash + Send,
425415
V: Send,
426416
S: BuildHasher,
427-
A: Allocator + Clone,
417+
A: Allocator,
428418
{
429419
fn par_extend<I>(&mut self, par_iter: I)
430420
where
@@ -440,7 +430,7 @@ where
440430
K: Copy + Eq + Hash + Sync,
441431
V: Copy + Sync,
442432
S: BuildHasher,
443-
A: Allocator + Clone,
433+
A: Allocator,
444434
{
445435
fn par_extend<I>(&mut self, par_iter: I)
446436
where
@@ -456,7 +446,7 @@ where
456446
K: Eq + Hash,
457447
S: BuildHasher,
458448
I: IntoParallelIterator,
459-
A: Allocator + Clone,
449+
A: Allocator,
460450
HashMap<K, V, S, A>: Extend<I::Item>,
461451
{
462452
let (list, len) = super::helpers::collect(par_iter);

src/external_trait_impls/rayon/raw.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -75,18 +75,18 @@ impl<T> UnindexedProducer for ParIterProducer<T> {
7575
}
7676

7777
/// Parallel iterator which consumes a table and returns elements.
78-
pub struct RawIntoParIter<T, A: Allocator + Clone = Global> {
78+
pub struct RawIntoParIter<T, A: Allocator = Global> {
7979
table: RawTable<T, A>,
8080
}
8181

82-
impl<T, A: Allocator + Clone> RawIntoParIter<T, A> {
82+
impl<T, A: Allocator> RawIntoParIter<T, A> {
8383
#[cfg_attr(feature = "inline-more", inline)]
8484
pub(super) unsafe fn par_iter(&self) -> RawParIter<T> {
8585
self.table.par_iter()
8686
}
8787
}
8888

89-
impl<T: Send, A: Allocator + Clone + Send> ParallelIterator for RawIntoParIter<T, A> {
89+
impl<T: Send, A: Allocator + Send> ParallelIterator for RawIntoParIter<T, A> {
9090
type Item = T;
9191

9292
#[cfg_attr(feature = "inline-more", inline)]
@@ -108,23 +108,23 @@ impl<T: Send, A: Allocator + Clone + Send> ParallelIterator for RawIntoParIter<T
108108
}
109109

110110
/// Parallel iterator which consumes elements without freeing the table storage.
111-
pub struct RawParDrain<'a, T, A: Allocator + Clone = Global> {
111+
pub struct RawParDrain<'a, T, A: Allocator = Global> {
112112
// We don't use a &'a mut RawTable<T> because we want RawParDrain to be
113113
// covariant over T.
114114
table: NonNull<RawTable<T, A>>,
115115
marker: PhantomData<&'a RawTable<T, A>>,
116116
}
117117

118-
unsafe impl<T: Send, A: Allocator + Clone> Send for RawParDrain<'_, T, A> {}
118+
unsafe impl<T: Send, A: Allocator> Send for RawParDrain<'_, T, A> {}
119119

120-
impl<T, A: Allocator + Clone> RawParDrain<'_, T, A> {
120+
impl<T, A: Allocator> RawParDrain<'_, T, A> {
121121
#[cfg_attr(feature = "inline-more", inline)]
122122
pub(super) unsafe fn par_iter(&self) -> RawParIter<T> {
123123
self.table.as_ref().par_iter()
124124
}
125125
}
126126

127-
impl<T: Send, A: Allocator + Clone> ParallelIterator for RawParDrain<'_, T, A> {
127+
impl<T: Send, A: Allocator> ParallelIterator for RawParDrain<'_, T, A> {
128128
type Item = T;
129129

130130
#[cfg_attr(feature = "inline-more", inline)]
@@ -142,7 +142,7 @@ impl<T: Send, A: Allocator + Clone> ParallelIterator for RawParDrain<'_, T, A> {
142142
}
143143
}
144144

145-
impl<T, A: Allocator + Clone> Drop for RawParDrain<'_, T, A> {
145+
impl<T, A: Allocator> Drop for RawParDrain<'_, T, A> {
146146
fn drop(&mut self) {
147147
// If drive_unindexed is not called then simply clear the table.
148148
unsafe {
@@ -203,7 +203,7 @@ impl<T> Drop for ParDrainProducer<T> {
203203
}
204204
}
205205

206-
impl<T, A: Allocator + Clone> RawTable<T, A> {
206+
impl<T, A: Allocator> RawTable<T, A> {
207207
/// Returns a parallel iterator over the elements in a `RawTable`.
208208
#[cfg_attr(feature = "inline-more", inline)]
209209
pub unsafe fn par_iter(&self) -> RawParIter<T> {

src/external_trait_impls/rayon/set.rs

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@ use rayon::iter::{FromParallelIterator, IntoParallelIterator, ParallelExtend, Pa
1616
/// [`into_par_iter`]: /hashbrown/struct.HashSet.html#method.into_par_iter
1717
/// [`HashSet`]: /hashbrown/struct.HashSet.html
1818
/// [`IntoParallelIterator`]: https://docs.rs/rayon/1.0/rayon/iter/trait.IntoParallelIterator.html
19-
pub struct IntoParIter<T, A: Allocator + Clone = Global> {
19+
pub struct IntoParIter<T, A: Allocator = Global> {
2020
inner: map::IntoParIter<T, (), A>,
2121
}
2222

23-
impl<T: Send, A: Allocator + Clone + Send> ParallelIterator for IntoParIter<T, A> {
23+
impl<T: Send, A: Allocator + Send> ParallelIterator for IntoParIter<T, A> {
2424
type Item = T;
2525

2626
fn drive_unindexed<C>(self, consumer: C) -> C::Result
@@ -38,11 +38,11 @@ impl<T: Send, A: Allocator + Clone + Send> ParallelIterator for IntoParIter<T, A
3838
///
3939
/// [`par_drain`]: /hashbrown/struct.HashSet.html#method.par_drain
4040
/// [`HashSet`]: /hashbrown/struct.HashSet.html
41-
pub struct ParDrain<'a, T, A: Allocator + Clone = Global> {
41+
pub struct ParDrain<'a, T, A: Allocator = Global> {
4242
inner: map::ParDrain<'a, T, (), A>,
4343
}
4444

45-
impl<T: Send, A: Allocator + Clone + Send + Sync> ParallelIterator for ParDrain<'_, T, A> {
45+
impl<T: Send, A: Allocator + Send + Sync> ParallelIterator for ParDrain<'_, T, A> {
4646
type Item = T;
4747

4848
fn drive_unindexed<C>(self, consumer: C) -> C::Result
@@ -85,7 +85,7 @@ impl<'a, T: Sync> ParallelIterator for ParIter<'a, T> {
8585
///
8686
/// [`par_difference`]: /hashbrown/struct.HashSet.html#method.par_difference
8787
/// [`HashSet`]: /hashbrown/struct.HashSet.html
88-
pub struct ParDifference<'a, T, S, A: Allocator + Clone = Global> {
88+
pub struct ParDifference<'a, T, S, A: Allocator = Global> {
8989
a: &'a HashSet<T, S, A>,
9090
b: &'a HashSet<T, S, A>,
9191
}
@@ -94,7 +94,7 @@ impl<'a, T, S, A> ParallelIterator for ParDifference<'a, T, S, A>
9494
where
9595
T: Eq + Hash + Sync,
9696
S: BuildHasher + Sync,
97-
A: Allocator + Clone + Sync,
97+
A: Allocator + Sync,
9898
{
9999
type Item = &'a T;
100100

@@ -118,7 +118,7 @@ where
118118
///
119119
/// [`par_symmetric_difference`]: /hashbrown/struct.HashSet.html#method.par_symmetric_difference
120120
/// [`HashSet`]: /hashbrown/struct.HashSet.html
121-
pub struct ParSymmetricDifference<'a, T, S, A: Allocator + Clone = Global> {
121+
pub struct ParSymmetricDifference<'a, T, S, A: Allocator = Global> {
122122
a: &'a HashSet<T, S, A>,
123123
b: &'a HashSet<T, S, A>,
124124
}
@@ -127,7 +127,7 @@ impl<'a, T, S, A> ParallelIterator for ParSymmetricDifference<'a, T, S, A>
127127
where
128128
T: Eq + Hash + Sync,
129129
S: BuildHasher + Sync,
130-
A: Allocator + Clone + Sync,
130+
A: Allocator + Sync,
131131
{
132132
type Item = &'a T;
133133

@@ -150,7 +150,7 @@ where
150150
///
151151
/// [`par_intersection`]: /hashbrown/struct.HashSet.html#method.par_intersection
152152
/// [`HashSet`]: /hashbrown/struct.HashSet.html
153-
pub struct ParIntersection<'a, T, S, A: Allocator + Clone = Global> {
153+
pub struct ParIntersection<'a, T, S, A: Allocator = Global> {
154154
a: &'a HashSet<T, S, A>,
155155
b: &'a HashSet<T, S, A>,
156156
}
@@ -159,7 +159,7 @@ impl<'a, T, S, A> ParallelIterator for ParIntersection<'a, T, S, A>
159159
where
160160
T: Eq + Hash + Sync,
161161
S: BuildHasher + Sync,
162-
A: Allocator + Clone + Sync,
162+
A: Allocator + Sync,
163163
{
164164
type Item = &'a T;
165165

@@ -181,7 +181,7 @@ where
181181
///
182182
/// [`par_union`]: /hashbrown/struct.HashSet.html#method.par_union
183183
/// [`HashSet`]: /hashbrown/struct.HashSet.html
184-
pub struct ParUnion<'a, T, S, A: Allocator + Clone = Global> {
184+
pub struct ParUnion<'a, T, S, A: Allocator = Global> {
185185
a: &'a HashSet<T, S, A>,
186186
b: &'a HashSet<T, S, A>,
187187
}
@@ -190,7 +190,7 @@ impl<'a, T, S, A> ParallelIterator for ParUnion<'a, T, S, A>
190190
where
191191
T: Eq + Hash + Sync,
192192
S: BuildHasher + Sync,
193-
A: Allocator + Clone + Sync,
193+
A: Allocator + Sync,
194194
{
195195
type Item = &'a T;
196196

@@ -216,7 +216,7 @@ impl<T, S, A> HashSet<T, S, A>
216216
where
217217
T: Eq + Hash + Sync,
218218
S: BuildHasher + Sync,
219-
A: Allocator + Clone + Sync,
219+
A: Allocator + Sync,
220220
{
221221
/// Visits (potentially in parallel) the values representing the union,
222222
/// i.e. all the values in `self` or `other`, without duplicates.
@@ -289,7 +289,7 @@ where
289289
impl<T, S, A> HashSet<T, S, A>
290290
where
291291
T: Eq + Hash + Send,
292-
A: Allocator + Clone + Send,
292+
A: Allocator + Send,
293293
{
294294
/// Consumes (potentially in parallel) all values in an arbitrary order,
295295
/// while preserving the set's allocated memory for reuse.
@@ -301,7 +301,7 @@ where
301301
}
302302
}
303303

304-
impl<T: Send, S, A: Allocator + Clone + Send> IntoParallelIterator for HashSet<T, S, A> {
304+
impl<T: Send, S, A: Allocator + Send> IntoParallelIterator for HashSet<T, S, A> {
305305
type Item = T;
306306
type Iter = IntoParIter<T, A>;
307307

@@ -313,7 +313,7 @@ impl<T: Send, S, A: Allocator + Clone + Send> IntoParallelIterator for HashSet<T
313313
}
314314
}
315315

316-
impl<'a, T: Sync, S, A: Allocator + Clone> IntoParallelIterator for &'a HashSet<T, S, A> {
316+
impl<'a, T: Sync, S, A: Allocator> IntoParallelIterator for &'a HashSet<T, S, A> {
317317
type Item = &'a T;
318318
type Iter = ParIter<'a, T>;
319319

@@ -374,7 +374,7 @@ fn extend<T, S, I, A>(set: &mut HashSet<T, S, A>, par_iter: I)
374374
where
375375
T: Eq + Hash,
376376
S: BuildHasher,
377-
A: Allocator + Clone,
377+
A: Allocator,
378378
I: IntoParallelIterator,
379379
HashSet<T, S, A>: Extend<I::Item>,
380380
{

0 commit comments

Comments
 (0)