@@ -5584,21 +5584,18 @@ where
55845584
55855585#[ doc( hidden) ]
55865586// intermediate trait for specialization of slice's PartialOrd
5587- trait SlicePartialOrd < B > {
5588- fn partial_compare ( & self , other : & [ B ] ) -> Option < Ordering > ;
5587+ trait SlicePartialOrd : Sized {
5588+ fn partial_compare ( left : & [ Self ] , right : & [ Self ] ) -> Option < Ordering > ;
55895589}
55905590
5591- impl < A > SlicePartialOrd < A > for [ A ]
5592- where
5593- A : PartialOrd ,
5594- {
5595- default fn partial_compare ( & self , other : & [ A ] ) -> Option < Ordering > {
5596- let l = cmp:: min ( self . len ( ) , other. len ( ) ) ;
5591+ impl < A : PartialOrd > SlicePartialOrd for A {
5592+ default fn partial_compare ( left : & [ A ] , right : & [ A ] ) -> Option < Ordering > {
5593+ let l = cmp:: min ( left. len ( ) , right. len ( ) ) ;
55975594
55985595 // Slice to the loop iteration range to enable bound check
55995596 // elimination in the compiler
5600- let lhs = & self [ ..l] ;
5601- let rhs = & other [ ..l] ;
5597+ let lhs = & left [ ..l] ;
5598+ let rhs = & right [ ..l] ;
56025599
56035600 for i in 0 ..l {
56045601 match lhs[ i] . partial_cmp ( & rhs[ i] ) {
@@ -5607,36 +5604,61 @@ where
56075604 }
56085605 }
56095606
5610- self . len ( ) . partial_cmp ( & other . len ( ) )
5607+ left . len ( ) . partial_cmp ( & right . len ( ) )
56115608 }
56125609}
56135610
5614- impl < A > SlicePartialOrd < A > for [ A ]
5611+ // This is the impl that we would like to have. Unfortunately it's not sound.
5612+ // See `partial_ord_slice.rs`.
5613+ /*
5614+ impl<A> SlicePartialOrd for A
56155615where
56165616 A: Ord,
56175617{
5618- default fn partial_compare ( & self , other : & [ A ] ) -> Option < Ordering > {
5619- Some ( SliceOrd :: compare ( self , other) )
5618+ default fn partial_compare(left: &[A], right: &[A]) -> Option<Ordering> {
5619+ Some(SliceOrd::compare(left, right))
5620+ }
5621+ }
5622+ */
5623+
5624+ impl < A : AlwaysApplicableOrd > SlicePartialOrd for A {
5625+ fn partial_compare ( left : & [ A ] , right : & [ A ] ) -> Option < Ordering > {
5626+ Some ( SliceOrd :: compare ( left, right) )
5627+ }
5628+ }
5629+
5630+ trait AlwaysApplicableOrd : SliceOrd + Ord { }
5631+
5632+ macro_rules! always_applicable_ord {
5633+ ( $( [ $( $p: tt) * ] $t: ty, ) * ) => {
5634+ $( impl <$( $p) * > AlwaysApplicableOrd for $t { } ) *
56205635 }
56215636}
56225637
5638+ always_applicable_ord ! {
5639+ [ ] u8 , [ ] u16 , [ ] u32 , [ ] u64 , [ ] u128 , [ ] usize ,
5640+ [ ] i8 , [ ] i16 , [ ] i32 , [ ] i64 , [ ] i128 , [ ] isize ,
5641+ [ ] bool , [ ] char ,
5642+ [ T : ?Sized ] * const T , [ T : ?Sized ] * mut T ,
5643+ [ T : AlwaysApplicableOrd ] & T ,
5644+ [ T : AlwaysApplicableOrd ] & mut T ,
5645+ [ T : AlwaysApplicableOrd ] Option <T >,
5646+ }
5647+
56235648#[ doc( hidden) ]
56245649// intermediate trait for specialization of slice's Ord
5625- trait SliceOrd < B > {
5626- fn compare ( & self , other : & [ B ] ) -> Ordering ;
5650+ trait SliceOrd : Sized {
5651+ fn compare ( left : & [ Self ] , right : & [ Self ] ) -> Ordering ;
56275652}
56285653
5629- impl < A > SliceOrd < A > for [ A ]
5630- where
5631- A : Ord ,
5632- {
5633- default fn compare ( & self , other : & [ A ] ) -> Ordering {
5634- let l = cmp:: min ( self . len ( ) , other. len ( ) ) ;
5654+ impl < A : Ord > SliceOrd for A {
5655+ default fn compare ( left : & [ Self ] , right : & [ Self ] ) -> Ordering {
5656+ let l = cmp:: min ( left. len ( ) , right. len ( ) ) ;
56355657
56365658 // Slice to the loop iteration range to enable bound check
56375659 // elimination in the compiler
5638- let lhs = & self [ ..l] ;
5639- let rhs = & other [ ..l] ;
5660+ let lhs = & left [ ..l] ;
5661+ let rhs = & right [ ..l] ;
56405662
56415663 for i in 0 ..l {
56425664 match lhs[ i] . cmp ( & rhs[ i] ) {
@@ -5645,19 +5667,19 @@ where
56455667 }
56465668 }
56475669
5648- self . len ( ) . cmp ( & other . len ( ) )
5670+ left . len ( ) . cmp ( & right . len ( ) )
56495671 }
56505672}
56515673
56525674// memcmp compares a sequence of unsigned bytes lexicographically.
56535675// this matches the order we want for [u8], but no others (not even [i8]).
5654- impl SliceOrd < u8 > for [ u8 ] {
5676+ impl SliceOrd for u8 {
56555677 #[ inline]
5656- fn compare ( & self , other : & [ u8 ] ) -> Ordering {
5678+ fn compare ( left : & [ Self ] , right : & [ Self ] ) -> Ordering {
56575679 let order =
5658- unsafe { memcmp ( self . as_ptr ( ) , other . as_ptr ( ) , cmp:: min ( self . len ( ) , other . len ( ) ) ) } ;
5680+ unsafe { memcmp ( left . as_ptr ( ) , right . as_ptr ( ) , cmp:: min ( left . len ( ) , right . len ( ) ) ) } ;
56595681 if order == 0 {
5660- self . len ( ) . cmp ( & other . len ( ) )
5682+ left . len ( ) . cmp ( & right . len ( ) )
56615683 } else if order < 0 {
56625684 Less
56635685 } else {
0 commit comments