about summary refs log tree commit diff
path: root/src/libcore/ops
diff options
context:
space:
mode:
authorScott McMurray <scottmcm@users.noreply.github.com>2018-02-09 01:47:18 -0800
committerScott McMurray <scottmcm@users.noreply.github.com>2018-02-09 01:47:18 -0800
commit4f8049a2b00c46cb1ac77cabaaf716895f185afe (patch)
tree9d04efd20feb079c5cc6e1a5182535109a8ca47c /src/libcore/ops
parentafa8acce251cda7ab1548640fdb769139a45f839 (diff)
Add Range[Inclusive]::is_empty
During the RFC, it was discussed that figuring out whether a range is empty was subtle, and thus there should be a clear and obvious way to do it.  It can't just be ExactSizeIterator::is_empty (also unstable) because not all ranges are ExactSize -- not even Range<i32> or RangeInclusive<usize>.
Diffstat (limited to 'src/libcore/ops')
-rw-r--r--src/libcore/ops/range.rs36
1 files changed, 34 insertions, 2 deletions
diff --git a/src/libcore/ops/range.rs b/src/libcore/ops/range.rs
index 3f573f7c7eb..102e08362cb 100644
--- a/src/libcore/ops/range.rs
+++ b/src/libcore/ops/range.rs
@@ -92,7 +92,6 @@ impl<Idx: fmt::Debug> fmt::Debug for Range<Idx> {
     }
 }
 
-#[unstable(feature = "range_contains", reason = "recently added as per RFC", issue = "32311")]
 impl<Idx: PartialOrd<Idx>> Range<Idx> {
     /// Returns `true` if `item` is contained in the range.
     ///
@@ -109,9 +108,26 @@ impl<Idx: PartialOrd<Idx>> Range<Idx> {
     /// assert!(!(3..3).contains(3));
     /// assert!(!(3..2).contains(3));
     /// ```
+    #[unstable(feature = "range_contains", reason = "recently added as per RFC", issue = "32311")]
     pub fn contains(&self, item: Idx) -> bool {
         (self.start <= item) && (item < self.end)
     }
+
+    /// Returns `true` if the range contains no items.
+    ///
+    /// # Examples
+    ///
+    /// ```
+    /// #![feature(range_is_empty)]
+    ///
+    /// assert!(!(3..5).is_empty());
+    /// assert!( (3..3).is_empty());
+    /// assert!( (3..2).is_empty());
+    /// ```
+    #[unstable(feature = "range_is_empty", reason = "recently added", issue = "123456789")]
+    pub fn is_empty(&self) -> bool {
+        !(self.start < self.end)
+    }
 }
 
 /// A range only bounded inclusively below (`start..`).
@@ -280,7 +296,6 @@ impl<Idx: fmt::Debug> fmt::Debug for RangeInclusive<Idx> {
     }
 }
 
-#[unstable(feature = "range_contains", reason = "recently added as per RFC", issue = "32311")]
 impl<Idx: PartialOrd<Idx>> RangeInclusive<Idx> {
     /// Returns `true` if `item` is contained in the range.
     ///
@@ -298,9 +313,26 @@ impl<Idx: PartialOrd<Idx>> RangeInclusive<Idx> {
     /// assert!( (3..=3).contains(3));
     /// assert!(!(3..=2).contains(3));
     /// ```
+    #[unstable(feature = "range_contains", reason = "recently added as per RFC", issue = "32311")]
     pub fn contains(&self, item: Idx) -> bool {
         self.start <= item && item <= self.end
     }
+
+    /// Returns `true` if the range contains no items.
+    ///
+    /// # Examples
+    ///
+    /// ```
+    /// #![feature(range_is_empty,inclusive_range_syntax)]
+    ///
+    /// assert!(!(3..=5).is_empty());
+    /// assert!(!(3..=3).is_empty());
+    /// assert!( (3..=2).is_empty());
+    /// ```
+    #[unstable(feature = "range_is_empty", reason = "recently added", issue = "123456789")]
+    pub fn is_empty(&self) -> bool {
+        !(self.start <= self.end)
+    }
 }
 
 /// A range only bounded inclusively above (`..=end`).