@@ -14,7 +14,7 @@ use std::{
1414
1515use crate :: {
1616 fingerprint:: Fingerprint ,
17- stable_hasher:: { HashStable , StableHasher , StableOrd , ToStableHashKey } ,
17+ stable_hasher:: { HashStable , StableCompare , StableHasher , ToStableHashKey } ,
1818} ;
1919
2020/// `UnordItems` is the order-less version of `Iterator`. It only contains methods
@@ -134,7 +134,7 @@ impl<'a, T: Copy + 'a, I: Iterator<Item = &'a T>> UnordItems<&'a T, I> {
134134 }
135135}
136136
137- impl < T : Ord , I : Iterator < Item = T > > UnordItems < T , I > {
137+ impl < T , I : Iterator < Item = T > > UnordItems < T , I > {
138138 pub fn into_sorted < HCX > ( self , hcx : & HCX ) -> Vec < T >
139139 where
140140 T : ToStableHashKey < HCX > ,
@@ -147,13 +147,36 @@ impl<T: Ord, I: Iterator<Item = T>> UnordItems<T, I> {
147147 #[ inline]
148148 pub fn into_sorted_stable_ord ( self ) -> Vec < T >
149149 where
150- T : Ord + StableOrd ,
150+ T : StableCompare ,
151151 {
152152 let mut items: Vec < T > = self . 0 . collect ( ) ;
153153 if !T :: CAN_USE_UNSTABLE_SORT {
154- items. sort ( ) ;
154+ items. sort_by ( T :: stable_cmp) ;
155+ } else {
156+ items. sort_unstable_by ( T :: stable_cmp)
157+ }
158+ items
159+ }
160+
161+ #[ inline]
162+ pub fn into_sorted_stable_ord_by_key < K , C > ( self , project_to_key : C ) -> Vec < T >
163+ where
164+ K : StableCompare ,
165+ C : for < ' a > Fn ( & ' a T ) -> & ' a K ,
166+ {
167+ let mut items: Vec < T > = self . 0 . collect ( ) ;
168+ if !K :: CAN_USE_UNSTABLE_SORT {
169+ items. sort_by ( |a, b| {
170+ let a_key = project_to_key ( a) ;
171+ let b_key = project_to_key ( b) ;
172+ a_key. stable_cmp ( b_key)
173+ } ) ;
155174 } else {
156- items. sort_unstable ( )
175+ items. sort_unstable_by ( |a, b| {
176+ let a_key = project_to_key ( a) ;
177+ let b_key = project_to_key ( b) ;
178+ a_key. stable_cmp ( b_key)
179+ } ) ;
157180 }
158181 items
159182 }
@@ -268,16 +291,30 @@ impl<V: Eq + Hash> UnordSet<V> {
268291 }
269292
270293 /// Returns the items of this set in stable sort order (as defined by
271- /// `StableOrd `). This method is much more efficient than
294+ /// `StableCompare `). This method is much more efficient than
272295 /// `into_sorted` because it does not need to transform keys to their
273296 /// `ToStableHashKey` equivalent.
274297 #[ inline]
275- pub fn to_sorted_stable_ord ( & self ) -> Vec < V >
298+ pub fn to_sorted_stable_ord ( & self ) -> Vec < & V >
276299 where
277- V : Ord + StableOrd + Clone ,
300+ V : StableCompare ,
278301 {
279- let mut items: Vec < V > = self . inner . iter ( ) . cloned ( ) . collect ( ) ;
280- items. sort_unstable ( ) ;
302+ let mut items: Vec < & V > = self . inner . iter ( ) . collect ( ) ;
303+ items. sort_unstable_by ( |a, b| a. stable_cmp ( * b) ) ;
304+ items
305+ }
306+
307+ /// Returns the items of this set in stable sort order (as defined by
308+ /// `StableCompare`). This method is much more efficient than
309+ /// `into_sorted` because it does not need to transform keys to their
310+ /// `ToStableHashKey` equivalent.
311+ #[ inline]
312+ pub fn into_sorted_stable_ord ( self ) -> Vec < V >
313+ where
314+ V : StableCompare ,
315+ {
316+ let mut items: Vec < V > = self . inner . into_iter ( ) . collect ( ) ;
317+ items. sort_unstable_by ( V :: stable_cmp) ;
281318 items
282319 }
283320
@@ -483,16 +520,16 @@ impl<K: Eq + Hash, V> UnordMap<K, V> {
483520 to_sorted_vec ( hcx, self . inner . iter ( ) , cache_sort_key, |& ( k, _) | k)
484521 }
485522
486- /// Returns the entries of this map in stable sort order (as defined by `StableOrd `).
523+ /// Returns the entries of this map in stable sort order (as defined by `StableCompare `).
487524 /// This method can be much more efficient than `into_sorted` because it does not need
488525 /// to transform keys to their `ToStableHashKey` equivalent.
489526 #[ inline]
490- pub fn to_sorted_stable_ord ( & self ) -> Vec < ( K , & V ) >
527+ pub fn to_sorted_stable_ord ( & self ) -> Vec < ( & K , & V ) >
491528 where
492- K : Ord + StableOrd + Copy ,
529+ K : StableCompare ,
493530 {
494- let mut items: Vec < ( K , & V ) > = self . inner . iter ( ) . map ( | ( & k , v ) | ( k , v ) ) . collect ( ) ;
495- items. sort_unstable_by_key ( | & ( k , _) | k ) ;
531+ let mut items: Vec < _ > = self . inner . iter ( ) . collect ( ) ;
532+ items. sort_unstable_by ( | ( a , _) , ( b , _ ) | a . stable_cmp ( * b ) ) ;
496533 items
497534 }
498535
@@ -510,6 +547,19 @@ impl<K: Eq + Hash, V> UnordMap<K, V> {
510547 to_sorted_vec ( hcx, self . inner . into_iter ( ) , cache_sort_key, |( k, _) | k)
511548 }
512549
550+ /// Returns the entries of this map in stable sort order (as defined by `StableCompare`).
551+ /// This method can be much more efficient than `into_sorted` because it does not need
552+ /// to transform keys to their `ToStableHashKey` equivalent.
553+ #[ inline]
554+ pub fn into_sorted_stable_ord ( self ) -> Vec < ( K , V ) >
555+ where
556+ K : StableCompare ,
557+ {
558+ let mut items: Vec < ( K , V ) > = self . inner . into_iter ( ) . collect ( ) ;
559+ items. sort_unstable_by ( |a, b| a. 0 . stable_cmp ( & b. 0 ) ) ;
560+ items
561+ }
562+
513563 /// Returns the values of this map in stable sort order (as defined by K's
514564 /// `ToStableHashKey` implementation).
515565 ///
0 commit comments