Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ jobs:
strategy:
matrix:
include:
- rust: 1.6.0 # MSRV
- rust: 1.28.0 # MSRV
- rust: stable
- rust: beta
- rust: nightly
Expand Down
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "equivalent"
version = "1.0.1"
rust-version = "1.6"
version = "1.1.0"
rust-version = "1.28"
license = "Apache-2.0 OR MIT"
description = "Traits for key comparison in maps."
repository = "https://github.com/cuviper/equivalent"
Expand Down
51 changes: 45 additions & 6 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,20 @@
//! assert_eq!(q1.compare(&key), Ordering::Equal);
//! assert_eq!(q2.compare(&key), Ordering::Less);
//! assert_eq!(q3.compare(&key), Ordering::Greater);
//!
//! // You cannot use `Comparable` with the `RangeBounds::contains` method:
//! // assert!((q1..q3).contains(&key));
//!
//! // But you can use the `ComparableRangeBounds::compare_contains` method:
//! assert!((q1..q3).compare_contains(&key));
//! }
//! ```

#![no_std]

use core::borrow::Borrow;
use core::cmp::Ordering;
use core::ops::{Bound, RangeBounds};

/// Key equivalence trait.
///
Expand All @@ -79,10 +86,10 @@ pub trait Equivalent<K: ?Sized> {
fn equivalent(&self, key: &K) -> bool;
}

impl<Q: ?Sized, K: ?Sized> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
where
Q: Eq,
K: Borrow<Q>,
Q: ?Sized + Eq,
K: ?Sized + Borrow<Q>,
{
#[inline]
fn equivalent(&self, key: &K) -> bool {
Expand All @@ -101,13 +108,45 @@ pub trait Comparable<K: ?Sized>: Equivalent<K> {
fn compare(&self, key: &K) -> Ordering;
}

impl<Q: ?Sized, K: ?Sized> Comparable<K> for Q
impl<Q, K> Comparable<K> for Q
where
Q: Ord,
K: Borrow<Q>,
Q: ?Sized + Ord,
K: ?Sized + Borrow<Q>,
{
#[inline]
fn compare(&self, key: &K) -> Ordering {
Ord::cmp(self, key.borrow())
}
}

/// `ComparableRangeBounds` is implemented as an extention to `RangeBounds` to
/// allow for comparison of items with range bounds.
pub trait ComparableRangeBounds<Q: ?Sized>: RangeBounds<Q> {
/// Returns `true` if `item` is contained in the range.
///
/// # Examples
///
/// See the [crate-level documentation](crate).
fn compare_contains<K>(&self, item: &K) -> bool
where
Q: Comparable<K>,
K: ?Sized,
{
(match self.start_bound() {
Bound::Included(start) => start.compare(item) != Ordering::Greater,
Bound::Excluded(start) => start.compare(item) == Ordering::Less,
Bound::Unbounded => true,
}) && (match self.end_bound() {
Bound::Included(end) => end.compare(item) != Ordering::Less,
Bound::Excluded(end) => end.compare(item) == Ordering::Greater,
Bound::Unbounded => true,
})
}
}

impl<R, Q> ComparableRangeBounds<Q> for R
where
R: ?Sized + RangeBounds<Q>,
Q: ?Sized,
{
}
Loading