about summary refs log tree commit diff
path: root/src/libcore
diff options
context:
space:
mode:
authorTill Hoeppner <till@hoeppner.ws>2014-09-23 20:01:59 +0200
committerTill Hoeppner <till@hoeppner.ws>2014-09-25 14:12:03 +0200
commit29c2d3df5269a6538b23d79f56b63903e21e69f4 (patch)
tree622f5751841d909f24cc8318eba666c24947f2c1 /src/libcore
parentd299bafb31a7c0528e690e48ec6d5591f1eb0bac (diff)
Add partial_min/max to libcore/cmp
Add partial_min/max to libcore/cmp

Match against None and mark as experimental

Shortened documentation.

Removed whitespace
Diffstat (limited to 'src/libcore')
-rw-r--r--src/libcore/cmp.rs28
1 files changed, 27 insertions, 1 deletions
diff --git a/src/libcore/cmp.rs b/src/libcore/cmp.rs
index b90c6daf9eb..a567eaa57d5 100644
--- a/src/libcore/cmp.rs
+++ b/src/libcore/cmp.rs
@@ -39,7 +39,7 @@
 
 #![stable]
 
-use option::{Option, Some};
+use option::{Option, Some, None};
 
 /// Trait for values that can be compared for equality and inequality.
 ///
@@ -268,6 +268,32 @@ pub fn max<T: Ord>(v1: T, v2: T) -> T {
     if v1 > v2 { v1 } else { v2 }
 }
 
+/// Compare and return the minimum of two values if there is one.
+///
+/// Returns the first argument if the comparison determines them to be equal.
+#[inline]
+#[experimental]
+pub fn partial_min<T: PartialOrd>(v1: T, v2: T) -> Option<T> {
+    match v1.partial_cmp(&v2) {
+        Some(Less) | Some(Equal) => Some(v1),
+        Some(Greater) => Some(v2),
+        None => None
+    }
+}
+
+/// Compare and return the maximum of two values if there is one.
+///
+/// Returns the first argument if the comparison determines them to be equal.
+#[inline]
+#[experimental]
+pub fn partial_max<T: PartialOrd>(v1: T, v2: T) -> Option<T> {
+    match v1.partial_cmp(&v2) {
+        Some(Less) => Some(v2),
+        Some(Equal) | Some(Greater) => Some(v1),
+        None => None
+    }
+}
+
 // Implementation of PartialEq, Eq, PartialOrd and Ord for primitive types
 mod impls {
     use cmp::{PartialOrd, Ord, PartialEq, Eq, Ordering,