Include is_empty() in PartialEq and Hash.

When the index is not PartialOrd, always treat the range as empty.
This commit is contained in:
kennytm 2018-06-30 17:13:21 +08:00
parent b6ea93e464
commit 6e0dd9ec03
No known key found for this signature in database
GPG Key ID: FEF6C8051D0E013C
1 changed files with 19 additions and 0 deletions

View File

@ -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);
}
}