From 789990ede8def8c333a305437899a953ed6f9a62 Mon Sep 17 00:00:00 2001 From: Steven Fackler Date: Thu, 10 Jul 2014 21:59:02 -0700 Subject: [PATCH] Byte and char key support --- phf/src/lib.rs | 8 ++++---- phf/src/test.rs | 20 ++++++++++++++++++++ phf_mac/src/lib.rs | 8 +++++++- 3 files changed, 31 insertions(+), 5 deletions(-) diff --git a/phf/src/lib.rs b/phf/src/lib.rs index f8b84591..49df08cf 100644 --- a/phf/src/lib.rs +++ b/phf/src/lib.rs @@ -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: /// @@ -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: /// @@ -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. @@ -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. diff --git a/phf/src/test.rs b/phf/src/test.rs index b59c5d46..9850c50c 100644 --- a/phf/src/test.rs +++ b/phf/src/test.rs @@ -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 = 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 = phf_map! { + 'a' => 0, + 'b' => 1, + }; + assert_eq!(Some(&0), map.find(&'a')); + assert_eq!(Some(&1), map.find(&'b')); + } } mod set { diff --git a/phf_mac/src/lib.rs b/phf_mac/src/lib.rs index 81c5cac0..c35b9c8f 100644 --- a/phf_mac/src/lib.rs +++ b/phf_mac/src/lib.rs @@ -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, @@ -48,6 +48,8 @@ pub fn macro_registrar(reg: &mut Registry) { enum Key { KeyStr(InternedString), KeyBinary(Rc>), + KeyByte(u8), + KeyChar(char), } impl Hash for Key { @@ -55,6 +57,8 @@ impl Hash for Key { 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), } } } @@ -228,6 +232,8 @@ fn parse_key(cx: &mut ExtCtxt, e: &Expr) -> Option { 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