Skip to content

Commit 08dd000

Browse files
committed
Support RangeBounds for T: Comparable
1 parent 8a56bf0 commit 08dd000

File tree

2 files changed

+39
-2
lines changed

2 files changed

+39
-2
lines changed

Cargo.toml

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[package]
22
name = "equivalent"
3-
version = "1.0.1"
4-
rust-version = "1.6"
3+
version = "1.1.0"
4+
rust-version = "1.28"
55
license = "Apache-2.0 OR MIT"
66
description = "Traits for key comparison in maps."
77
repository = "https://github.com/cuviper/equivalent"

src/lib.rs

+37
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@
6363

6464
use core::borrow::Borrow;
6565
use core::cmp::Ordering;
66+
use core::ops::{Bound, RangeBounds};
6667

6768
/// Key equivalence trait.
6869
///
@@ -111,3 +112,39 @@ where
111112
Ord::cmp(self, key.borrow())
112113
}
113114
}
115+
116+
/// `ComparableRangeBounds` is implemented as an extention to `RangeBounds` to
117+
/// allow for comparison of items with range bounds.
118+
pub trait ComparableRangeBounds<T>: RangeBounds<T> {
119+
/// Returns `true` if `item` is contained in the range.
120+
///
121+
/// # Examples
122+
///
123+
/// ```
124+
/// assert!( (3..5).comparable_contains(&4));
125+
/// assert!(!(3..5).comparable_contains(&2));
126+
///
127+
/// assert!( (0.0..1.0).comparable_contains(&0.5));
128+
/// assert!(!(0.0..1.0).comparable_contains(&f32::NAN));
129+
/// assert!(!(0.0..f32::NAN).comparable_contains(&0.5));
130+
/// assert!(!(f32::NAN..1.0).comparable_contains(&0.5));
131+
/// ```
132+
fn comparable_contains<U>(&self, item: &U) -> bool
133+
where
134+
U: ?Sized + Comparable<T>,
135+
{
136+
(match self.start_bound() {
137+
Bound::Included(start) => {
138+
matches!(item.compare(start), Ordering::Greater | Ordering::Equal)
139+
}
140+
Bound::Excluded(start) => item.compare(start) == Ordering::Greater,
141+
Bound::Unbounded => true,
142+
}) && (match self.end_bound() {
143+
Bound::Included(end) => matches!(item.compare(end), Ordering::Less | Ordering::Equal),
144+
Bound::Excluded(end) => item.compare(end) == Ordering::Less,
145+
Bound::Unbounded => true,
146+
})
147+
}
148+
}
149+
150+
impl<R, T> ComparableRangeBounds<T> for R where R: RangeBounds<T> {}

0 commit comments

Comments
 (0)