@@ -208,21 +208,24 @@ impl<V: Eq + Hash> UnordSet<V> {
208208 UnordItems ( self . inner . into_iter ( ) )
209209 }
210210
211+ /// Returns the items of this set in stable sort order (as defined by `ToStableHashKey`).
212+ ///
213+ /// The `cache_sort_key` parameter controls if [slice::sort_by_cached_key] or
214+ /// [slice::sort_unstable_by_key] will be used for sorting the vec. Use
215+ /// `cache_sort_key` when the [ToStableHashKey::to_stable_hash_key] implementation
216+ /// for `V` is expensive (e.g. a `DefId -> DefPathHash` lookup).
211217 #[ inline]
212218 pub fn to_sorted < HCX > ( & self , hcx : & HCX , cache_sort_key : bool ) -> Vec < & V >
213219 where
214220 V : ToStableHashKey < HCX > ,
215221 {
216- let mut items: Vec < & V > = self . inner . iter ( ) . collect ( ) ;
217- if cache_sort_key {
218- items. sort_by_cached_key ( |k| k. to_stable_hash_key ( hcx) ) ;
219- } else {
220- items. sort_unstable_by_key ( |k| k. to_stable_hash_key ( hcx) ) ;
221- }
222-
223- items
222+ to_sorted_vec ( hcx, self . inner . iter ( ) , cache_sort_key, |& x| x)
224223 }
225224
225+ /// Returns the items of this set in stable sort order (as defined by
226+ /// `StableOrd`). This method is much more efficient than
227+ /// `into_sorted` because it does not need to transform keys to their
228+ /// `ToStableHashKey` equivalent.
226229 #[ inline]
227230 pub fn to_sorted_stable_ord ( & self ) -> Vec < V >
228231 where
@@ -233,19 +236,18 @@ impl<V: Eq + Hash> UnordSet<V> {
233236 items
234237 }
235238
239+ /// Returns the items of this set in stable sort order (as defined by `ToStableHashKey`).
240+ ///
241+ /// The `cache_sort_key` parameter controls if [slice::sort_by_cached_key] or
242+ /// [slice::sort_unstable_by_key] will be used for sorting the vec. Use
243+ /// `cache_sort_key` when the [ToStableHashKey::to_stable_hash_key] implementation
244+ /// for `V` is expensive (e.g. a `DefId -> DefPathHash` lookup).
236245 #[ inline]
237246 pub fn into_sorted < HCX > ( self , hcx : & HCX , cache_sort_key : bool ) -> Vec < V >
238247 where
239248 V : ToStableHashKey < HCX > ,
240249 {
241- let mut items: Vec < V > = self . inner . into_iter ( ) . collect ( ) ;
242- if cache_sort_key {
243- items. sort_by_cached_key ( |k| k. to_stable_hash_key ( hcx) ) ;
244- } else {
245- items. sort_unstable_by_key ( |k| k. to_stable_hash_key ( hcx) ) ;
246- }
247-
248- items
250+ to_sorted_vec ( hcx, self . inner . into_iter ( ) , cache_sort_key, |x| x)
249251 }
250252
251253 // We can safely extend this UnordSet from a set of unordered values because that
@@ -398,21 +400,23 @@ impl<K: Eq + Hash, V> UnordMap<K, V> {
398400 self . inner . extend ( items. 0 )
399401 }
400402
403+ /// Returns the entries of this map in stable sort order (as defined by `ToStableHashKey`).
404+ ///
405+ /// The `cache_sort_key` parameter controls if [slice::sort_by_cached_key] or
406+ /// [slice::sort_unstable_by_key] will be used for sorting the vec. Use
407+ /// `cache_sort_key` when the [ToStableHashKey::to_stable_hash_key] implementation
408+ /// for `K` is expensive (e.g. a `DefId -> DefPathHash` lookup).
401409 #[ inline]
402410 pub fn to_sorted < HCX > ( & self , hcx : & HCX , cache_sort_key : bool ) -> Vec < ( & K , & V ) >
403411 where
404412 K : ToStableHashKey < HCX > ,
405413 {
406- let mut items: Vec < ( & K , & V ) > = self . inner . iter ( ) . collect ( ) ;
407- if cache_sort_key {
408- items. sort_by_cached_key ( |( k, _) | k. to_stable_hash_key ( hcx) ) ;
409- } else {
410- items. sort_unstable_by_key ( |( k, _) | k. to_stable_hash_key ( hcx) ) ;
411- }
412-
413- items
414+ to_sorted_vec ( hcx, self . inner . iter ( ) , cache_sort_key, |& ( k, _) | k)
414415 }
415416
417+ /// Returns the entries of this map in stable sort order (as defined by `StableOrd`).
418+ /// This method can be much more efficient than `into_sorted` because it does not need
419+ /// to transform keys to their `ToStableHashKey` equivalent.
416420 #[ inline]
417421 pub fn to_sorted_stable_ord ( & self ) -> Vec < ( K , & V ) >
418422 where
@@ -423,32 +427,35 @@ impl<K: Eq + Hash, V> UnordMap<K, V> {
423427 items
424428 }
425429
430+ /// Returns the entries of this map in stable sort order (as defined by `ToStableHashKey`).
431+ ///
432+ /// The `cache_sort_key` parameter controls if [slice::sort_by_cached_key] or
433+ /// [slice::sort_unstable_by_key] will be used for sorting the vec. Use
434+ /// `cache_sort_key` when the [ToStableHashKey::to_stable_hash_key] implementation
435+ /// for `K` is expensive (e.g. a `DefId -> DefPathHash` lookup).
426436 #[ inline]
427437 pub fn into_sorted < HCX > ( self , hcx : & HCX , cache_sort_key : bool ) -> Vec < ( K , V ) >
428438 where
429439 K : ToStableHashKey < HCX > ,
430440 {
431- let mut items: Vec < ( K , V ) > = self . inner . into_iter ( ) . collect ( ) ;
432- if cache_sort_key {
433- items. sort_by_cached_key ( |( k, _) | k. to_stable_hash_key ( hcx) ) ;
434- } else {
435- items. sort_unstable_by_key ( |( k, _) | k. to_stable_hash_key ( hcx) ) ;
436- }
437- items
441+ to_sorted_vec ( hcx, self . inner . into_iter ( ) , cache_sort_key, |( k, _) | k)
438442 }
439443
444+ /// Returns the values of this map in stable sort order (as defined by K's
445+ /// `ToStableHashKey` implementation).
446+ ///
447+ /// The `cache_sort_key` parameter controls if [slice::sort_by_cached_key] or
448+ /// [slice::sort_unstable_by_key] will be used for sorting the vec. Use
449+ /// `cache_sort_key` when the [ToStableHashKey::to_stable_hash_key] implementation
450+ /// for `K` is expensive (e.g. a `DefId -> DefPathHash` lookup).
440451 #[ inline]
441452 pub fn values_sorted < HCX > ( & self , hcx : & HCX , cache_sort_key : bool ) -> impl Iterator < Item = & V >
442453 where
443454 K : ToStableHashKey < HCX > ,
444455 {
445- let mut items: Vec < ( & K , & V ) > = self . inner . iter ( ) . collect ( ) ;
446- if cache_sort_key {
447- items. sort_by_cached_key ( |( k, _) | k. to_stable_hash_key ( hcx) ) ;
448- } else {
449- items. sort_unstable_by_key ( |( k, _) | k. to_stable_hash_key ( hcx) ) ;
450- }
451- items. into_iter ( ) . map ( |( _, v) | v)
456+ to_sorted_vec ( hcx, self . inner . iter ( ) , cache_sort_key, |& ( k, _) | k)
457+ . into_iter ( )
458+ . map ( |( _, v) | v)
452459 }
453460}
454461
@@ -540,6 +547,27 @@ impl<HCX, V: Hash + Eq + HashStable<HCX>> HashStable<HCX> for UnordBag<V> {
540547 }
541548}
542549
550+ #[ inline]
551+ fn to_sorted_vec < HCX , T , K , I > (
552+ hcx : & HCX ,
553+ iter : I ,
554+ cache_sort_key : bool ,
555+ extract_key : fn ( & T ) -> & K ,
556+ ) -> Vec < T >
557+ where
558+ I : Iterator < Item = T > ,
559+ K : ToStableHashKey < HCX > ,
560+ {
561+ let mut items: Vec < T > = iter. collect ( ) ;
562+ if cache_sort_key {
563+ items. sort_by_cached_key ( |x| extract_key ( x) . to_stable_hash_key ( hcx) ) ;
564+ } else {
565+ items. sort_unstable_by_key ( |x| extract_key ( x) . to_stable_hash_key ( hcx) ) ;
566+ }
567+
568+ items
569+ }
570+
543571fn hash_iter_order_independent <
544572 HCX ,
545573 T : HashStable < HCX > ,
0 commit comments