Skip to content

Commit 5e3bd09

Browse files
committed
Include is_empty() in PartialEq and Hash.
When the index is not PartialOrd, always treat the range as empty.
1 parent 37fbf68 commit 5e3bd09

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

src/libcore/ops/range.rs

+19
Original file line numberDiff line numberDiff line change
@@ -341,11 +341,29 @@ pub struct RangeInclusive<Idx> {
341341
// accept non-PartialOrd types, also we want the constructor to be const.
342342
}
343343

344+
trait RangeInclusiveEquality: Sized {
345+
fn canonicalized_is_empty(range: &RangeInclusive<Self>) -> bool;
346+
}
347+
impl<T> RangeInclusiveEquality for T {
348+
#[inline]
349+
default fn canonicalized_is_empty(range: &RangeInclusive<Self>) -> bool {
350+
!range.is_iterating.unwrap_or(false)
351+
}
352+
}
353+
impl<T: PartialOrd> RangeInclusiveEquality for T {
354+
#[inline]
355+
fn canonicalized_is_empty(range: &RangeInclusive<Self>) -> bool {
356+
range.is_empty()
357+
}
358+
}
359+
344360
#[stable(feature = "inclusive_range", since = "1.26.0")]
345361
impl<Idx: PartialEq> PartialEq for RangeInclusive<Idx> {
346362
#[inline]
347363
fn eq(&self, other: &Self) -> bool {
348364
self.start == other.start && self.end == other.end
365+
&& RangeInclusiveEquality::canonicalized_is_empty(self)
366+
== RangeInclusiveEquality::canonicalized_is_empty(other)
349367
}
350368
}
351369

@@ -357,6 +375,7 @@ impl<Idx: Hash> Hash for RangeInclusive<Idx> {
357375
fn hash<H: Hasher>(&self, state: &mut H) {
358376
self.start.hash(state);
359377
self.end.hash(state);
378+
RangeInclusiveEquality::canonicalized_is_empty(self).hash(state);
360379
}
361380
}
362381

0 commit comments

Comments
 (0)