2222
2323#![ stable( feature = "rust1" , since = "1.0.0" ) ]
2424
25+ use crate :: const_closure:: ConstFnMutClosure ;
2526use crate :: marker:: Destruct ;
2627use crate :: marker:: StructuralPartialEq ;
2728
@@ -1222,7 +1223,12 @@ pub const fn min<T: ~const Ord + ~const Destruct>(v1: T, v2: T) -> T {
12221223#[ inline]
12231224#[ must_use]
12241225#[ 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+ {
12261232 match compare ( & v1, & v2) {
12271233 Ordering :: Less | Ordering :: Equal => v1,
12281234 Ordering :: Greater => v2,
@@ -1244,8 +1250,24 @@ pub fn min_by<T, F: FnOnce(&T, &T) -> Ordering>(v1: T, v2: T, compare: F) -> T {
12441250#[ inline]
12451251#[ must_use]
12461252#[ 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) )
12491271}
12501272
12511273/// 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 {
12861308#[ inline]
12871309#[ must_use]
12881310#[ 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+ {
12901317 match compare ( & v1, & v2) {
12911318 Ordering :: Less | Ordering :: Equal => v2,
12921319 Ordering :: Greater => v1,
@@ -1308,8 +1335,24 @@ pub fn max_by<T, F: FnOnce(&T, &T) -> Ordering>(v1: T, v2: T, compare: F) -> T {
13081335#[ inline]
13091336#[ must_use]
13101337#[ 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) )
13131356}
13141357
13151358// Implementation of PartialEq, Eq, PartialOrd and Ord for primitive types
0 commit comments