diff options
| author | bors <bors@rust-lang.org> | 2017-06-14 00:40:10 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2017-06-14 00:40:10 +0000 |
| commit | e40ef964fe491b19c22dfb8dd36d1eab14223c36 (patch) | |
| tree | 03f28fa7e4ab70baca8217f633446e0811f3a32b /src/libcore/cmp.rs | |
| parent | 03abb1bd70ac56e4aba0684bab819892a0157843 (diff) | |
| parent | 9242f22666e44d525e57a6f4ade91be450f0ee2e (diff) | |
Auto merge of #42644 - frewsxcv:rollup, r=frewsxcv
Rollup of 6 pull requests - Successful merges: #42408, #42428, #42496, #42597, #42636, #42638 - Failed merges: #42612
Diffstat (limited to 'src/libcore/cmp.rs')
| -rw-r--r-- | src/libcore/cmp.rs | 44 |
1 files changed, 42 insertions, 2 deletions
diff --git a/src/libcore/cmp.rs b/src/libcore/cmp.rs index 661cf73c7f3..6f35d0417f1 100644 --- a/src/libcore/cmp.rs +++ b/src/libcore/cmp.rs @@ -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")] @@ -678,6 +714,8 @@ pub trait PartialOrd<Rhs: ?Sized = Self>: PartialEq<Rhs> { /// /// Returns the first argument if the comparison determines them to be equal. /// +/// Internally uses an alias to `Ord::min`. +/// /// # Examples /// /// ``` @@ -689,13 +727,15 @@ pub trait PartialOrd<Rhs: ?Sized = Self>: PartialEq<Rhs> { #[inline] #[stable(feature = "rust1", since = "1.0.0")] pub fn min<T: Ord>(v1: T, v2: T) -> T { - if v1 <= v2 { v1 } else { v2 } + v1.min(v2) } /// Compares and returns the maximum of two values. /// /// Returns the second argument if the comparison determines them to be equal. /// +/// Internally uses an alias to `Ord::max`. +/// /// # Examples /// /// ``` @@ -707,7 +747,7 @@ pub fn min<T: Ord>(v1: T, v2: T) -> T { #[inline] #[stable(feature = "rust1", since = "1.0.0")] pub fn max<T: Ord>(v1: T, v2: T) -> T { - if v2 >= v1 { v2 } else { v1 } + v1.max(v2) } // Implementation of PartialEq, Eq, PartialOrd and Ord for primitive types |
