New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Abstract key lookup trait, so that it can be extended #10
Merged
Merged
Changes from all commits
Commits
File filter...
Filter file types
Jump to…
Jump to file or symbol
Failed to load files and symbols.
Diff settings
| @@ -0,0 +1,27 @@ | |||
|
|
|||
| use std::borrow::Borrow; | |||
|
|
|||
| /// Key equivalence trait. | |||
| /// | |||
| /// This trait allows hash table lookup to be customized. | |||
| /// It has one blanket implementation that uses the regular `Borrow` solution, | |||
| /// just like `HashMap` and `BTreeMap` do, so that you can pass `&str` to lookup | |||
| /// into a map with `String` keys and so on. | |||
| /// | |||
| /// # Contract | |||
| /// | |||
| /// The implementor must hash like `K`, if applicable. | |||
| pub trait Equivalent<K> { | |||
| /// Compare self to `key` and return `true` if they are equal. | |||
| fn equivalent(&self, key: &K) -> bool; | |||
| } | |||
|
|
|||
| impl<Q: ?Sized, K> Equivalent<K> for Q | |||
| where Q: Eq, | |||
| K: Borrow<Q>, | |||
| { | |||
| #[inline] | |||
| fn equivalent(&self, key: &K) -> bool { | |||
| *self == *key.borrow() | |||
| } | |||
| } | |||
48
src/lib.rs
| @@ -0,0 +1,42 @@ | |||
|
|
|||
| #[macro_use] extern crate ordermap; | |||
|
|
|||
| use ordermap::Equivalent; | |||
|
|
|||
| use std::hash::Hash; | |||
|
|
|||
| #[derive(Debug, Hash)] | |||
| pub struct Pair<A, B>(pub A, pub B); | |||
|
|
|||
| impl<A, B, C, D> PartialEq<(A, B)> for Pair<C, D> | |||
| where C: PartialEq<A>, | |||
| D: PartialEq<B>, | |||
| { | |||
| fn eq(&self, rhs: &(A, B)) -> bool { | |||
| self.0 == rhs.0 && | |||
| self.1 == rhs.1 && | |||
| true | |||
| } | |||
| } | |||
|
|
|||
| impl<A, B, X> Equivalent<X> for Pair<A, B> | |||
| where Pair<A, B>: PartialEq<X>, | |||
| A: Hash + Eq, | |||
| B: Hash + Eq, | |||
| { | |||
| fn equivalent(&self, other: &X) -> bool { | |||
| *self == *other | |||
| } | |||
| } | |||
|
|
|||
| #[test] | |||
| fn test_lookup() { | |||
| let s = String::from; | |||
| let map = ordermap! { | |||
| (s("a"), s("b")) => 1, | |||
| (s("a"), s("x")) => 2, | |||
| }; | |||
|
|
|||
| assert!(map.contains_key(&Pair("a", "b"))); | |||
| assert!(!map.contains_key(&Pair("b", "a"))); | |||
| } | |||
ProTip!
Use n and p to navigate between commits in a pull request.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.