forked from rust-lang/miri
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request rust-lang#10 from bluss/lookup-trait
Abstract key lookup trait, so that it can be extended
- Loading branch information
Showing
4 changed files
with
88 additions
and
31 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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() | ||
} | ||
} |
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,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"))); | ||
} |