about summary refs log tree commit diff
diff options
context:
space:
mode:
authorkennytm <kennytm@gmail.com>2018-06-30 17:13:21 +0800
committerkennytm <kennytm@gmail.com>2018-07-13 09:53:37 +0800
commit6e0dd9ec0362af41996cf2d2a0afd520bf873d3a (patch)
tree44d9c77f23d4fc19f95de5016e81febb5330399e
parentb6ea93e46410cccf8d115e57283d1df5968dd0f2 (diff)
downloadrust-6e0dd9ec0362af41996cf2d2a0afd520bf873d3a.tar.gz
rust-6e0dd9ec0362af41996cf2d2a0afd520bf873d3a.zip
Include is_empty() in PartialEq and Hash.
When the index is not PartialOrd, always treat the range as empty.
-rw-r--r--src/libcore/ops/range.rs19
1 files changed, 19 insertions, 0 deletions
diff --git a/src/libcore/ops/range.rs b/src/libcore/ops/range.rs
index 3f9ac8a54bf..0f119789a75 100644
--- a/src/libcore/ops/range.rs
+++ b/src/libcore/ops/range.rs
@@ -341,11 +341,29 @@ pub struct RangeInclusive<Idx> {
     // accept non-PartialOrd types, also we want the constructor to be const.
 }
 
+trait RangeInclusiveEquality: Sized {
+    fn canonicalized_is_empty(range: &RangeInclusive<Self>) -> bool;
+}
+impl<T> RangeInclusiveEquality for T {
+    #[inline]
+    default fn canonicalized_is_empty(range: &RangeInclusive<Self>) -> bool {
+        !range.is_iterating.unwrap_or(false)
+    }
+}
+impl<T: PartialOrd> RangeInclusiveEquality for T {
+    #[inline]
+    fn canonicalized_is_empty(range: &RangeInclusive<Self>) -> bool {
+        range.is_empty()
+    }
+}
+
 #[stable(feature = "inclusive_range", since = "1.26.0")]
 impl<Idx: PartialEq> PartialEq for RangeInclusive<Idx> {
     #[inline]
     fn eq(&self, other: &Self) -> bool {
         self.start == other.start && self.end == other.end
+            && RangeInclusiveEquality::canonicalized_is_empty(self)
+                == RangeInclusiveEquality::canonicalized_is_empty(other)
     }
 }
 
@@ -357,6 +375,7 @@ impl<Idx: Hash> Hash for RangeInclusive<Idx> {
     fn hash<H: Hasher>(&self, state: &mut H) {
         self.start.hash(state);
         self.end.hash(state);
+        RangeInclusiveEquality::canonicalized_is_empty(self).hash(state);
     }
 }