From 4499d0b4ab17e4bc05dd24352b5399640318c1d8 Mon Sep 17 00:00:00 2001 From: Steven Fackler Date: Mon, 17 Nov 2014 13:30:22 -0800 Subject: [PATCH] Clean up Key enum --- phf_mac/src/lib.rs | 42 ++++++++++++------------- phf_mac/src/util.rs | 75 ++++++++++++++++++++++----------------------- 2 files changed, 56 insertions(+), 61 deletions(-) diff --git a/phf_mac/src/lib.rs b/phf_mac/src/lib.rs index 8ff04e70..9febece5 100644 --- a/phf_mac/src/lib.rs +++ b/phf_mac/src/lib.rs @@ -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"] @@ -111,7 +109,7 @@ fn parse_map(cx: &mut ExtCtxt, tts: &[TokenTree]) -> Option> { 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) { @@ -150,7 +148,7 @@ fn parse_set(cx: &mut ExtCtxt, tts: &[TokenTree]) -> Option> { 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 { @@ -176,23 +174,23 @@ fn parse_key(cx: &mut ExtCtxt, e: &Expr) -> Option { 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 diff --git a/phf_mac/src/util.rs b/phf_mac/src/util.rs index 584a609b..1782219f 100644 --- a/phf_mac/src/util.rs +++ b/phf_mac/src/util.rs @@ -17,44 +17,41 @@ 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>), - KeyChar(char), - KeyU8(u8), - KeyI8(i8), - KeyU16(u16), - KeyI16(i16), - KeyU32(u32), - KeyI32(i32), - KeyU64(u64), - KeyI64(i64), - KeyBool(bool), + Str(InternedString), + Binary(Rc>), + Char(char), + U8(u8), + I8(i8), + U16(u16), + I16(i16), + U32(u32), + I32(i32), + U64(u64), + I64(i64), + Bool(bool), } impl Hash 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), } } } @@ -62,18 +59,18 @@ impl Hash for Key where S: hash::Writer { 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), } } }