Skip to content

Commit

Permalink
Add an impl of PhfHash for UniCase
Browse files Browse the repository at this point in the history
Fixes #21
  • Loading branch information
Ryman committed Mar 8, 2016
1 parent 888f623 commit d761144
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 0 deletions.
1 change: 1 addition & 0 deletions phf/Cargo.toml
Expand Up @@ -14,6 +14,7 @@ test = false

[features]
core = ["phf_shared/core"]
unicase = ["phf_shared/unicase"]

[dependencies]
phf_shared = { version = "=0.7.13", path = "../phf_shared" }
7 changes: 7 additions & 0 deletions phf_codegen/test/Cargo.toml
Expand Up @@ -4,8 +4,15 @@ authors = ["Steven Fackler <sfackler@gmail.com>"]
version = "0.0.0"
build = "build.rs"

[dependencies]
unicase = "1.1"

[build-dependencies]
unicase = "1.1"

[build-dependencies.phf_codegen]
path = ".."

[dependencies.phf]
path = "../../phf"
features = ["unicase"]
12 changes: 12 additions & 0 deletions phf_codegen/test/build.rs
@@ -1,10 +1,13 @@
extern crate phf_codegen;
extern crate unicase;

use std::env;
use std::fs::File;
use std::io::{BufWriter, Write};
use std::path::Path;

use unicase::UniCase;

fn main() {
let file = Path::new(&env::var("OUT_DIR").unwrap()).join("codegen.rs");
let mut file = BufWriter::new(File::create(&file).unwrap());
Expand Down Expand Up @@ -53,4 +56,13 @@ fn main() {
.build(&mut file)
.unwrap();
write!(&mut file, ";\n").unwrap();

write!(&mut file, "static UNICASE_MAP: ::phf::Map<::unicase::UniCase<&'static str>, \
&'static str> = ").unwrap();
phf_codegen::Map::new()
.entry(UniCase("abc"), "\"a\"")
.entry(UniCase("DEF"), "\"b\"")
.build(&mut file)
.unwrap();
write!(&mut file, ";\n").unwrap();
}
11 changes: 11 additions & 0 deletions phf_codegen/test/src/lib.rs
@@ -1,7 +1,10 @@
extern crate phf;
extern crate unicase;

#[cfg(test)]
mod test {
use unicase::UniCase;

include!(concat!(env!("OUT_DIR"), "/codegen.rs"));

#[test]
Expand Down Expand Up @@ -44,4 +47,12 @@ mod test {
assert_eq!(2, STR_KEYS["b"]);
assert_eq!(3, STR_KEYS["c"]);
}

#[test]
fn unicase_map() {
assert_eq!("a", UNICASE_MAP[&UniCase("AbC")]);
assert_eq!("a", UNICASE_MAP[&UniCase("abc")]);
assert_eq!("b", UNICASE_MAP[&UniCase("DEf")]);
assert!(!UNICASE_MAP.contains_key(&UniCase("XyZ")));
}
}
3 changes: 3 additions & 0 deletions phf_shared/Cargo.toml
Expand Up @@ -14,3 +14,6 @@ test = false

[features]
core = []

[dependencies]
unicase = { version = "1.1", optional = true }
11 changes: 11 additions & 0 deletions phf_shared/src/lib.rs
Expand Up @@ -3,6 +3,9 @@
#[cfg(not(feature = "core"))]
extern crate std as core;

#[cfg(feature = "unicase")]
extern crate unicase;

use core::hash::{Hasher, Hash, SipHasher};

#[inline]
Expand Down Expand Up @@ -103,6 +106,14 @@ impl PhfHash for [u8] {
}
}

#[cfg(feature = "unicase")]
impl<S> PhfHash for unicase::UniCase<S>
where unicase::UniCase<S>: Hash {
#[inline]
fn phf_hash<H: Hasher>(&self, state: &mut H) {
self.hash(state)
}
}

macro_rules! sip_impl(
(le $t:ty) => (
Expand Down

0 comments on commit d761144

Please sign in to comment.