Skip to content

Commit f70ea69

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

File tree

2 files changed

+42
-2
lines changed

2 files changed

+42
-2
lines changed

Cargo.toml

Lines changed: 2 additions & 2 deletions
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

Lines changed: 40 additions & 0 deletions
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,42 @@ 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: ?Sized>: RangeBounds<T> {
119+
/// Returns `true` if `item` is contained in the range.
120+
///
121+
/// # Examples
122+
///
123+
/// ```rust
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<Q>(&self, item: &Q) -> bool
133+
where
134+
T: Comparable<Q>,
135+
Q: ?Sized,
136+
{
137+
(match self.start_bound() {
138+
Bound::Included(start) => {
139+
matches!(start.compare(item), Ordering::Less | Ordering::Equal)
140+
}
141+
Bound::Excluded(start) => start.compare(item) == Ordering::Less,
142+
Bound::Unbounded => true,
143+
}) && (match self.end_bound() {
144+
Bound::Included(end) => {
145+
matches!(end.compare(item), Ordering::Greater | Ordering::Equal)
146+
}
147+
Bound::Excluded(end) => end.compare(item) == Ordering::Greater,
148+
Bound::Unbounded => true,
149+
})
150+
}
151+
}
152+
153+
impl<R, T: ?Sized> ComparableRangeBounds<T> for R where R: RangeBounds<T> {}

0 commit comments

Comments
 (0)