-
Notifications
You must be signed in to change notification settings - Fork 472
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
254 additions
and
186 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
use core::{borrow::Borrow, cmp::Ordering}; | ||
|
||
/// Key equivalence trait. | ||
/// | ||
/// This trait allows hash table lookup to be customized. It has one blanket | ||
/// implementation that uses the regular solution with `Borrow` and `Eq`, just | ||
/// like `HashMap` does, so that you can pass `&str` to lookup into a map with | ||
/// `String` keys and so on. | ||
/// | ||
/// # Contract | ||
/// | ||
/// The implementor **must** hash like `Q`, if it is hashable. | ||
pub trait Equivalent<Q: ?Sized> { | ||
/// Compare self to `key` and return `true` if they are equal. | ||
fn equivalent(&self, key: &Q) -> bool; | ||
} | ||
|
||
impl<K: ?Sized, Q: ?Sized> Equivalent<Q> for K | ||
where | ||
K: Borrow<Q>, | ||
Q: Eq, | ||
{ | ||
#[inline] | ||
fn equivalent(&self, key: &Q) -> bool { | ||
PartialEq::eq(self.borrow(), key) | ||
} | ||
} | ||
|
||
/// Key ordering trait. | ||
/// | ||
/// This trait allows ordered map lookup to be customized. It has one blanket | ||
/// implementation that uses the regular solution with `Borrow` and `Ord`, just | ||
/// like `BTreeMap` does, so that you can pass `&str` to lookup into a map with | ||
/// `String` keys and so on. | ||
pub trait Comparable<Q: ?Sized>: Equivalent<Q> { | ||
/// Compare self to `key` and return their ordering. | ||
fn compare(&self, key: &Q) -> Ordering; | ||
} | ||
|
||
impl<K: ?Sized, Q: ?Sized> Comparable<Q> for K | ||
where | ||
K: Borrow<Q>, | ||
Q: Ord, | ||
{ | ||
#[inline] | ||
fn compare(&self, key: &Q) -> Ordering { | ||
Ord::cmp(self.borrow(), key) | ||
} | ||
} |
Oops, something went wrong.