Skip to content

Commit

Permalink
Byte and char key support
Browse files Browse the repository at this point in the history
  • Loading branch information
sfackler committed Jul 11, 2014
1 parent e9644d4 commit 789990e
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 5 deletions.
8 changes: 4 additions & 4 deletions phf/src/lib.rs
Expand Up @@ -35,7 +35,7 @@ pub fn displace(f1: uint, f2: uint, d1: uint, d2: uint) -> uint {

/// An immutable map constructed at compile time.
///
/// Keys may be either string literals or binary string literals.
/// Keys may be string, binary string, byte, or character literals.
///
/// `PhfMap`s may be created with the `phf_map` macro:
///
Expand Down Expand Up @@ -216,7 +216,7 @@ impl<'a, K, V> Iterator<&'a V> for PhfMapValues<'a, K, V> {

/// An immutable set constructed at compile time.
///
/// Values may be either string literals or binary string literals.
/// Values may be string, binary string, byte, or character literals.
///
/// `PhfSet`s may be created with the `phf_set` macro:
///
Expand Down Expand Up @@ -325,7 +325,7 @@ impl<'a, T> Iterator<&'a T> for PhfSetValues<'a, T> {

/// An order-preserving immutable map constructed at compile time.
///
/// Keys may be either string literals or binary string literals.
/// Keys may be string, binary string, byte, or character literals.
///
/// Unlike a `PhfMap`, the order of entries in a `PhfOrderedMap` is guaranteed
/// to be the order the entries were listed in.
Expand Down Expand Up @@ -548,7 +548,7 @@ impl<'a, K, V> ExactSize<&'a V> for PhfOrderedMapValues<'a, K, V> {}

/// An order-preserving immutable set constructed at compile time.
///
/// Values may be either string literals or binary string literals.
/// Values may be string, binary string, byte, or character literals.
///
/// Unlike a `PhfSet`, the order of entries in a `PhfOrderedSet` is guaranteed
/// to be the order the entries were listed in.
Expand Down
20 changes: 20 additions & 0 deletions phf/src/test.rs
Expand Up @@ -124,6 +124,26 @@ mod map {
assert_eq!(Some(&0), map.find(&b"hello"));
assert_eq!(Some(&1), map.find(&b"world"));
}

#[test]
fn test_byte_keys() {
static map: PhfMap<u8, int> = phf_map! {
b'a' => 0,
b'b' => 1,
};
assert_eq!(Some(&0), map.find(&b'a'));
assert_eq!(Some(&1), map.find(&b'b'));
}

#[test]
fn test_char_keys() {
static map: PhfMap<char, int> = phf_map! {
'a' => 0,
'b' => 1,
};
assert_eq!(Some(&0), map.find(&'a'));
assert_eq!(Some(&1), map.find(&'b'));
}
}

mod set {
Expand Down
8 changes: 7 additions & 1 deletion phf_mac/src/lib.rs
Expand Up @@ -19,7 +19,7 @@ use std::hash::{Hash};
use std::os;
use std::rc::Rc;
use syntax::ast;
use syntax::ast::{TokenTree, LitStr, LitBinary, Expr, ExprVec, ExprLit};
use syntax::ast::{TokenTree, LitStr, LitBinary, LitByte, LitChar, Expr, ExprVec, ExprLit};
use syntax::codemap::Span;
use syntax::ext::base::{DummyResult,
ExtCtxt,
Expand Down Expand Up @@ -48,13 +48,17 @@ pub fn macro_registrar(reg: &mut Registry) {
enum Key {
KeyStr(InternedString),
KeyBinary(Rc<Vec<u8>>),
KeyByte(u8),
KeyChar(char),
}

impl<S: hash::Writer> Hash<S> for Key {
fn hash(&self, state: &mut S) {
match *self {
KeyStr(ref s) => s.get().hash(state),
KeyBinary(ref b) => b.hash(state),
KeyByte(b) => b.hash(state),
KeyChar(c) => c.hash(state),
}
}
}
Expand Down Expand Up @@ -228,6 +232,8 @@ fn parse_key(cx: &mut ExtCtxt, e: &Expr) -> Option<Key> {
match lit.node {
LitStr(ref s, _) => Some(KeyStr(s.clone())),
LitBinary(ref b) => Some(KeyBinary(b.clone())),
LitByte(b) => Some(KeyByte(b)),
LitChar(c) => Some(KeyChar(c)),
_ => {
cx.span_err(e.span, "unsupported literal type");
None
Expand Down

0 comments on commit 789990e

Please sign in to comment.