-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(tree_index, #162): vendor traits from "equivalent" crate
Make "equivalent" crate optional dependency
- Loading branch information
Showing
9 changed files
with
75 additions
and
10 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,63 @@ | ||
use std::{borrow::Borrow, cmp::Ordering}; | ||
|
||
/// Key equivalence trait. | ||
/// | ||
/// This trait defines the function used to compare the input value with the | ||
/// map keys (or set values) during a lookup operation such as [`HashMap::get`](crate::HashMap::get) | ||
/// or [`HashSet::contains`](crate::HashSet::contains). | ||
/// It is provided with a blanket implementation based on the | ||
/// [`Borrow`](core::borrow::Borrow) trait. | ||
/// | ||
/// # Correctness | ||
/// | ||
/// The implementor **must** hash like `K`, if it is hashable. | ||
/// | ||
/// # Credits | ||
/// | ||
/// Inspired by `equivalent` and `hashbrown` crates | ||
pub trait Equivalent<K: ?Sized> { | ||
/// Compare self to `key` and return `true` if they are equal. | ||
/// | ||
/// # Correctness | ||
/// | ||
/// When this function returns `true`, both `self` and `key` must hash to | ||
/// the same value. | ||
fn equivalent(&self, key: &K) -> bool; | ||
} | ||
|
||
impl<Q: ?Sized, K: ?Sized> Equivalent<K> for Q | ||
where | ||
Q: Eq, | ||
K: Borrow<Q>, | ||
{ | ||
#[inline] | ||
fn equivalent(&self, key: &K) -> bool { | ||
PartialEq::eq(self, key.borrow()) | ||
} | ||
} | ||
|
||
/// Key ordering trait. | ||
/// | ||
/// This trait defines the function used to compare the input value with the [`TreeIndex`](crate::TreeIndex) keys | ||
/// during a lookup operations such as [`TreeIndex::peek`](crate::TreeIndex::peek) and [`TreeIndex::range`](crate::TreeIndex::range) | ||
/// It is provided with a blanket implementation based on the | ||
/// [`Borrow`](core::borrow::Borrow) trait. | ||
/// | ||
/// /// # Credits | ||
/// | ||
/// Inspired by `equivalent` crate | ||
pub trait Comparable<K: ?Sized>: Equivalent<K> { | ||
/// Compare self to `key` and return their ordering. | ||
fn compare(&self, key: &K) -> Ordering; | ||
} | ||
|
||
impl<Q: ?Sized, K: ?Sized> Comparable<K> for Q | ||
where | ||
Q: Ord, | ||
K: Borrow<Q>, | ||
{ | ||
#[inline] | ||
fn compare(&self, key: &K) -> Ordering { | ||
Ord::cmp(self, key.borrow()) | ||
} | ||
} |
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
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