mirror of
https://github.com/rust-lang/rust.git
synced 2024-11-27 09:14:20 +00:00
Add max and min default fns to Ord trait
Pursuant to issue #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.
This commit is contained in:
parent
76242aebb9
commit
2cadc32b66
@ -165,6 +165,7 @@
|
||||
- [n16](library-features/n16.md)
|
||||
- [never_type_impls](library-features/never-type-impls.md)
|
||||
- [nonzero](library-features/nonzero.md)
|
||||
- [ord_max_min](library-features/ord-max-min.md)
|
||||
- [offset_to](library-features/offset-to.md)
|
||||
- [once_poison](library-features/once-poison.md)
|
||||
- [oom](library-features/oom.md)
|
||||
|
@ -0,0 +1,7 @@
|
||||
# `ord-max-min`
|
||||
|
||||
The tracking issue for this feature is: [#25663]
|
||||
|
||||
[#25663]: https://github.com/rust-lang/rust/issues/25663
|
||||
|
||||
------------------------
|
@ -443,6 +443,42 @@ pub trait Ord: Eq + PartialOrd<Self> {
|
||||
/// ```
|
||||
#[stable(feature = "rust1", since = "1.0.0")]
|
||||
fn cmp(&self, other: &Self) -> Ordering;
|
||||
|
||||
/// Compares and returns the maximum of two values.
|
||||
///
|
||||
/// Returns the second argument if the comparison determines them to be equal.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```
|
||||
/// #![feature(ord_max_min)]
|
||||
///
|
||||
/// assert_eq!(2, 1.max(2));
|
||||
/// assert_eq!(2, 2.max(2));
|
||||
/// ```
|
||||
#[unstable(feature = "ord_max_min", issue = "25663")]
|
||||
fn max(self, other: Self) -> Self
|
||||
where Self: Sized {
|
||||
if other >= self { other } else { self }
|
||||
}
|
||||
|
||||
/// Compares and returns the minimum of two values.
|
||||
///
|
||||
/// Returns the first argument if the comparison determines them to be equal.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```
|
||||
/// #![feature(ord_max_min)]
|
||||
///
|
||||
/// assert_eq!(1, 1.min(2));
|
||||
/// assert_eq!(2, 2.min(2));
|
||||
/// ```
|
||||
#[unstable(feature = "ord_max_min", issue = "25663")]
|
||||
fn min(self, other: Self) -> Self
|
||||
where Self: Sized {
|
||||
if self <= other { self } else { other }
|
||||
}
|
||||
}
|
||||
|
||||
#[stable(feature = "rust1", since = "1.0.0")]
|
||||
|
@ -28,6 +28,16 @@ fn test_mut_int_totalord() {
|
||||
assert_eq!((&mut 12).cmp(&&mut -5), Greater);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_ord_max_min() {
|
||||
assert_eq!(1.max(2), 2);
|
||||
assert_eq!(2.max(1), 2);
|
||||
assert_eq!(1.min(2), 1);
|
||||
assert_eq!(2.min(1), 1);
|
||||
assert_eq!(1.max(1), 1);
|
||||
assert_eq!(1.min(1), 1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_ordering_reverse() {
|
||||
assert_eq!(Less.reverse(), Greater);
|
||||
|
@ -26,6 +26,7 @@
|
||||
#![feature(iter_rfind)]
|
||||
#![feature(libc)]
|
||||
#![feature(nonzero)]
|
||||
#![feature(ord_max_min)]
|
||||
#![feature(rand)]
|
||||
#![feature(raw)]
|
||||
#![feature(sip_hash_13)]
|
||||
|
Loading…
Reference in New Issue
Block a user