Include is_empty() in PartialEq and Hash.
When the index is not PartialOrd, always treat the range as empty.
This commit is contained in:
parent
b6ea93e464
commit
6e0dd9ec03
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue