@@ -5584,21 +5584,18 @@ where
5584
5584
5585
5585
#[ doc( hidden) ]
5586
5586
// 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 > ;
5589
5589
}
5590
5590
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 ( ) ) ;
5597
5594
5598
5595
// Slice to the loop iteration range to enable bound check
5599
5596
// elimination in the compiler
5600
- let lhs = & self [ ..l] ;
5601
- let rhs = & other [ ..l] ;
5597
+ let lhs = & left [ ..l] ;
5598
+ let rhs = & right [ ..l] ;
5602
5599
5603
5600
for i in 0 ..l {
5604
5601
match lhs[ i] . partial_cmp ( & rhs[ i] ) {
@@ -5607,36 +5604,61 @@ where
5607
5604
}
5608
5605
}
5609
5606
5610
- self . len ( ) . partial_cmp ( & other . len ( ) )
5607
+ left . len ( ) . partial_cmp ( & right . len ( ) )
5611
5608
}
5612
5609
}
5613
5610
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
5615
5615
where
5616
5616
A: Ord,
5617
5617
{
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 { } ) *
5620
5635
}
5621
5636
}
5622
5637
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
+
5623
5648
#[ doc( hidden) ]
5624
5649
// 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 ;
5627
5652
}
5628
5653
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 ( ) ) ;
5635
5657
5636
5658
// Slice to the loop iteration range to enable bound check
5637
5659
// elimination in the compiler
5638
- let lhs = & self [ ..l] ;
5639
- let rhs = & other [ ..l] ;
5660
+ let lhs = & left [ ..l] ;
5661
+ let rhs = & right [ ..l] ;
5640
5662
5641
5663
for i in 0 ..l {
5642
5664
match lhs[ i] . cmp ( & rhs[ i] ) {
@@ -5645,19 +5667,19 @@ where
5645
5667
}
5646
5668
}
5647
5669
5648
- self . len ( ) . cmp ( & other . len ( ) )
5670
+ left . len ( ) . cmp ( & right . len ( ) )
5649
5671
}
5650
5672
}
5651
5673
5652
5674
// memcmp compares a sequence of unsigned bytes lexicographically.
5653
5675
// 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 {
5655
5677
#[ inline]
5656
- fn compare ( & self , other : & [ u8 ] ) -> Ordering {
5678
+ fn compare ( left : & [ Self ] , right : & [ Self ] ) -> Ordering {
5657
5679
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 ( ) ) ) } ;
5659
5681
if order == 0 {
5660
- self . len ( ) . cmp ( & other . len ( ) )
5682
+ left . len ( ) . cmp ( & right . len ( ) )
5661
5683
} else if order < 0 {
5662
5684
Less
5663
5685
} else {
0 commit comments