Skip to content

Commit

Permalink
Clean up Key enum
Browse files Browse the repository at this point in the history
  • Loading branch information
sfackler committed Nov 17, 2014
1 parent 6776189 commit 4499d0b
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 61 deletions.
42 changes: 20 additions & 22 deletions phf_mac/src/lib.rs
Expand Up @@ -24,9 +24,7 @@ use syntax::parse::token::{InternedString, Comma, Eof, FatArrow};
use syntax::print::pprust;
use rustc::plugin::Registry;

use util::Entry;
use util::Key::{mod, KeyStr, KeyBinary, KeyChar, KeyU8, KeyI8, KeyU16};
use util::Key::{KeyI16, KeyU32, KeyI32, KeyU64, KeyI64, KeyBool};
use util::{Entry, Key};
use util::{generate_hash, create_map, create_set, create_ordered_map, create_ordered_set};

#[path="../../shared/mod.rs"]
Expand Down Expand Up @@ -111,7 +109,7 @@ fn parse_map(cx: &mut ExtCtxt, tts: &[TokenTree]) -> Option<Vec<Entry>> {
let key = cx.expander().fold_expr(parser.parse_expr());
let key_contents = parse_key(cx, &*key).unwrap_or_else(|| {
bad = true;
KeyStr(InternedString::new(""))
Key::Str(InternedString::new(""))
});

if !parser.eat(&FatArrow) {
Expand Down Expand Up @@ -150,7 +148,7 @@ fn parse_set(cx: &mut ExtCtxt, tts: &[TokenTree]) -> Option<Vec<Entry>> {
let key = cx.expander().fold_expr(parser.parse_expr());
let key_contents = parse_key(cx, &*key).unwrap_or_else(|| {
bad = true;
KeyStr(InternedString::new(""))
Key::Str(InternedString::new(""))
});

entries.push(Entry {
Expand All @@ -176,23 +174,23 @@ fn parse_key(cx: &mut ExtCtxt, e: &Expr) -> Option<Key> {
match e.node {
ExprLit(ref lit) => {
match lit.node {
ast::LitStr(ref s, _) => Some(KeyStr(s.clone())),
ast::LitBinary(ref b) => Some(KeyBinary(b.clone())),
ast::LitByte(b) => Some(KeyU8(b)),
ast::LitChar(c) => Some(KeyChar(c)),
ast::LitInt(i, ast::SignedIntLit(ast::TyI8, ast::Plus)) => Some(KeyI8(i as i8)),
ast::LitInt(i, ast::SignedIntLit(ast::TyI8, ast::Minus)) => Some(KeyI8(-(i as i8))),
ast::LitInt(i, ast::SignedIntLit(ast::TyI16, ast::Plus)) => Some(KeyI16(i as i16)),
ast::LitInt(i, ast::SignedIntLit(ast::TyI16, ast::Minus)) => Some(KeyI16(-(i as i16))),
ast::LitInt(i, ast::SignedIntLit(ast::TyI32, ast::Plus)) => Some(KeyI32(i as i32)),
ast::LitInt(i, ast::SignedIntLit(ast::TyI32, ast::Minus)) => Some(KeyI32(-(i as i32))),
ast::LitInt(i, ast::SignedIntLit(ast::TyI64, ast::Plus)) => Some(KeyI64(i as i64)),
ast::LitInt(i, ast::SignedIntLit(ast::TyI64, ast::Minus)) => Some(KeyI64(-(i as i64))),
ast::LitInt(i, ast::UnsignedIntLit(ast::TyU8)) => Some(KeyU8(i as u8)),
ast::LitInt(i, ast::UnsignedIntLit(ast::TyU16)) => Some(KeyU16(i as u16)),
ast::LitInt(i, ast::UnsignedIntLit(ast::TyU32)) => Some(KeyU32(i as u32)),
ast::LitInt(i, ast::UnsignedIntLit(ast::TyU64)) => Some(KeyU64(i as u64)),
ast::LitBool(b) => Some(KeyBool(b)),
ast::LitStr(ref s, _) => Some(Key::Str(s.clone())),
ast::LitBinary(ref b) => Some(Key::Binary(b.clone())),
ast::LitByte(b) => Some(Key::U8(b)),
ast::LitChar(c) => Some(Key::Char(c)),
ast::LitInt(i, ast::SignedIntLit(ast::TyI8, ast::Plus)) => Some(Key::I8(i as i8)),
ast::LitInt(i, ast::SignedIntLit(ast::TyI8, ast::Minus)) => Some(Key::I8(-(i as i8))),
ast::LitInt(i, ast::SignedIntLit(ast::TyI16, ast::Plus)) => Some(Key::I16(i as i16)),
ast::LitInt(i, ast::SignedIntLit(ast::TyI16, ast::Minus)) => Some(Key::I16(-(i as i16))),
ast::LitInt(i, ast::SignedIntLit(ast::TyI32, ast::Plus)) => Some(Key::I32(i as i32)),
ast::LitInt(i, ast::SignedIntLit(ast::TyI32, ast::Minus)) => Some(Key::I32(-(i as i32))),
ast::LitInt(i, ast::SignedIntLit(ast::TyI64, ast::Plus)) => Some(Key::I64(i as i64)),
ast::LitInt(i, ast::SignedIntLit(ast::TyI64, ast::Minus)) => Some(Key::I64(-(i as i64))),
ast::LitInt(i, ast::UnsignedIntLit(ast::TyU8)) => Some(Key::U8(i as u8)),
ast::LitInt(i, ast::UnsignedIntLit(ast::TyU16)) => Some(Key::U16(i as u16)),
ast::LitInt(i, ast::UnsignedIntLit(ast::TyU32)) => Some(Key::U32(i as u32)),
ast::LitInt(i, ast::UnsignedIntLit(ast::TyU64)) => Some(Key::U64(i as u64)),
ast::LitBool(b) => Some(Key::Bool(b)),
_ => {
cx.span_err(e.span, "unsupported literal type");
None
Expand Down
75 changes: 36 additions & 39 deletions phf_mac/src/util.rs
Expand Up @@ -17,63 +17,60 @@ use shared::PhfHash;

use time;

use self::Key::{KeyStr, KeyBinary, KeyChar, KeyU8, KeyI8, KeyU16};
use self::Key::{KeyI16, KeyU32, KeyI32, KeyU64, KeyI64, KeyBool};

static DEFAULT_LAMBDA: uint = 5;

static FIXED_SEED: [u32, ..4] = [3141592653, 589793238, 462643383, 2795028841];

#[deriving(PartialEq, Eq, Clone)]
pub enum Key {
KeyStr(InternedString),
KeyBinary(Rc<Vec<u8>>),
KeyChar(char),
KeyU8(u8),
KeyI8(i8),
KeyU16(u16),
KeyI16(i16),
KeyU32(u32),
KeyI32(i32),
KeyU64(u64),
KeyI64(i64),
KeyBool(bool),
Str(InternedString),
Binary(Rc<Vec<u8>>),
Char(char),
U8(u8),
I8(i8),
U16(u16),
I16(i16),
U32(u32),
I32(i32),
U64(u64),
I64(i64),
Bool(bool),
}

impl<S> Hash<S> for Key where S: hash::Writer {
fn hash(&self, state: &mut S) {
match *self {
KeyStr(ref s) => s.get().hash(state),
KeyBinary(ref b) => b.hash(state),
KeyChar(c) => c.hash(state),
KeyU8(b) => b.hash(state),
KeyI8(b) => b.hash(state),
KeyU16(b) => b.hash(state),
KeyI16(b) => b.hash(state),
KeyU32(b) => b.hash(state),
KeyI32(b) => b.hash(state),
KeyU64(b) => b.hash(state),
KeyI64(b) => b.hash(state),
KeyBool(b) => b.hash(state),
Key::Str(ref s) => s.get().hash(state),
Key::Binary(ref b) => b.hash(state),
Key::Char(c) => c.hash(state),
Key::U8(b) => b.hash(state),
Key::I8(b) => b.hash(state),
Key::U16(b) => b.hash(state),
Key::I16(b) => b.hash(state),
Key::U32(b) => b.hash(state),
Key::I32(b) => b.hash(state),
Key::U64(b) => b.hash(state),
Key::I64(b) => b.hash(state),
Key::Bool(b) => b.hash(state),
}
}
}

impl PhfHash for Key {
fn phf_hash(&self, key: u64) -> (u32, u32, u32) {
match *self {
KeyStr(ref s) => s.get().phf_hash(key),
KeyBinary(ref b) => (**b)[].phf_hash(key),
KeyChar(c) => c.phf_hash(key),
KeyU8(b) => b.phf_hash(key),
KeyI8(b) => b.phf_hash(key),
KeyU16(b) => b.phf_hash(key),
KeyI16(b) => b.phf_hash(key),
KeyU32(b) => b.phf_hash(key),
KeyI32(b) => b.phf_hash(key),
KeyU64(b) => b.phf_hash(key),
KeyI64(b) => b.phf_hash(key),
KeyBool(b) => b.phf_hash(key),
Key::Str(ref s) => s.get().phf_hash(key),
Key::Binary(ref b) => (**b)[].phf_hash(key),
Key::Char(c) => c.phf_hash(key),
Key::U8(b) => b.phf_hash(key),
Key::I8(b) => b.phf_hash(key),
Key::U16(b) => b.phf_hash(key),
Key::I16(b) => b.phf_hash(key),
Key::U32(b) => b.phf_hash(key),
Key::I32(b) => b.phf_hash(key),
Key::U64(b) => b.phf_hash(key),
Key::I64(b) => b.phf_hash(key),
Key::Bool(b) => b.phf_hash(key),
}
}
}
Expand Down

0 comments on commit 4499d0b

Please sign in to comment.