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

Added FmtConst impl for String and Vec<Primitive> #185

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
53 changes: 30 additions & 23 deletions phf_codegen/src/lib.rs
Expand Up @@ -133,7 +133,7 @@
//! ```
#![doc(html_root_url = "https://docs.rs/phf_codegen/0.7")]

use phf_shared::{PhfHash, FmtConst};
use phf_shared::{FmtConst, PhfHash};
use std::collections::HashSet;
use std::fmt;
use std::hash::Hash;
Expand Down Expand Up @@ -221,45 +221,54 @@ pub struct DisplayMap<'a, K> {
state: HashState,
keys: &'a [K],
values: &'a [String],

}

impl<'a, K: FmtConst + 'a> fmt::Display for DisplayMap<'a, K> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
// funky formatting here for nice output
write!(f,
"{}::Map {{
write!(
f,
"{}::Map {{
key: {:?},
disps: {}::Slice::Static(&[",
self.path, self.state.key, self.path)?;
self.path, self.state.key, self.path
)?;

// write map displacements
for &(d1, d2) in &self.state.disps {
write!(f,
"
write!(
f,
"
({}, {}),",
d1,
d2)?;
d1, d2
)?;
}

write!(f,
"
write!(
f,
"
]),
entries: {}::Slice::Static(&[", self.path)?;
entries: {}::Slice::Static(&[",
self.path
)?;

// write map entries
for &idx in &self.state.map {
write!(f,
"
write!(
f,
"
({}, {}),",
Delegate(&self.keys[idx]),
&self.values[idx])?;
Delegate(&self.keys[idx]),
&self.values[idx]
)?;
}

write!(f,
"
write!(
f,
"
]),
}}")
}}"
)
}
}

Expand All @@ -271,9 +280,7 @@ pub struct Set<T> {
impl<T: Hash + PhfHash + Eq + FmtConst> Set<T> {
/// Constructs a new `phf::Set` builder.
pub fn new() -> Set<T> {
Set {
map: Map::new(),
}
Set { map: Map::new() }
}

/// Set the path to the `phf` crate from the global namespace
Expand All @@ -296,7 +303,7 @@ impl<T: Hash + PhfHash + Eq + FmtConst> Set<T> {
/// Panics if there are any duplicate keys.
pub fn build(&self) -> DisplaySet<T> {
DisplaySet {
inner: self.map.build()
inner: self.map.build(),
}
}
}
Expand Down
13 changes: 13 additions & 0 deletions phf_shared/src/lib.rs
Expand Up @@ -111,6 +111,19 @@ delegate_debug!(u128);
delegate_debug!(i128);
delegate_debug!(bool);

delegate_debug!(String);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For consistency I would probably move this one up above under the delegate_debug!(str); line then have this next grouping be for Vec<>(). Would it make sense to have Vec<String> as well?

delegate_debug!(Vec<u8>);
delegate_debug!(Vec<i8>);
delegate_debug!(Vec<u16>);
delegate_debug!(Vec<i16>);
delegate_debug!(Vec<u32>);
delegate_debug!(Vec<i32>);
delegate_debug!(Vec<u64>);
delegate_debug!(Vec<i64>);
delegate_debug!(Vec<u128>);
delegate_debug!(Vec<i128>);
delegate_debug!(Vec<bool>);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

delegate_debug!() is not correct for Vec<_> because it prints an array literal.


#[cfg(feature = "std")]
impl PhfHash for String {
#[inline]
Expand Down