22
22
23
23
#![ stable( feature = "rust1" , since = "1.0.0" ) ]
24
24
25
+ use crate :: const_closure:: ConstFnMutClosure ;
25
26
use crate :: marker:: Destruct ;
26
27
use crate :: marker:: StructuralPartialEq ;
27
28
@@ -1222,7 +1223,12 @@ pub const fn min<T: ~const Ord + ~const Destruct>(v1: T, v2: T) -> T {
1222
1223
#[ inline]
1223
1224
#[ must_use]
1224
1225
#[ stable( feature = "cmp_min_max_by" , since = "1.53.0" ) ]
1225
- pub fn min_by < T , F : FnOnce ( & T , & T ) -> Ordering > ( v1 : T , v2 : T , compare : F ) -> T {
1226
+ #[ rustc_const_unstable( feature = "const_cmp" , issue = "92391" ) ]
1227
+ pub const fn min_by < T , F : ~const FnOnce ( & T , & T ) -> Ordering > ( v1 : T , v2 : T , compare : F ) -> T
1228
+ where
1229
+ T : ~const Destruct ,
1230
+ F : ~const Destruct ,
1231
+ {
1226
1232
match compare ( & v1, & v2) {
1227
1233
Ordering :: Less | Ordering :: Equal => v1,
1228
1234
Ordering :: Greater => v2,
@@ -1244,8 +1250,24 @@ pub fn min_by<T, F: FnOnce(&T, &T) -> Ordering>(v1: T, v2: T, compare: F) -> T {
1244
1250
#[ inline]
1245
1251
#[ must_use]
1246
1252
#[ stable( feature = "cmp_min_max_by" , since = "1.53.0" ) ]
1247
- pub fn min_by_key < T , F : FnMut ( & T ) -> K , K : Ord > ( v1 : T , v2 : T , mut f : F ) -> T {
1248
- min_by ( v1, v2, |v1, v2| f ( v1) . cmp ( & f ( v2) ) )
1253
+ #[ rustc_const_unstable( feature = "const_cmp" , issue = "92391" ) ]
1254
+ pub const fn min_by_key < T , F : ~const FnMut ( & T ) -> K , K : ~const Ord > ( v1 : T , v2 : T , mut f : F ) -> T
1255
+ where
1256
+ T : ~const Destruct ,
1257
+ F : ~const Destruct ,
1258
+ K : ~const Destruct ,
1259
+ {
1260
+ const fn imp < T , F : ~const FnMut ( & T ) -> K , K : ~const Ord > (
1261
+ f : & mut F ,
1262
+ ( v1, v2) : ( & T , & T ) ,
1263
+ ) -> Ordering
1264
+ where
1265
+ T : ~const Destruct ,
1266
+ K : ~const Destruct ,
1267
+ {
1268
+ f ( v1) . cmp ( & f ( v2) )
1269
+ }
1270
+ min_by ( v1, v2, ConstFnMutClosure :: new ( & mut f, imp) )
1249
1271
}
1250
1272
1251
1273
/// Compares and returns the maximum of two values.
@@ -1286,7 +1308,12 @@ pub const fn max<T: ~const Ord + ~const Destruct>(v1: T, v2: T) -> T {
1286
1308
#[ inline]
1287
1309
#[ must_use]
1288
1310
#[ stable( feature = "cmp_min_max_by" , since = "1.53.0" ) ]
1289
- pub fn max_by < T , F : FnOnce ( & T , & T ) -> Ordering > ( v1 : T , v2 : T , compare : F ) -> T {
1311
+ #[ rustc_const_unstable( feature = "const_cmp" , issue = "92391" ) ]
1312
+ pub const fn max_by < T , F : ~const FnOnce ( & T , & T ) -> Ordering > ( v1 : T , v2 : T , compare : F ) -> T
1313
+ where
1314
+ T : ~const Destruct ,
1315
+ F : ~const Destruct ,
1316
+ {
1290
1317
match compare ( & v1, & v2) {
1291
1318
Ordering :: Less | Ordering :: Equal => v2,
1292
1319
Ordering :: Greater => v1,
@@ -1308,8 +1335,24 @@ pub fn max_by<T, F: FnOnce(&T, &T) -> Ordering>(v1: T, v2: T, compare: F) -> T {
1308
1335
#[ inline]
1309
1336
#[ must_use]
1310
1337
#[ stable( feature = "cmp_min_max_by" , since = "1.53.0" ) ]
1311
- pub fn max_by_key < T , F : FnMut ( & T ) -> K , K : Ord > ( v1 : T , v2 : T , mut f : F ) -> T {
1312
- max_by ( v1, v2, |v1, v2| f ( v1) . cmp ( & f ( v2) ) )
1338
+ #[ rustc_const_unstable( feature = "const_cmp" , issue = "92391" ) ]
1339
+ pub const fn max_by_key < T , F : ~const FnMut ( & T ) -> K , K : ~const Ord > ( v1 : T , v2 : T , mut f : F ) -> T
1340
+ where
1341
+ T : ~const Destruct ,
1342
+ F : ~const Destruct ,
1343
+ K : ~const Destruct ,
1344
+ {
1345
+ const fn imp < T , F : ~const FnMut ( & T ) -> K , K : ~const Ord > (
1346
+ f : & mut F ,
1347
+ ( v1, v2) : ( & T , & T ) ,
1348
+ ) -> Ordering
1349
+ where
1350
+ T : ~const Destruct ,
1351
+ K : ~const Destruct ,
1352
+ {
1353
+ f ( v1) . cmp ( & f ( v2) )
1354
+ }
1355
+ max_by ( v1, v2, ConstFnMutClosure :: new ( & mut f, imp) )
1313
1356
}
1314
1357
1315
1358
// Implementation of PartialEq, Eq, PartialOrd and Ord for primitive types
0 commit comments