1
1
//! Comparison traits for `[T]`.
2
2
3
3
use crate :: cmp:: { self , BytewiseEq , Ordering } ;
4
- use crate :: ffi ;
4
+ use crate :: intrinsics :: compare_bytes ;
5
5
use crate :: mem;
6
6
7
7
use super :: from_raw_parts;
8
8
use super :: memchr;
9
9
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
-
20
10
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
21
11
impl < A , B > PartialEq < [ B ] > for [ A ]
22
12
where
74
64
}
75
65
}
76
66
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.
78
69
impl < A , B > SlicePartialEq < B > for [ A ]
79
70
where
80
71
A : BytewiseEq < B > ,
88
79
// The two slices have been checked to have the same size above.
89
80
unsafe {
90
81
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
92
83
}
93
84
}
94
85
}
@@ -183,7 +174,7 @@ impl<A: Ord> SliceOrd for A {
183
174
}
184
175
}
185
176
186
- // memcmp compares a sequence of unsigned bytes lexicographically.
177
+ // `compare_bytes` compares a sequence of unsigned bytes lexicographically.
187
178
// this matches the order we want for [u8], but no others (not even [i8]).
188
179
impl SliceOrd for u8 {
189
180
#[ inline]
@@ -195,7 +186,7 @@ impl SliceOrd for u8 {
195
186
// SAFETY: `left` and `right` are references and are thus guaranteed to be valid.
196
187
// We use the minimum of both lengths which guarantees that both regions are
197
188
// 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 } ;
199
190
if order == 0 {
200
191
order = diff;
201
192
}
0 commit comments