Skip to content

Commit

Permalink
Fix up Map definition
Browse files Browse the repository at this point in the history
There's no need to define the key type as &'static str when &'a str will
do and be more flexible
  • Loading branch information
sfackler committed Jan 18, 2014
1 parent 8ca3907 commit 49638ed
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 21 deletions.
16 changes: 6 additions & 10 deletions phf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,17 @@ impl<T> Container for PhfMap<T> {
}
}

impl<T> Map<&'static str, T> for PhfMap<T> {
impl<'a, T> Map<&'a str, T> for PhfMap<T> {
#[inline]
fn find<'a>(&'a self, key: & &'static str) -> Option<&'a T> {
self.find_str(key)
}
}

impl<T> PhfMap<T> {
#[inline]
pub fn find_str<'a, S: Str>(&'a self, key: &S) -> Option<&'a T> {
self.entries.bsearch(|&(val, _)| val.cmp(&key.as_slice())).map(|idx| {
fn find<'a>(&'a self, key: & &str) -> Option<&'a T> {
self.entries.bsearch(|&(val, _)| val.cmp(key)).map(|idx| {
let (_, ref val) = self.entries[idx];
val
})
}
}

impl<T> PhfMap<T> {

#[inline]
pub fn entries<'a>(&'a self) -> PhfMapEntries<'a, T> {
Expand Down
1 change: 1 addition & 0 deletions phf_mac.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ use syntax::parse::token;
use syntax::parse::token::{COMMA, EOF, FAT_ARROW};

#[macro_registrar]
#[doc(hidden)]
pub fn macro_registrar(register: |Name, SyntaxExtension|) {
register(token::intern("phf_map"),
NormalTT(~SyntaxExpanderTT {
Expand Down
11 changes: 0 additions & 11 deletions test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,14 +80,3 @@ fn test_values() {
assert!(hash.contains(&11));
assert_eq!(2, hash.len());
}

#[test]
fn test_find_str() {
static map: PhfMap<int> = phf_map!(
"foo" => 10,
"bar" => 11,
);
assert!(map.find_str(&~"foo") == Some(&10));
assert!(map.find_str(&~"bar") == Some(&11));
assert!(map.find_str(&~"baz") == None);
}

0 comments on commit 49638ed

Please sign in to comment.