@@ -216,6 +216,7 @@ pub use crate::read::{MapReadRef, ReadGuardIter, ReadHandle, ReadHandleFactory};
216
216
217
217
pub mod shallow_copy;
218
218
pub use crate :: shallow_copy:: ShallowCopy ;
219
+ use shallow_copy:: MaybeShallowCopied ;
219
220
220
221
// Expose `ReadGuard` since it has useful methods the user will likely care about.
221
222
#[ doc( inline) ]
@@ -225,27 +226,27 @@ pub use left_right::ReadGuard;
225
226
///
226
227
/// The predicate function is called once for each distinct value, and `true` if this is the
227
228
/// _first_ call to the predicate on the _second_ application of the operation.
228
- pub struct Predicate < V > ( pub ( crate ) Box < dyn FnMut ( & V , bool ) -> bool + Send > ) ;
229
+ pub struct Predicate < V : ? Sized > ( pub ( crate ) Box < dyn FnMut ( & V , bool ) -> bool + Send > ) ;
229
230
230
- impl < V > Predicate < V > {
231
+ impl < V : ? Sized > Predicate < V > {
231
232
/// Evaluate the predicate for the given element
232
233
#[ inline]
233
234
pub fn eval ( & mut self , value : & V , reset : bool ) -> bool {
234
235
( * self . 0 ) ( value, reset)
235
236
}
236
237
}
237
238
238
- impl < V > PartialEq for Predicate < V > {
239
+ impl < V : ? Sized > PartialEq for Predicate < V > {
239
240
#[ inline]
240
241
fn eq ( & self , other : & Self ) -> bool {
241
242
// only compare data, not vtable: https://stackoverflow.com/q/47489449/472927
242
243
& * self . 0 as * const _ as * const ( ) == & * other. 0 as * const _ as * const ( )
243
244
}
244
245
}
245
246
246
- impl < V > Eq for Predicate < V > { }
247
+ impl < V : ? Sized > Eq for Predicate < V > { }
247
248
248
- impl < V > fmt:: Debug for Predicate < V > {
249
+ impl < V : ? Sized > fmt:: Debug for Predicate < V > {
249
250
fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
250
251
f. debug_tuple ( "Predicate" )
251
252
. field ( & format_args ! ( "{:p}" , & * self . 0 as * const _) )
@@ -255,12 +256,14 @@ impl<V> fmt::Debug for Predicate<V> {
255
256
256
257
/// A pending map operation.
257
258
#[ non_exhaustive]
258
- #[ derive( PartialEq , Eq , Debug ) ]
259
- pub ( crate ) enum Operation < K , V , M > {
259
+ pub ( crate ) enum Operation < K , V , M >
260
+ where
261
+ V : ShallowCopy ,
262
+ {
260
263
/// Replace the set of entries for this key with this value.
261
- Replace ( K , V ) ,
264
+ Replace ( K , MaybeShallowCopied < V > ) ,
262
265
/// Add this value to the set of entries for this key.
263
- Add ( K , V ) ,
266
+ Add ( K , MaybeShallowCopied < V > ) ,
264
267
/// Remove this value from the set of entries for this key.
265
268
RemoveValue ( K , V ) ,
266
269
/// Remove the value set for this key.
@@ -277,24 +280,48 @@ pub(crate) enum Operation<K, V, M> {
277
280
/// Note that this will iterate once over all the keys internally.
278
281
Purge ,
279
282
/// Retains all values matching the given predicate.
280
- Retain ( K , Predicate < V > ) ,
281
- /// Shrinks [`Values `] to their minimum necessary size, freeing memory
283
+ Retain ( K , Predicate < V :: Target > ) ,
284
+ /// Shrinks [`MaybeShallowCopied<V>alues `] to their minimum necessary size, freeing memory
282
285
/// and potentially improving cache locality.
283
286
///
284
- /// If no key is given, all `Values ` will shrink to fit.
287
+ /// If no key is given, all `MaybeShallowCopied<V>alues ` will shrink to fit.
285
288
Fit ( Option < K > ) ,
286
- /// Reserves capacity for some number of additional elements in [`Values `]
289
+ /// Reserves capacity for some number of additional elements in [`MaybeShallowCopied<V>alues `]
287
290
/// for the given key. If the given key does not exist, allocate an empty
288
- /// `Values ` with the given capacity.
291
+ /// `MaybeShallowCopied<V>alues ` with the given capacity.
289
292
///
290
293
/// This can improve performance by pre-allocating space for large bags of values.
291
294
Reserve ( K , usize ) ,
292
295
/// Mark the map as ready to be consumed for readers.
293
296
MarkReady ,
294
297
/// Set the value of the map meta.
295
298
SetMeta ( M ) ,
296
- /// Copy over the contents of the read map wholesale as the write map is empty.
297
- JustCloneRHandle ,
299
+ }
300
+
301
+ impl < K , V , M > fmt:: Debug for Operation < K , V , M >
302
+ where
303
+ K : fmt:: Debug ,
304
+ V : ShallowCopy + fmt:: Debug ,
305
+ V :: Target : fmt:: Debug ,
306
+ M : fmt:: Debug ,
307
+ {
308
+ fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
309
+ match * self {
310
+ Operation :: Replace ( ref a, ref b) => f. debug_tuple ( "Replace" ) . field ( a) . field ( b) . finish ( ) ,
311
+ Operation :: Add ( ref a, ref b) => f. debug_tuple ( "Add" ) . field ( a) . field ( b) . finish ( ) ,
312
+ Operation :: RemoveValue ( ref a, ref b) => {
313
+ f. debug_tuple ( "RemoveValue" ) . field ( a) . field ( b) . finish ( )
314
+ }
315
+ Operation :: RemoveEntry ( ref a) => f. debug_tuple ( "RemoveEntry" ) . field ( a) . finish ( ) ,
316
+ Operation :: Clear ( ref a) => f. debug_tuple ( "Clear" ) . field ( a) . finish ( ) ,
317
+ Operation :: Purge => f. debug_tuple ( "Purge" ) . finish ( ) ,
318
+ Operation :: Retain ( ref a, ref b) => f. debug_tuple ( "Retain" ) . field ( a) . field ( b) . finish ( ) ,
319
+ Operation :: Fit ( ref a) => f. debug_tuple ( "Fit" ) . field ( a) . finish ( ) ,
320
+ Operation :: Reserve ( ref a, ref b) => f. debug_tuple ( "Reserve" ) . field ( a) . field ( b) . finish ( ) ,
321
+ Operation :: MarkReady => f. debug_tuple ( "MarkReady" ) . finish ( ) ,
322
+ Operation :: SetMeta ( ref a) => f. debug_tuple ( "SetMeta" ) . field ( a) . finish ( ) ,
323
+ }
324
+ }
298
325
}
299
326
300
327
/// Options for how to initialize the map.
@@ -373,7 +400,8 @@ where
373
400
where
374
401
K : Eq + Hash + Clone ,
375
402
S : BuildHasher + Clone ,
376
- V : Eq + Hash + ShallowCopy ,
403
+ V : ShallowCopy ,
404
+ V :: Target : Eq + Hash ,
377
405
M : ' static + Clone ,
378
406
{
379
407
let inner = if let Some ( cap) = self . capacity {
@@ -399,7 +427,8 @@ pub fn new<K, V>() -> (
399
427
)
400
428
where
401
429
K : Eq + Hash + Clone ,
402
- V : Eq + Hash + ShallowCopy ,
430
+ V : ShallowCopy ,
431
+ V :: Target : Eq + Hash ,
403
432
{
404
433
Options :: default ( ) . construct ( )
405
434
}
@@ -416,7 +445,8 @@ pub fn with_meta<K, V, M>(
416
445
)
417
446
where
418
447
K : Eq + Hash + Clone ,
419
- V : Eq + Hash + ShallowCopy ,
448
+ V : ShallowCopy ,
449
+ V :: Target : Eq + Hash ,
420
450
M : ' static + Clone ,
421
451
{
422
452
Options :: default ( ) . with_meta ( meta) . construct ( )
@@ -432,7 +462,8 @@ pub fn with_hasher<K, V, M, S>(
432
462
) -> ( WriteHandle < K , V , M , S > , ReadHandle < K , V , M , S > )
433
463
where
434
464
K : Eq + Hash + Clone ,
435
- V : Eq + Hash + ShallowCopy ,
465
+ V : ShallowCopy ,
466
+ V :: Target : Eq + Hash ,
436
467
M : ' static + Clone ,
437
468
S : BuildHasher + Clone ,
438
469
{
0 commit comments