@@ -503,13 +503,17 @@ impl_stable_hash_via_hash!(::std::path::PathBuf);
503
503
504
504
impl < K , V , R , HCX > HashStable < HCX > for :: std:: collections:: HashMap < K , V , R >
505
505
where
506
- K : HashStable < HCX > + ToStableHashKey < HCX > + Eq ,
506
+ K : ToStableHashKey < HCX > + Eq ,
507
507
V : HashStable < HCX > ,
508
508
R : BuildHasher ,
509
509
{
510
510
#[ inline]
511
511
fn hash_stable ( & self , hcx : & mut HCX , hasher : & mut StableHasher ) {
512
- hash_stable_hashmap ( hcx, hasher, self , ToStableHashKey :: to_stable_hash_key) ;
512
+ stable_hash_reduce ( hcx, hasher, self . iter ( ) , self . len ( ) , |hasher, hcx, ( key, value) | {
513
+ let key = key. to_stable_hash_key ( hcx) ;
514
+ key. hash_stable ( hcx, hasher) ;
515
+ value. hash_stable ( hcx, hasher) ;
516
+ } ) ;
513
517
}
514
518
}
515
519
@@ -519,9 +523,10 @@ where
519
523
R : BuildHasher ,
520
524
{
521
525
fn hash_stable ( & self , hcx : & mut HCX , hasher : & mut StableHasher ) {
522
- let mut keys: Vec < _ > = self . iter ( ) . map ( |k| k. to_stable_hash_key ( hcx) ) . collect ( ) ;
523
- keys. sort_unstable ( ) ;
524
- keys. hash_stable ( hcx, hasher) ;
526
+ stable_hash_reduce ( hcx, hasher, self . iter ( ) , self . len ( ) , |hasher, hcx, key| {
527
+ let key = key. to_stable_hash_key ( hcx) ;
528
+ key. hash_stable ( hcx, hasher) ;
529
+ } ) ;
525
530
}
526
531
}
527
532
@@ -531,10 +536,11 @@ where
531
536
V : HashStable < HCX > ,
532
537
{
533
538
fn hash_stable ( & self , hcx : & mut HCX , hasher : & mut StableHasher ) {
534
- let mut entries: Vec < _ > =
535
- self . iter ( ) . map ( |( k, v) | ( k. to_stable_hash_key ( hcx) , v) ) . collect ( ) ;
536
- entries. sort_unstable_by ( |& ( ref sk1, _) , & ( ref sk2, _) | sk1. cmp ( sk2) ) ;
537
- entries. hash_stable ( hcx, hasher) ;
539
+ stable_hash_reduce ( hcx, hasher, self . iter ( ) , self . len ( ) , |hasher, hcx, ( key, value) | {
540
+ let key = key. to_stable_hash_key ( hcx) ;
541
+ key. hash_stable ( hcx, hasher) ;
542
+ value. hash_stable ( hcx, hasher) ;
543
+ } ) ;
538
544
}
539
545
}
540
546
@@ -543,34 +549,30 @@ where
543
549
K : ToStableHashKey < HCX > ,
544
550
{
545
551
fn hash_stable ( & self , hcx : & mut HCX , hasher : & mut StableHasher ) {
546
- let mut keys: Vec < _ > = self . iter ( ) . map ( |k| k. to_stable_hash_key ( hcx) ) . collect ( ) ;
547
- keys. sort_unstable ( ) ;
548
- keys. hash_stable ( hcx, hasher) ;
552
+ stable_hash_reduce ( hcx, hasher, self . iter ( ) , self . len ( ) , |hasher, hcx, key| {
553
+ let key = key. to_stable_hash_key ( hcx) ;
554
+ key. hash_stable ( hcx, hasher) ;
555
+ } ) ;
549
556
}
550
557
}
551
558
552
- pub fn hash_stable_hashmap < HCX , K , V , R , SK , F > (
559
+ fn stable_hash_reduce < HCX , I , C , F > (
553
560
hcx : & mut HCX ,
554
561
hasher : & mut StableHasher ,
555
- map : & :: std:: collections:: HashMap < K , V , R > ,
556
- to_stable_hash_key : F ,
562
+ collection : C ,
563
+ length : usize ,
564
+ hash_function : F ,
557
565
) where
558
- K : Eq + HashStable < HCX > ,
559
- V : HashStable < HCX > ,
560
- R : BuildHasher ,
561
- SK : HashStable < HCX > + Ord ,
562
- F : Fn ( & K , & HCX ) -> SK ,
566
+ C : Iterator < Item = I > ,
567
+ F : Fn ( & mut StableHasher , & mut HCX , I ) ,
563
568
{
564
- let hash = map
565
- . iter ( )
566
- . map ( |( key, value) | {
567
- let key = to_stable_hash_key ( key, hcx) ;
569
+ let hash = collection
570
+ . map ( |value| {
568
571
let mut hasher = StableHasher :: new ( ) ;
569
- key. hash_stable ( hcx, & mut hasher) ;
570
- value. hash_stable ( hcx, & mut hasher) ;
572
+ hash_function ( & mut hasher, hcx, value) ;
571
573
hasher. finish :: < u128 > ( )
572
574
} )
573
575
. reduce ( |accum, value| accum. wrapping_add ( value) ) ;
574
- map . len ( ) . hash_stable ( hcx, hasher) ;
576
+ length . hash_stable ( hcx, hasher) ;
575
577
hash. hash_stable ( hcx, hasher) ;
576
578
}
0 commit comments