]> git.lizzy.rs Git - rust.git/commitdiff
Rollup merge of #42496 - Razaekel:feature/integer_max-min, r=BurntSushi
authorCorey Farwell <coreyf@rwell.org>
Tue, 13 Jun 2017 21:15:00 +0000 (17:15 -0400)
committerGitHub <noreply@github.com>
Tue, 13 Jun 2017 21:15:00 +0000 (17:15 -0400)
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

src/doc/unstable-book/src/SUMMARY.md
src/doc/unstable-book/src/library-features/ord-max-min.md [new file with mode: 0644]
src/libcore/cmp.rs
src/libcore/tests/cmp.rs
src/libcore/tests/lib.rs

index 2b3ef338fad5860605a9b04b64a9e607e242136b..b278b5dbb2ca6dfdd5b5763a18c458ca452c5836 100644 (file)
     - [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)
diff --git a/src/doc/unstable-book/src/library-features/ord-max-min.md b/src/doc/unstable-book/src/library-features/ord-max-min.md
new file mode 100644 (file)
index 0000000..564cd1a
--- /dev/null
@@ -0,0 +1,7 @@
+# `ord-max-min`
+
+The tracking issue for this feature is: [#25663]
+
+[#25663]: https://github.com/rust-lang/rust/issues/25663
+
+------------------------
index 661cf73c7f30e3f36daf2da8d55eea9399ca1ed1..6f35d0417f18b1986f6db9042df62c09b625fa2b 100644 (file)
@@ -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 @@ fn ge(&self, other: &Rhs) -> bool {
 ///
 /// Returns the first argument if the comparison determines them to be equal.
 ///
+/// Internally uses an alias to `Ord::min`.
+///
 /// # Examples
 ///
 /// ```
@@ -689,13 +727,15 @@ fn ge(&self, other: &Rhs) -> bool {
 #[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
index e3c65ad8b33c019378ad6e53cbcbdb3295f0ec71..8c5179f59932bba13ef4636260099ae16aabed1c 100644 (file)
@@ -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);
index 505e51fa80b333b651eb10c089b1378272620dc0..77a9307f43ecf97375ea03c0cf4d1b85ccd8bf19 100644 (file)
@@ -26,6 +26,7 @@
 #![feature(iter_rfind)]
 #![feature(libc)]
 #![feature(nonzero)]
+#![feature(ord_max_min)]
 #![feature(rand)]
 #![feature(raw)]
 #![feature(sip_hash_13)]