Skip to content

Commit 7463cf5

Browse files
authored
Rollup merge of #42496 - Razaekel:feature/integer_max-min, r=BurntSushi
Add max and min to Ord Pursuant to issue #25663, this PR adds max and min methods with default implementations to std::cmp::Ord. It also modifies std::cmp::max|min to internally alias to Ord::max|min, so that any overrides of the default implementations are automatically used by std::cmp::max|min. Closes #25663
2 parents 78d5d37 + a32ffc6 commit 7463cf5

File tree

5 files changed

+61
-2
lines changed

5 files changed

+61
-2
lines changed

src/doc/unstable-book/src/SUMMARY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,7 @@
165165
- [n16](library-features/n16.md)
166166
- [never_type_impls](library-features/never-type-impls.md)
167167
- [nonzero](library-features/nonzero.md)
168+
- [ord_max_min](library-features/ord-max-min.md)
168169
- [offset_to](library-features/offset-to.md)
169170
- [once_poison](library-features/once-poison.md)
170171
- [oom](library-features/oom.md)
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# `ord-max-min`
2+
3+
The tracking issue for this feature is: [#25663]
4+
5+
[#25663]: https://github.com/rust-lang/rust/issues/25663
6+
7+
------------------------

src/libcore/cmp.rs

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -443,6 +443,42 @@ pub trait Ord: Eq + PartialOrd<Self> {
443443
/// ```
444444
#[stable(feature = "rust1", since = "1.0.0")]
445445
fn cmp(&self, other: &Self) -> Ordering;
446+
447+
/// Compares and returns the maximum of two values.
448+
///
449+
/// Returns the second argument if the comparison determines them to be equal.
450+
///
451+
/// # Examples
452+
///
453+
/// ```
454+
/// #![feature(ord_max_min)]
455+
///
456+
/// assert_eq!(2, 1.max(2));
457+
/// assert_eq!(2, 2.max(2));
458+
/// ```
459+
#[unstable(feature = "ord_max_min", issue = "25663")]
460+
fn max(self, other: Self) -> Self
461+
where Self: Sized {
462+
if other >= self { other } else { self }
463+
}
464+
465+
/// Compares and returns the minimum of two values.
466+
///
467+
/// Returns the first argument if the comparison determines them to be equal.
468+
///
469+
/// # Examples
470+
///
471+
/// ```
472+
/// #![feature(ord_max_min)]
473+
///
474+
/// assert_eq!(1, 1.min(2));
475+
/// assert_eq!(2, 2.min(2));
476+
/// ```
477+
#[unstable(feature = "ord_max_min", issue = "25663")]
478+
fn min(self, other: Self) -> Self
479+
where Self: Sized {
480+
if self <= other { self } else { other }
481+
}
446482
}
447483

448484
#[stable(feature = "rust1", since = "1.0.0")]
@@ -678,6 +714,8 @@ pub trait PartialOrd<Rhs: ?Sized = Self>: PartialEq<Rhs> {
678714
///
679715
/// Returns the first argument if the comparison determines them to be equal.
680716
///
717+
/// Internally uses an alias to `Ord::min`.
718+
///
681719
/// # Examples
682720
///
683721
/// ```
@@ -689,13 +727,15 @@ pub trait PartialOrd<Rhs: ?Sized = Self>: PartialEq<Rhs> {
689727
#[inline]
690728
#[stable(feature = "rust1", since = "1.0.0")]
691729
pub fn min<T: Ord>(v1: T, v2: T) -> T {
692-
if v1 <= v2 { v1 } else { v2 }
730+
v1.min(v2)
693731
}
694732

695733
/// Compares and returns the maximum of two values.
696734
///
697735
/// Returns the second argument if the comparison determines them to be equal.
698736
///
737+
/// Internally uses an alias to `Ord::max`.
738+
///
699739
/// # Examples
700740
///
701741
/// ```
@@ -707,7 +747,7 @@ pub fn min<T: Ord>(v1: T, v2: T) -> T {
707747
#[inline]
708748
#[stable(feature = "rust1", since = "1.0.0")]
709749
pub fn max<T: Ord>(v1: T, v2: T) -> T {
710-
if v2 >= v1 { v2 } else { v1 }
750+
v1.max(v2)
711751
}
712752

713753
// Implementation of PartialEq, Eq, PartialOrd and Ord for primitive types

src/libcore/tests/cmp.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,16 @@ fn test_mut_int_totalord() {
2828
assert_eq!((&mut 12).cmp(&&mut -5), Greater);
2929
}
3030

31+
#[test]
32+
fn test_ord_max_min() {
33+
assert_eq!(1.max(2), 2);
34+
assert_eq!(2.max(1), 2);
35+
assert_eq!(1.min(2), 1);
36+
assert_eq!(2.min(1), 1);
37+
assert_eq!(1.max(1), 1);
38+
assert_eq!(1.min(1), 1);
39+
}
40+
3141
#[test]
3242
fn test_ordering_reverse() {
3343
assert_eq!(Less.reverse(), Greater);

src/libcore/tests/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#![feature(iter_rfind)]
2727
#![feature(libc)]
2828
#![feature(nonzero)]
29+
#![feature(ord_max_min)]
2930
#![feature(rand)]
3031
#![feature(raw)]
3132
#![feature(sip_hash_13)]

0 commit comments

Comments
 (0)