Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix formatting for arrays after #156 #157

Merged
merged 2 commits into from Jul 6, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
79 changes: 46 additions & 33 deletions phf_codegen/test/build.rs
Expand Up @@ -3,75 +3,88 @@ extern crate unicase;

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

use unicase::UniCase;

fn main() {
fn main() -> io::Result<()> {
let file = Path::new(&env::var("OUT_DIR").unwrap()).join("codegen.rs");
let mut file = BufWriter::new(File::create(&file).unwrap());
let mut file = BufWriter::new(File::create(&file)?);

write!(&mut file, "static MAP: ::phf::Map<u32, &'static str> = ").unwrap();
write!(&mut file, "static MAP: ::phf::Map<u32, &'static str> = ")?;
phf_codegen::Map::new()
.entry(1u32, "\"a\"")
.entry(2u32, "\"b\"")
.entry(3u32, "\"c\"")
.build(&mut file)
.unwrap();
write!(&mut file, ";\n").unwrap();
.build(&mut file)?;
write!(&mut file, ";\n")?;

write!(&mut file, "static SET: ::phf::Set<u32> = ").unwrap();
write!(&mut file, "static SET: ::phf::Set<u32> = ")?;
phf_codegen::Set::new()
.entry(1u32)
.entry(2u32)
.entry(3u32)
.build(&mut file)
.unwrap();
write!(&mut file, ";\n").unwrap();
.build(&mut file)?;
write!(&mut file, ";\n")?;

write!(&mut file, "static ORDERED_MAP: ::phf::OrderedMap<u32, &'static str> = ").unwrap();
write!(&mut file, "static ORDERED_MAP: ::phf::OrderedMap<u32, &'static str> = ")?;
phf_codegen::OrderedMap::new()
.entry(1u32, "\"a\"")
.entry(2u32, "\"b\"")
.entry(3u32, "\"c\"")
.build(&mut file)
.unwrap();
write!(&mut file, ";\n").unwrap();
.build(&mut file)?;
write!(&mut file, ";\n")?;

write!(&mut file, "static ORDERED_SET: ::phf::OrderedSet<u32> = ").unwrap();
write!(&mut file, "static ORDERED_SET: ::phf::OrderedSet<u32> = ")?;
phf_codegen::OrderedSet::new()
.entry(1u32)
.entry(2u32)
.entry(3u32)
.build(&mut file)
.unwrap();
write!(&mut file, ";\n").unwrap();
.build(&mut file)?;
write!(&mut file, ";\n")?;

write!(&mut file, "static STR_KEYS: ::phf::Map<&'static str, u32> = ").unwrap();
write!(&mut file, "static STR_KEYS: ::phf::Map<&'static str, u32> = ")?;
phf_codegen::Map::new()
.entry("a", "1")
.entry("b", "2")
.entry("c", "3")
.build(&mut file)
.unwrap();
write!(&mut file, ";\n").unwrap();
.build(&mut file)?;
write!(&mut file, ";\n")?;

write!(&mut file, "static UNICASE_MAP: ::phf::Map<::unicase::UniCase<&'static str>, \
&'static str> = ").unwrap();
&'static str> = ")?;
phf_codegen::Map::new()
.entry(UniCase::new("abc"), "\"a\"")
.entry(UniCase::new("DEF"), "\"b\"")
.build(&mut file)
.unwrap();
write!(&mut file, ";\n").unwrap();
.build(&mut file)?;
write!(&mut file, ";\n")?;

//u32 is used here purely for a type that impls `Hash+PhfHash+Eq+fmt::Debug`, but is not required for the empty test itself
write!(&mut file, "static EMPTY: ::phf::Map<u32, u32> = ").unwrap();
phf_codegen::Map::<u32>::new().build(&mut file).unwrap();
write!(&mut file, ";\n").unwrap();
write!(&mut file, "static EMPTY: ::phf::Map<u32, u32> = ")?;
phf_codegen::Map::<u32>::new().build(&mut file)?;
write!(&mut file, ";\n")?;

write!(&mut file, "static EMPTY_ORDERED: ::phf::OrderedMap<u32, u32> = ").unwrap();
phf_codegen::OrderedMap::<u32>::new().build(&mut file).unwrap();
write!(&mut file, ";\n").unwrap();
write!(&mut file, "static EMPTY_ORDERED: ::phf::OrderedMap<u32, u32> = ")?;
phf_codegen::OrderedMap::<u32>::new().build(&mut file)?;
write!(&mut file, ";\n")?;

write!(&mut file, "static ARRAY_KEYS: ::phf::Map<[u8; 3], u32> = ")?;
phf_codegen::Map::<[u8; 3]>::new()
.entry(*b"foo", "0")
.entry(*b"bar", "1")
.entry(*b"baz", "2")
.build(&mut file)?;

writeln!(&mut file, ";")?;

write!(&mut file, "static BYTE_STR_KEYS: ::phf::Map<&[u8], u32> = ")?;
// key type required here as it will infer `&'static [u8; 3]` instead
phf_codegen::Map::<&[u8]>::new()
.entry(b"foo", "0")
.entry(b"bar", "1")
.entry(b"baz", "2")
.entry(b"quux", "3")
.build(&mut file)?;
writeln!(&mut file, ";")
}
17 changes: 17 additions & 0 deletions phf_codegen/test/src/lib.rs
Expand Up @@ -53,6 +53,22 @@ mod test {
assert!(!UNICASE_MAP.contains_key(&UniCase::new("XyZ")));
}

#[test]
fn array_keys() {
assert_eq!(0, ARRAY_KEYS[b"foo"]);
assert_eq!(1, ARRAY_KEYS[b"bar"]);
assert_eq!(2, ARRAY_KEYS[b"baz"]);
}

#[test]
fn byte_str_keys() {
// slicing is required unless the key type is fixed-size
assert_eq!(0, BYTE_STR_KEYS[&b"foo"[..]]);
assert_eq!(1, BYTE_STR_KEYS[&b"bar"[..]]);
assert_eq!(2, BYTE_STR_KEYS[&b"baz"[..]]);
assert_eq!(3, BYTE_STR_KEYS[&b"quux"[..]]);
}

#[test]
fn empty_map() {
assert_eq!(None, EMPTY.get(&1));
Expand All @@ -63,4 +79,5 @@ mod test {
assert_eq!(None, EMPTY_ORDERED.get(&1));
}


}
8 changes: 6 additions & 2 deletions phf_shared/src/lib.rs
Expand Up @@ -212,6 +212,11 @@ impl PhfHash for char {
}
}

// minimize duplicated code since formatting drags in quite a bit
fn fmt_array(array: &[u8], f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{:?}", array)
}

macro_rules! array_impl(
($t:ty, $n:expr) => (
impl PhfHash for [$t; $n] {
Expand All @@ -223,8 +228,7 @@ macro_rules! array_impl(

impl FmtConst for [$t; $n] {
fn fmt_const(&self, f: &mut fmt::Formatter) -> fmt::Result {
// delegate to the slice impl to minimize duplicated code
self[..].fmt_const(f)
fmt_array(self, f)
}
}
)
Expand Down