11//! Comparison traits for `[T]`.
22
33use crate :: cmp:: { self , BytewiseEq , Ordering } ;
4- use crate :: ffi ;
4+ use crate :: intrinsics :: compare_bytes ;
55use crate :: mem;
66
77use super :: from_raw_parts;
88use super :: memchr;
99
10- extern "C" {
11- /// Calls implementation provided memcmp.
12- ///
13- /// Interprets the data as u8.
14- ///
15- /// Returns 0 for equal, < 0 for less than and > 0 for greater
16- /// than.
17- fn memcmp ( s1 : * const u8 , s2 : * const u8 , n : usize ) -> ffi:: c_int ;
18- }
19-
2010#[ stable( feature = "rust1" , since = "1.0.0" ) ]
2111impl < A , B > PartialEq < [ B ] > for [ A ]
2212where
7464 }
7565}
7666
77- // Use memcmp for bytewise equality when the types allow
67+ // When each element can be compared byte-wise, we can compare all the bytes
68+ // from the whole size in one call to the intrinsics.
7869impl < A , B > SlicePartialEq < B > for [ A ]
7970where
8071 A : BytewiseEq < B > ,
8879 // The two slices have been checked to have the same size above.
8980 unsafe {
9081 let size = mem:: size_of_val ( self ) ;
91- memcmp ( self . as_ptr ( ) as * const u8 , other. as_ptr ( ) as * const u8 , size) == 0
82+ compare_bytes ( self . as_ptr ( ) as * const u8 , other. as_ptr ( ) as * const u8 , size) == 0
9283 }
9384 }
9485}
@@ -183,7 +174,7 @@ impl<A: Ord> SliceOrd for A {
183174 }
184175}
185176
186- // memcmp compares a sequence of unsigned bytes lexicographically.
177+ // `compare_bytes` compares a sequence of unsigned bytes lexicographically.
187178// this matches the order we want for [u8], but no others (not even [i8]).
188179impl SliceOrd for u8 {
189180 #[ inline]
@@ -195,7 +186,7 @@ impl SliceOrd for u8 {
195186 // SAFETY: `left` and `right` are references and are thus guaranteed to be valid.
196187 // We use the minimum of both lengths which guarantees that both regions are
197188 // valid for reads in that interval.
198- let mut order = unsafe { memcmp ( left. as_ptr ( ) , right. as_ptr ( ) , len) as isize } ;
189+ let mut order = unsafe { compare_bytes ( left. as_ptr ( ) , right. as_ptr ( ) , len) as isize } ;
199190 if order == 0 {
200191 order = diff;
201192 }
0 commit comments