Skip to content

Commit 2cadc32

Browse files
committedJun 7, 2017
Add max and min default fns to Ord trait
Pursuant to issue rust-lang#25663, this commit adds the max and min functions to the Ord trait, enabling items that implement Ord to use UFCS (ex. 1.max(2)) instead of the longer std::cmp::max(1,2) format.
1 parent 76242ae commit 2cadc32

File tree

5 files changed

+55
-0
lines changed

5 files changed

+55
-0
lines changed
 

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

+1
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)
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

+36
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")]

‎src/libcore/tests/cmp.rs

+10
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

+1
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)
Please sign in to comment.