3
3
use super :: { from_raw_parts, memchr} ;
4
4
use crate :: ascii;
5
5
use crate :: cmp:: { self , BytewiseEq , Ordering } ;
6
+ use crate :: convert:: Infallible ;
6
7
use crate :: intrinsics:: compare_bytes;
8
+ use crate :: marker:: Destruct ;
7
9
use crate :: num:: NonZero ;
8
10
use crate :: ops:: ControlFlow ;
9
11
@@ -23,18 +25,20 @@ where
23
25
}
24
26
25
27
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
26
- impl < T : Eq > Eq for [ T ] { }
28
+ #[ rustc_const_unstable( feature = "const_cmp" , issue = "143800" ) ]
29
+ impl < T : [ const ] Eq > const Eq for [ T ] { }
27
30
28
31
/// Implements comparison of slices [lexicographically](Ord#lexicographical-comparison).
29
32
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
30
- impl < T : Ord > Ord for [ T ] {
33
+ #[ rustc_const_unstable( feature = "const_cmp" , issue = "143800" ) ]
34
+ impl <T : [ const ] Ord > const Ord for [ T ] {
31
35
fn cmp ( & self , other : & [ T ] ) -> Ordering {
32
36
SliceOrd :: compare ( self , other)
33
37
}
34
38
}
35
39
36
40
#[ inline]
37
- fn as_underlying ( x : ControlFlow < bool > ) -> u8 {
41
+ const fn as_underlying ( x : ControlFlow < bool > ) -> u8 {
38
42
// SAFETY: This will only compile if `bool` and `ControlFlow<bool>` have the same
39
43
// size (which isn't guaranteed but this is libcore). Because they have the same
40
44
// size, it's a niched implementation, which in one byte means there can't be
@@ -46,7 +50,8 @@ fn as_underlying(x: ControlFlow<bool>) -> u8 {
46
50
47
51
/// Implements comparison of slices [lexicographically](Ord#lexicographical-comparison).
48
52
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
49
- impl < T : PartialOrd > PartialOrd for [ T ] {
53
+ #[ rustc_const_unstable( feature = "const_cmp" , issue = "143800" ) ]
54
+ impl <T : [ const ] PartialOrd > const PartialOrd for [ T ] {
50
55
#[ inline ]
51
56
fn partial_cmp ( & self , other : & [ T ] ) -> Option < Ordering > {
52
57
SlicePartialOrd :: partial_compare ( self , other )
@@ -154,35 +159,50 @@ where
154
159
}
155
160
156
161
#[ doc( hidden) ]
162
+ #[ rustc_const_unstable( feature = "const_cmp" , issue = "143800" ) ]
157
163
// intermediate trait for specialization of slice's PartialOrd
158
- trait SlicePartialOrd : Sized {
164
+ const trait SlicePartialOrd : Sized {
159
165
fn partial_compare ( left : & [ Self ] , right : & [ Self ] ) -> Option < Ordering > ;
160
166
}
161
167
162
168
#[ doc ( hidden ) ]
169
+ #[ rustc_const_unstable ( feature = "const_cmp" , issue = "143800" ) ]
163
170
// intermediate trait for specialization of slice's PartialOrd chaining methods
164
- trait SliceChain : Sized {
171
+ const trait SliceChain : Sized {
165
172
fn chaining_lt ( left : & [ Self ] , right : & [ Self ] ) -> ControlFlow < bool > ;
166
173
fn chaining_le ( left : & [ Self ] , right : & [ Self ] ) -> ControlFlow < bool > ;
167
174
fn chaining_gt ( left : & [ Self ] , right : & [ Self ] ) -> ControlFlow < bool > ;
168
175
fn chaining_ge ( left : & [ Self ] , right : & [ Self ] ) -> ControlFlow < bool > ;
169
176
}
170
177
171
- type AlwaysBreak < B > = ControlFlow < B , crate :: convert :: Infallible > ;
178
+ type AlwaysBreak < B > = ControlFlow < B , Infallible > ;
172
179
173
- impl < A : PartialOrd > SlicePartialOrd for A {
180
+ #[ rustc_const_unstable ( feature = "const_cmp" , issue = "143800" ) ]
181
+ const fn elem_chain < B > ( a : & B , b : & B ) -> ControlFlow < Option < Ordering > >
182
+ where
183
+ B : [ const ] PartialOrd ,
184
+ {
185
+ match a. partial_cmp ( b ) {
186
+ Some ( Ordering :: Equal ) => ControlFlow :: Continue ( ( ) ) ,
187
+ non_eq => ControlFlow :: Break ( non_eq ) ,
188
+ }
189
+ }
190
+
191
+ #[ rustc_const_unstable ( feature = "const_cmp" , issue = "143800" ) ]
192
+ const fn len_chain ( a : & usize , b : & usize ) -> ControlFlow < Option < Ordering > , Infallible > {
193
+ ControlFlow :: Break ( usize:: partial_cmp ( a , b ) )
194
+ }
195
+
196
+ #[ rustc_const_unstable ( feature = "const_cmp" , issue = "143800" ) ]
197
+ impl <A : [ const ] PartialOrd > const SlicePartialOrd for A {
174
198
default fn partial_compare ( left : & [ A ] , right : & [ A ] ) -> Option < Ordering > {
175
- let elem_chain = |a, b| match PartialOrd :: partial_cmp ( a, b) {
176
- Some ( Ordering :: Equal ) => ControlFlow :: Continue ( ( ) ) ,
177
- non_eq => ControlFlow :: Break ( non_eq) ,
178
- } ;
179
- let len_chain = |a : & _ , b : & _ | ControlFlow :: Break ( usize:: partial_cmp ( a, b) ) ;
180
199
let AlwaysBreak :: Break ( b ) = chaining_impl ( left , right , elem_chain , len_chain ) ;
181
200
b
182
201
}
183
202
}
184
203
185
- impl < A : PartialOrd > SliceChain for A {
204
+ #[ rustc_const_unstable ( feature = "const_cmp" , issue = "143800" ) ]
205
+ impl<A : [ const ] PartialOrd > const SliceChain for A {
186
206
default fn chaining_lt( left : & [ Self ] , right : & [ Self ] ) -> ControlFlow < bool > {
187
207
chaining_impl( left, right, PartialOrd :: __chaining_lt, usize:: __chaining_lt)
188
208
}
@@ -198,21 +218,28 @@ impl<A: PartialOrd> SliceChain for A {
198
218
}
199
219
200
220
#[ inline]
201
- fn chaining_impl < ' l , ' r , A : PartialOrd , B , C > (
221
+ #[ rustc_const_unstable( feature = "const_cmp" , issue = "143800" ) ]
222
+ const fn chaining_impl < ' l , ' r , A : PartialOrd , B , C , X , Y > (
202
223
left: & ' l [ A ] ,
203
224
right: & ' r [ A ] ,
204
- elem_chain : impl Fn ( & ' l A , & ' r A ) -> ControlFlow < B > ,
205
- len_chain : impl for < ' a > FnOnce ( & ' a usize , & ' a usize ) -> ControlFlow < B , C > ,
206
- ) -> ControlFlow < B , C > {
225
+ elem_chain: X ,
226
+ len_chain: Y ,
227
+ ) -> ControlFlow < B , C >
228
+ where
229
+ X : [ const ] Destruct + [ const ] Fn ( & ' l A , & ' r A ) -> ControlFlow < B > ,
230
+ Y : [ const ] Destruct + for < ' a > [ const ] FnOnce ( & ' a usize, & ' a usize) -> ControlFlow < B , C > ,
231
+ {
207
232
let l = cmp:: min( left. len( ) , right. len( ) ) ;
208
233
209
234
// Slice to the loop iteration range to enable bound check
210
235
// elimination in the compiler
211
236
let lhs = & left[ ..l] ;
212
237
let rhs = & right[ ..l] ;
213
238
214
- for i in 0 ..l {
239
+ let mut i = 0 ;
240
+ while i < l {
215
241
elem_chain ( & lhs[ i] , & rhs[ i] ) ?;
242
+ i += 1 ;
216
243
}
217
244
218
245
len_chain ( & left. len ( ) , & right. len ( ) )
@@ -231,46 +258,65 @@ where
231
258
}
232
259
*/
233
260
234
- impl < A : AlwaysApplicableOrd > SlicePartialOrd for A {
261
+ #[ rustc_const_unstable( feature = "const_cmp" , issue = "143800" ) ]
262
+ impl <A : [ const ] AlwaysApplicableOrd > const SlicePartialOrd for A {
235
263
fn partial_compare ( left : & [ A ] , right : & [ A ] ) -> Option < Ordering > {
236
264
Some ( SliceOrd :: compare ( left , right ) )
237
265
}
238
266
}
239
267
240
268
#[ rustc_specialization_trait ]
241
- trait AlwaysApplicableOrd : SliceOrd + Ord { }
269
+ #[ rustc_const_unstable ( feature = "const_cmp" , issue = "143800" ) ]
270
+ const trait AlwaysApplicableOrd : [ const ] SliceOrd + [ const ] Ord { }
242
271
243
272
macro_rules! always_applicable_ord {
244
273
( $( [ $( $p: tt) * ] $t: ty, ) * ) => {
245
- $( impl <$( $p) * > AlwaysApplicableOrd for $t { } ) *
274
+ $(
275
+ #[ rustc_const_unstable ( feature = "const_cmp" , issue = "143800" ) ]
276
+ impl <$( $p) * > const AlwaysApplicableOrd for $t { }
277
+ ) *
246
278
}
247
279
}
248
280
249
281
always_applicable_ord! {
250
282
[ ] u8 , [ ] u16 , [ ] u32 , [ ] u64 , [ ] u128 , [ ] usize ,
251
283
[ ] i8 , [ ] i16 , [ ] i32 , [ ] i64 , [ ] i128 , [ ] isize ,
252
284
[ ] bool , [ ] char ,
253
- [ T : ?Sized ] * const T , [ T : ?Sized ] * mut T ,
254
- [ T : AlwaysApplicableOrd ] & T ,
255
- [ T : AlwaysApplicableOrd ] & mut T ,
256
- [ T : AlwaysApplicableOrd ] Option <T >,
285
+ [ T : [ const ] AlwaysApplicableOrd ] & T ,
286
+ [ T : [ const ] AlwaysApplicableOrd ] & mut T ,
287
+ [ T : [ const ] AlwaysApplicableOrd ] Option <T >,
257
288
}
258
289
290
+ impl <T : ?Sized > AlwaysApplicableOrd for * const T { }
291
+ impl <T : ?Sized > AlwaysApplicableOrd for * mut T { }
292
+
259
293
#[ doc( hidden) ]
260
294
// intermediate trait for specialization of slice's Ord
261
- trait SliceOrd : Sized {
295
+ #[ rustc_const_unstable( feature = "const_cmp" , issue = "143800" ) ]
296
+ const trait SliceOrd : Sized {
262
297
fn compare( left: & [ Self ] , right: & [ Self ] ) -> Ordering ;
263
298
}
264
299
265
- impl < A : Ord > SliceOrd for A {
300
+ #[ rustc_const_unstable( feature = "const_cmp" , issue = "143800" ) ]
301
+ impl <A : [ const ] Ord > const SliceOrd for A {
266
302
default fn compare( left: & [ Self ] , right: & [ Self ] ) -> Ordering {
267
- let elem_chain = |a, b| match Ord :: cmp ( a, b) {
268
- Ordering :: Equal => ControlFlow :: Continue ( ( ) ) ,
269
- non_eq => ControlFlow :: Break ( non_eq) ,
270
- } ;
271
- let len_chain = |a : & _ , b : & _ | ControlFlow :: Break ( usize:: cmp ( a, b) ) ;
272
- let AlwaysBreak :: Break ( b) = chaining_impl ( left, right, elem_chain, len_chain) ;
273
- b
303
+ let l = cmp:: min( left. len( ) , right. len( ) ) ;
304
+
305
+ // Slice to the loop iteration range to enable bound check
306
+ // elimination in the compiler
307
+ let lhs = & left[ ..l] ;
308
+ let rhs = & right[ ..l] ;
309
+
310
+ let mut i = 0 ;
311
+ while i < l {
312
+ match Ord :: cmp( & lhs[ i] , & rhs[ i] ) {
313
+ Ordering :: Equal => { }
314
+ non_eq => return non_eq,
315
+ }
316
+ i += 1 ;
317
+ }
318
+
319
+ usize :: cmp( & left. len( ) , & right. len( ) )
274
320
}
275
321
}
276
322
@@ -282,17 +328,19 @@ impl<A: Ord> SliceOrd for A {
282
328
/// * For every `x` and `y` of this type, `Ord(x, y)` must return the same
283
329
/// value as `Ord::cmp(transmute::<_, u8>(x), transmute::<_, u8>(y))`.
284
330
#[ rustc_specialization_trait]
285
- unsafe trait UnsignedBytewiseOrd : Ord { }
331
+ #[ const_trait]
332
+ unsafe trait UnsignedBytewiseOrd : [ const ] Ord { }
286
333
287
- unsafe impl UnsignedBytewiseOrd for bool { }
288
- unsafe impl UnsignedBytewiseOrd for u8 { }
289
- unsafe impl UnsignedBytewiseOrd for NonZero < u8 > { }
290
- unsafe impl UnsignedBytewiseOrd for Option < NonZero < u8 > > { }
291
- unsafe impl UnsignedBytewiseOrd for ascii:: Char { }
334
+ unsafe impl const UnsignedBytewiseOrd for bool { }
335
+ unsafe impl const UnsignedBytewiseOrd for u8 { }
336
+ unsafe impl const UnsignedBytewiseOrd for NonZero <u8 > { }
337
+ unsafe impl const UnsignedBytewiseOrd for Option <NonZero <u8 >> { }
338
+ unsafe impl const UnsignedBytewiseOrd for ascii:: Char { }
292
339
293
340
// `compare_bytes` compares a sequence of unsigned bytes lexicographically, so
294
341
// use it if the requirements for `UnsignedBytewiseOrd` are fulfilled.
295
- impl < A : Ord + UnsignedBytewiseOrd > SliceOrd for A {
342
+ #[ rustc_const_unstable( feature = "const_cmp" , issue = "143800" ) ]
343
+ impl <A : [ const ] Ord + [ const ] UnsignedBytewiseOrd > const SliceOrd for A {
296
344
#[ inline]
297
345
fn compare( left: & [ Self ] , right: & [ Self ] ) -> Ordering {
298
346
// Since the length of a slice is always less than or equal to
@@ -317,7 +365,8 @@ impl<A: Ord + UnsignedBytewiseOrd> SliceOrd for A {
317
365
}
318
366
319
367
// Don't generate our own chaining loops for `memcmp`-able things either.
320
- impl < A : PartialOrd + UnsignedBytewiseOrd > SliceChain for A {
368
+ #[ rustc_const_unstable( feature = "const_cmp" , issue = "143800" ) ]
369
+ impl <A : [ const ] PartialOrd + [ const ] UnsignedBytewiseOrd > const SliceChain for A {
321
370
#[ inline]
322
371
fn chaining_lt( left: & [ Self ] , right: & [ Self ] ) -> ControlFlow <bool > {
323
372
match SliceOrd :: compare( left, right) {
0 commit comments