From 6e0dd9ec0362af41996cf2d2a0afd520bf873d3a Mon Sep 17 00:00:00 2001 From: kennytm Date: Sat, 30 Jun 2018 17:13:21 +0800 Subject: [PATCH] Include is_empty() in PartialEq and Hash. When the index is not PartialOrd, always treat the range as empty. --- src/libcore/ops/range.rs | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) 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 { // accept non-PartialOrd types, also we want the constructor to be const. } +trait RangeInclusiveEquality: Sized { + fn canonicalized_is_empty(range: &RangeInclusive) -> bool; +} +impl RangeInclusiveEquality for T { + #[inline] + default fn canonicalized_is_empty(range: &RangeInclusive) -> bool { + !range.is_iterating.unwrap_or(false) + } +} +impl RangeInclusiveEquality for T { + #[inline] + fn canonicalized_is_empty(range: &RangeInclusive) -> bool { + range.is_empty() + } +} + #[stable(feature = "inclusive_range", since = "1.26.0")] impl PartialEq for RangeInclusive { #[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 Hash for RangeInclusive { fn hash(&self, state: &mut H) { self.start.hash(state); self.end.hash(state); + RangeInclusiveEquality::canonicalized_is_empty(self).hash(state); } } -- 2.44.0