@@ -13,8 +13,10 @@ use hir::def_id::DefId;
1313use ich:: { self , CachingCodemapView } ;
1414use session:: config:: DebugInfoLevel :: NoDebugInfo ;
1515use ty;
16+ use util:: nodemap:: NodeMap ;
1617
1718use std:: hash as std_hash;
19+ use std:: collections:: { HashMap , HashSet } ;
1820
1921use syntax:: ast;
2022use syntax:: attr;
@@ -296,3 +298,53 @@ impl<'a, 'tcx> HashStable<StableHashingContext<'a, 'tcx>> for Span {
296298 }
297299 }
298300}
301+
302+ pub fn hash_stable_hashmap < ' a , ' tcx , K , V , R , SK , F , W > ( hcx : & mut StableHashingContext < ' a , ' tcx > ,
303+ hasher : & mut StableHasher < W > ,
304+ map : & HashMap < K , V , R > ,
305+ extract_stable_key : F )
306+ where K : Eq + std_hash:: Hash ,
307+ V : HashStable < StableHashingContext < ' a , ' tcx > > ,
308+ R : std_hash:: BuildHasher ,
309+ SK : HashStable < StableHashingContext < ' a , ' tcx > > + Ord + Clone ,
310+ F : Fn ( & mut StableHashingContext < ' a , ' tcx > , & K ) -> SK ,
311+ W : StableHasherResult ,
312+ {
313+ let mut keys: Vec < _ > = map. keys ( )
314+ . map ( |k| ( extract_stable_key ( hcx, k) , k) )
315+ . collect ( ) ;
316+ keys. sort_unstable_by_key ( |& ( ref stable_key, _) | stable_key. clone ( ) ) ;
317+ keys. len ( ) . hash_stable ( hcx, hasher) ;
318+ for ( stable_key, key) in keys {
319+ stable_key. hash_stable ( hcx, hasher) ;
320+ map[ key] . hash_stable ( hcx, hasher) ;
321+ }
322+ }
323+
324+ pub fn hash_stable_hashset < ' a , ' tcx , K , R , SK , F , W > ( hcx : & mut StableHashingContext < ' a , ' tcx > ,
325+ hasher : & mut StableHasher < W > ,
326+ set : & HashSet < K , R > ,
327+ extract_stable_key : F )
328+ where K : Eq + std_hash:: Hash ,
329+ R : std_hash:: BuildHasher ,
330+ SK : HashStable < StableHashingContext < ' a , ' tcx > > + Ord + Clone ,
331+ F : Fn ( & mut StableHashingContext < ' a , ' tcx > , & K ) -> SK ,
332+ W : StableHasherResult ,
333+ {
334+ let mut keys: Vec < _ > = set. iter ( )
335+ . map ( |k| extract_stable_key ( hcx, k) )
336+ . collect ( ) ;
337+ keys. sort_unstable ( ) ;
338+ keys. hash_stable ( hcx, hasher) ;
339+ }
340+
341+ pub fn hash_stable_nodemap < ' a , ' tcx , V , W > ( hcx : & mut StableHashingContext < ' a , ' tcx > ,
342+ hasher : & mut StableHasher < W > ,
343+ map : & NodeMap < V > )
344+ where V : HashStable < StableHashingContext < ' a , ' tcx > > ,
345+ W : StableHasherResult ,
346+ {
347+ hash_stable_hashmap ( hcx, hasher, map, |hcx, node_id| {
348+ hcx. tcx . hir . definitions ( ) . node_to_hir_id ( * node_id) . local_id
349+ } ) ;
350+ }
0 commit comments