about summary refs log tree commit diff
path: root/src/libcore/cmp.rs
diff options
context:
space:
mode:
authorMarcin Fatyga <marcinf@google.com>2016-10-09 10:46:11 +0200
committerMarcin Fatyga <marcinf@google.com>2016-10-09 12:01:17 +0200
commitd41c91c1fa6633754e7c88ab45224aedd5b5ed92 (patch)
tree157365b97cb1be9d1aa1b45ddbf03c727030dc9a /src/libcore/cmp.rs
parentc6673db58d117d5c554559ae51b4ddf0aae3de00 (diff)
downloadrust-d41c91c1fa6633754e7c88ab45224aedd5b5ed92.tar.gz
rust-d41c91c1fa6633754e7c88ab45224aedd5b5ed92.zip
Add or and or_else for ordering.
Diffstat (limited to 'src/libcore/cmp.rs')
-rw-r--r--src/libcore/cmp.rs70
1 files changed, 70 insertions, 0 deletions
diff --git a/src/libcore/cmp.rs b/src/libcore/cmp.rs
index f990a27e52b..58b3db0ad39 100644
--- a/src/libcore/cmp.rs
+++ b/src/libcore/cmp.rs
@@ -245,6 +245,76 @@ impl Ordering {
             Greater => Less,
         }
     }
+
+    /// Chain two orderings. 
+    /// 
+    /// Returns `self` when it's not `Equal`. Otherwise returns `other`.
+    /// # Examples
+    ///
+    /// ```
+    /// use std::cmp::Ordering;
+    ///
+    /// let result = Ordering::Equal.or(Ordering::Less);
+    /// assert_eq!(result, Ordering::Less);
+    ///
+    /// let result = Ordering::Less.or(Ordering::Equal);
+    /// assert_eq!(result, Ordering::Less);
+    ///
+    /// let result = Ordering::Less.or(Ordering::Greater);
+    /// assert_eq!(result, Ordering::Less);
+    ///
+    /// let result = Ordering::Equal.or(Ordering::Equal);
+    /// assert_eq!(result, Ordering::Equal);
+    ///
+    /// let x = (1, 2, 7);
+    /// let y = (1, 5, 3);
+    /// let result = x.0.cmp(y.0).or(x.1.cmp(y.1)).or(x.2.cmp(y.2));
+    ///
+    /// assert_eq!(result, Ordering::Less);
+    /// ```
+    #[unstable(feature = "ordering_chaining", issue = "37053")]
+    pub fn or(self, other: Ordering) -> Ordering {
+        match self {
+            Equal => other,
+            _ => self,
+        }
+    }
+
+    /// Chain the ordering with given function.
+    /// 
+    /// Returns `self` when it's not `Equal`. Otherwise calls `f` and returns
+    /// the result.
+    /// 
+    /// # Examples
+    ///
+    /// ```
+    /// use std::cmp::Ordering;
+    ///
+    /// let result = Ordering::Equal.or_else(|| Ordering::Less);
+    /// assert_eq!(result, Ordering::Less);
+    ///
+    /// let result = Ordering::Less.or_else(|| Ordering::Equal);
+    /// assert_eq!(result, Ordering::Less);
+    ///
+    /// let result = Ordering::Less.or_else(|| Ordering::Greater);
+    /// assert_eq!(result, Ordering::Less);
+    ///
+    /// let result = Ordering::Equal.or_else(|| Ordering::Equal);
+    /// assert_eq!(result, Ordering::Equal);
+    ///
+    /// let x = (1, 2, 7);
+    /// let y = (1, 5, 3);
+    /// let result = x.0.cmp(&y.0).or_else(|| x.1.cmp(&y.1)).or_else(|| x.2.cmp(&y.2));
+    ///
+    /// assert_eq!(result, Ordering::Less);
+    /// ```
+    #[unstable(feature = "ordering_chaining", issue = "37053")]
+    pub fn or_else<F: FnOnce() -> Ordering>(self, f: F) -> Ordering {
+        match self {
+            Equal => f(),
+            _ => self,
+        }
+    }
 }
 
 /// Trait for types that form a [total order](https://en.wikipedia.org/wiki/Total_order).