From dd86c6c103f25021b52144085b8fab0a94582bef Mon Sep 17 00:00:00 2001 From: Yuki Okushi Date: Thu, 17 Jun 2021 17:05:08 +0900 Subject: [PATCH] Run rustfmt --- phf/src/lib.rs | 12 +-- phf/src/map.rs | 56 ++++++++---- phf/src/ordered_map.rs | 62 +++++++++---- phf/src/ordered_set.rs | 33 ++++--- phf/src/set.rs | 26 ++++-- phf_codegen/src/lib.rs | 122 +++++++++++++++---------- phf_codegen/test/build.rs | 48 ++++++---- phf_codegen/test/src/lib.rs | 16 ++-- phf_generator/src/bin/gen_hash_test.rs | 4 +- phf_generator/src/lib.rs | 31 ++++--- phf_macros/benches/bench.rs | 3 +- phf_macros/src/lib.rs | 52 ++++++----- phf_macros/tests/test.rs | 18 ++-- phf_shared/src/lib.rs | 36 ++++++-- 14 files changed, 325 insertions(+), 194 deletions(-) diff --git a/phf/src/lib.rs b/phf/src/lib.rs index ec39325b..d0111b58 100644 --- a/phf/src/lib.rs +++ b/phf/src/lib.rs @@ -44,7 +44,7 @@ //! //! [#183]: https://github.com/rust-phf/rust-phf/issues/183 //! [#196]: https://github.com/rust-phf/rust-phf/issues/196 -#![doc(html_root_url="https://docs.rs/phf/0.9")] +#![doc(html_root_url = "https://docs.rs/phf/0.9")] #![warn(missing_docs)] #![cfg_attr(not(feature = "std"), no_std)] @@ -71,7 +71,7 @@ extern crate std as core; /// } /// ``` #[::proc_macro_hack::proc_macro_hack] -pub use phf_macros:: phf_map; +pub use phf_macros::phf_map; #[cfg(feature = "macros")] /// Macro to create a `static` (compile-time) [`OrderedMap`]. @@ -111,20 +111,20 @@ pub use phf_macros::phf_ordered_set; use core::ops::Deref; -pub use phf_shared::PhfHash; #[doc(inline)] pub use self::map::Map; #[doc(inline)] -pub use self::set::Set; -#[doc(inline)] pub use self::ordered_map::OrderedMap; #[doc(inline)] pub use self::ordered_set::OrderedSet; +#[doc(inline)] +pub use self::set::Set; +pub use phf_shared::PhfHash; pub mod map; -pub mod set; pub mod ordered_map; pub mod ordered_set; +pub mod set; // WARNING: this is not considered part of phf's public API and is subject to // change at any time. diff --git a/phf/src/map.rs b/phf/src/map.rs index ed96e804..1cd02dbc 100644 --- a/phf/src/map.rs +++ b/phf/src/map.rs @@ -1,10 +1,10 @@ //! An immutable map constructed at compile time. -use core::ops::Index; -use core::slice; +use crate::Slice; use core::fmt; use core::iter::IntoIterator; -use phf_shared::{self, PhfHash, PhfBorrow, HashKey}; -use crate::Slice; +use core::ops::Index; +use core::slice; +use phf_shared::{self, HashKey, PhfBorrow, PhfHash}; /// An immutable map constructed at compile time. /// @@ -22,13 +22,21 @@ pub struct Map { pub entries: Slice<(K, V)>, } -impl fmt::Debug for Map where K: fmt::Debug, V: fmt::Debug { +impl fmt::Debug for Map +where + K: fmt::Debug, + V: fmt::Debug, +{ fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { fmt.debug_map().entries(self.entries()).finish() } } -impl<'a, K, V, T: ?Sized> Index<&'a T> for Map where T: Eq + PhfHash, K: PhfBorrow { +impl<'a, K, V, T: ?Sized> Index<&'a T> for Map +where + T: Eq + PhfHash, + K: PhfBorrow, +{ type Output = V; fn index(&self, k: &'a T) -> &V { @@ -49,16 +57,18 @@ impl Map { /// Determines if `key` is in the `Map`. pub fn contains_key(&self, key: &T) -> bool - where T: Eq + PhfHash, - K: PhfBorrow + where + T: Eq + PhfHash, + K: PhfBorrow, { self.get(key).is_some() } /// Returns a reference to the value that `key` maps to. pub fn get(&self, key: &T) -> Option<&V> - where T: Eq + PhfHash, - K: PhfBorrow + where + T: Eq + PhfHash, + K: PhfBorrow, { self.get_entry(key).map(|e| e.1) } @@ -68,18 +78,22 @@ impl Map { /// /// This can be useful for interning schemes. pub fn get_key(&self, key: &T) -> Option<&K> - where T: Eq + PhfHash, - K: PhfBorrow + where + T: Eq + PhfHash, + K: PhfBorrow, { self.get_entry(key).map(|e| e.0) } /// Like `get`, but returns both the key and the value. pub fn get_entry(&self, key: &T) -> Option<(&K, &V)> - where T: Eq + PhfHash, - K: PhfBorrow + where + T: Eq + PhfHash, + K: PhfBorrow, { - if self.disps.len() == 0 { return None; } //Prevent panic on empty map + if self.disps.len() == 0 { + return None; + } //Prevent panic on empty map let hashes = phf_shared::hash(key, &self.key); let index = phf_shared::get_index(&hashes, &*self.disps, self.entries.len()); let entry = &self.entries[index as usize]; @@ -95,21 +109,27 @@ impl Map { /// /// Entries are returned in an arbitrary but fixed order. pub fn entries(&self) -> Entries { - Entries { iter: self.entries.iter() } + Entries { + iter: self.entries.iter(), + } } /// Returns an iterator over the keys in the map. /// /// Keys are returned in an arbitrary but fixed order. pub fn keys(&self) -> Keys { - Keys { iter: self.entries() } + Keys { + iter: self.entries(), + } } /// Returns an iterator over the values in the map. /// /// Values are returned in an arbitrary but fixed order. pub fn values(&self) -> Values { - Values { iter: self.entries() } + Values { + iter: self.entries(), + } } } diff --git a/phf/src/ordered_map.rs b/phf/src/ordered_map.rs index 356a0e25..5a5f583e 100644 --- a/phf/src/ordered_map.rs +++ b/phf/src/ordered_map.rs @@ -1,9 +1,9 @@ //! An order-preserving immutable map constructed at compile time. +use core::fmt; use core::iter::IntoIterator; use core::ops::Index; -use core::fmt; use core::slice; -use phf_shared::{self, PhfHash, PhfBorrow, HashKey}; +use phf_shared::{self, HashKey, PhfBorrow, PhfHash}; use crate::Slice; @@ -28,13 +28,21 @@ pub struct OrderedMap { pub entries: Slice<(K, V)>, } -impl fmt::Debug for OrderedMap where K: fmt::Debug, V: fmt::Debug { +impl fmt::Debug for OrderedMap +where + K: fmt::Debug, + V: fmt::Debug, +{ fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { fmt.debug_map().entries(self.entries()).finish() } } -impl<'a, K, V, T: ?Sized> Index<&'a T> for OrderedMap where T: Eq + PhfHash, K: PhfBorrow { +impl<'a, K, V, T: ?Sized> Index<&'a T> for OrderedMap +where + T: Eq + PhfHash, + K: PhfBorrow, +{ type Output = V; fn index(&self, k: &'a T) -> &V { @@ -55,8 +63,9 @@ impl OrderedMap { /// Returns a reference to the value that `key` maps to. pub fn get(&self, key: &T) -> Option<&V> - where T: Eq + PhfHash, - K: PhfBorrow + where + T: Eq + PhfHash, + K: PhfBorrow, { self.get_entry(key).map(|e| e.1) } @@ -66,16 +75,18 @@ impl OrderedMap { /// /// This can be useful for interning schemes. pub fn get_key(&self, key: &T) -> Option<&K> - where T: Eq + PhfHash, - K: PhfBorrow + where + T: Eq + PhfHash, + K: PhfBorrow, { self.get_entry(key).map(|e| e.0) } /// Determines if `key` is in the `Map`. pub fn contains_key(&self, key: &T) -> bool - where T: Eq + PhfHash, - K: PhfBorrow + where + T: Eq + PhfHash, + K: PhfBorrow, { self.get(key).is_some() } @@ -83,8 +94,9 @@ impl OrderedMap { /// Returns the index of the key within the list used to initialize /// the ordered map. pub fn get_index(&self, key: &T) -> Option - where T: Eq + PhfHash, - K: PhfBorrow + where + T: Eq + PhfHash, + K: PhfBorrow, { self.get_internal(key).map(|(i, _)| i) } @@ -97,17 +109,21 @@ impl OrderedMap { /// Like `get`, but returns both the key and the value. pub fn get_entry(&self, key: &T) -> Option<(&K, &V)> - where T: Eq + PhfHash, - K: PhfBorrow + where + T: Eq + PhfHash, + K: PhfBorrow, { self.get_internal(key).map(|(_, e)| e) } fn get_internal(&self, key: &T) -> Option<(usize, (&K, &V))> - where T: Eq + PhfHash, - K: PhfBorrow + where + T: Eq + PhfHash, + K: PhfBorrow, { - if self.disps.len() == 0 { return None; } //Prevent panic on empty map + if self.disps.len() == 0 { + return None; + } //Prevent panic on empty map let hashes = phf_shared::hash(key, &self.key); let idx_index = phf_shared::get_index(&hashes, &*self.disps, self.idxs.len()); let idx = self.idxs[idx_index as usize]; @@ -125,21 +141,27 @@ impl OrderedMap { /// /// Entries are returned in the same order in which they were defined. pub fn entries(&self) -> Entries { - Entries { iter: self.entries.iter() } + Entries { + iter: self.entries.iter(), + } } /// Returns an iterator over the keys in the map. /// /// Keys are returned in the same order in which they were defined. pub fn keys(&self) -> Keys { - Keys { iter: self.entries() } + Keys { + iter: self.entries(), + } } /// Returns an iterator over the values in the map. /// /// Values are returned in the same order in which they were defined. pub fn values(&self) -> Values { - Values { iter: self.entries() } + Values { + iter: self.entries(), + } } } diff --git a/phf/src/ordered_set.rs b/phf/src/ordered_set.rs index 1a025bba..11b9203d 100644 --- a/phf/src/ordered_set.rs +++ b/phf/src/ordered_set.rs @@ -1,8 +1,8 @@ //! An order-preserving immutable set constructed at compile time. -use core::iter::IntoIterator; +use crate::{ordered_map, OrderedMap, PhfHash}; use core::fmt; +use core::iter::IntoIterator; use phf_shared::PhfBorrow; -use crate::{ordered_map, PhfHash, OrderedMap}; /// An order-preserving immutable set constructed at compile time. /// @@ -19,7 +19,10 @@ pub struct OrderedSet { pub map: OrderedMap, } -impl fmt::Debug for OrderedSet where T: fmt::Debug { +impl fmt::Debug for OrderedSet +where + T: fmt::Debug, +{ fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { fmt.debug_set().entries(self).finish() } @@ -41,8 +44,9 @@ impl OrderedSet { /// /// This can be useful for interning schemes. pub fn get_key(&self, key: &U) -> Option<&T> - where U: Eq + PhfHash, - T: PhfBorrow + where + U: Eq + PhfHash, + T: PhfBorrow, { self.map.get_key(key) } @@ -50,8 +54,9 @@ impl OrderedSet { /// Returns the index of the key within the list used to initialize /// the ordered set. pub fn get_index(&self, key: &U) -> Option - where U: Eq + PhfHash, - T: PhfBorrow + where + U: Eq + PhfHash, + T: PhfBorrow, { self.map.get_index(key) } @@ -64,8 +69,9 @@ impl OrderedSet { /// Returns true if `value` is in the `Set`. pub fn contains(&self, value: &U) -> bool - where U: Eq + PhfHash, - T: PhfBorrow + where + U: Eq + PhfHash, + T: PhfBorrow, { self.map.contains_key(value) } @@ -74,11 +80,16 @@ impl OrderedSet { /// /// Values are returned in the same order in which they were defined. pub fn iter(&self) -> Iter { - Iter { iter: self.map.keys() } + Iter { + iter: self.map.keys(), + } } } -impl OrderedSet where T: Eq + PhfHash + PhfBorrow { +impl OrderedSet +where + T: Eq + PhfHash + PhfBorrow, +{ /// Returns true if `other` shares no elements with `self`. #[inline] pub fn is_disjoint(&self, other: &OrderedSet) -> bool { diff --git a/phf/src/set.rs b/phf/src/set.rs index c2f0be2e..8d84c3b6 100644 --- a/phf/src/set.rs +++ b/phf/src/set.rs @@ -1,6 +1,6 @@ //! An immutable set constructed at compile time. -use core::iter::IntoIterator; use core::fmt; +use core::iter::IntoIterator; use phf_shared::{PhfBorrow, PhfHash}; @@ -18,7 +18,10 @@ pub struct Set { pub map: Map, } -impl fmt::Debug for Set where T: fmt::Debug { +impl fmt::Debug for Set +where + T: fmt::Debug, +{ fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { fmt.debug_set().entries(self).finish() } @@ -40,16 +43,18 @@ impl Set { /// /// This can be useful for interning schemes. pub fn get_key(&self, key: &U) -> Option<&T> - where U: Eq + PhfHash, - T: PhfBorrow + where + U: Eq + PhfHash, + T: PhfBorrow, { self.map.get_key(key) } /// Returns true if `value` is in the `Set`. pub fn contains(&self, value: &U) -> bool - where U: Eq + PhfHash, - T: PhfBorrow + where + U: Eq + PhfHash, + T: PhfBorrow, { self.map.contains_key(value) } @@ -58,11 +63,16 @@ impl Set { /// /// Values are returned in an arbitrary but fixed order. pub fn iter(&self) -> Iter { - Iter { iter: self.map.keys() } + Iter { + iter: self.map.keys(), + } } } -impl Set where T: Eq + PhfHash + PhfBorrow { +impl Set +where + T: Eq + PhfHash + PhfBorrow, +{ /// Returns true if `other` shares no elements with `self`. pub fn is_disjoint(&self, other: &Set) -> bool { !self.iter().any(|value| other.contains(value)) diff --git a/phf_codegen/src/lib.rs b/phf_codegen/src/lib.rs index 2555a9fa..3caa4362 100644 --- a/phf_codegen/src/lib.rs +++ b/phf_codegen/src/lib.rs @@ -125,7 +125,7 @@ //! ``` #![doc(html_root_url = "https://docs.rs/phf_codegen/0.9")] -use phf_shared::{PhfHash, FmtConst}; +use phf_shared::{FmtConst, PhfHash}; use std::collections::HashSet; use std::fmt; use std::hash::Hash; @@ -213,45 +213,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, + " ]), -}}") +}}" + ) } } @@ -263,9 +272,7 @@ pub struct Set { impl Set { /// Constructs a new `phf::Set` builder. pub fn new() -> Set { - Set { - map: Map::new(), - } + Set { map: Map::new() } } /// Set the path to the `phf` crate from the global namespace @@ -288,7 +295,7 @@ impl Set { /// Panics if there are any duplicate keys. pub fn build(&self) -> DisplaySet { DisplaySet { - inner: self.map.build() + inner: self.map.build(), } } } @@ -372,43 +379,58 @@ pub struct DisplayOrderedMap<'a, K: 'a> { impl<'a, K: FmtConst + 'a> fmt::Display for DisplayOrderedMap<'a, K> { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - write!(f, - "{}::OrderedMap {{ + write!( + f, + "{}::OrderedMap {{ key: {:?}, disps: {}::Slice::Static(&[", - self.path, self.state.key, self.path)?; + self.path, self.state.key, self.path + )?; for &(d1, d2) in &self.state.disps { - write!(f, - " + write!( + f, + " ({}, {}),", - d1, - d2)?; + d1, d2 + )?; } - write!(f, - " + write!( + f, + " ]), - idxs: {}::Slice::Static(&[", self.path)?; + idxs: {}::Slice::Static(&[", + self.path + )?; for &idx in &self.state.map { - write!(f, - " + write!( + f, + " {},", - idx)?; + idx + )?; } - write!(f, - " + write!( + f, + " ]), - entries: {}::Slice::Static(&[", self.path)?; + entries: {}::Slice::Static(&[", + self.path + )?; for (key, value) in self.keys.iter().zip(self.values.iter()) { - write!(f, - " + write!( + f, + " ({}, {}),", - Delegate(key), - value)?; + Delegate(key), + value + )?; } - write!(f, - " + write!( + f, + " ]), -}}") +}}" + ) } } @@ -446,7 +468,7 @@ impl OrderedSet { /// Panics if there are any duplicate keys. pub fn build(&self) -> DisplayOrderedSet { DisplayOrderedSet { - inner: self.map.build() + inner: self.map.build(), } } } @@ -458,6 +480,10 @@ pub struct DisplayOrderedSet<'a, T: 'a> { impl<'a, T: FmtConst + 'a> fmt::Display for DisplayOrderedSet<'a, T> { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - write!(f, "{}::OrderedSet {{ map: {} }}", self.inner.path, self.inner) + write!( + f, + "{}::OrderedSet {{ map: {} }}", + self.inner.path, self.inner + ) } } diff --git a/phf_codegen/test/build.rs b/phf_codegen/test/build.rs index ce93f621..94235d56 100644 --- a/phf_codegen/test/build.rs +++ b/phf_codegen/test/build.rs @@ -79,27 +79,37 @@ fn main() -> io::Result<()> { )?; //u32 is used here purely for a type that impls `Hash+PhfHash+Eq+fmt::Debug`, but is not required for the empty test itself - writeln!(&mut file, - "static EMPTY: ::phf::Map = \n{};", - phf_codegen::Map::::new().build())?; + writeln!( + &mut file, + "static EMPTY: ::phf::Map = \n{};", + phf_codegen::Map::::new().build() + )?; - writeln!(&mut file, - "static EMPTY_ORDERED: ::phf::OrderedMap = \n{};", - phf_codegen::OrderedMap::::new().build())?; + writeln!( + &mut file, + "static EMPTY_ORDERED: ::phf::OrderedMap = \n{};", + phf_codegen::OrderedMap::::new().build() + )?; - writeln!(&mut file, "static ARRAY_KEYS: ::phf::Map<[u8; 3], u32> = \n{};", - phf_codegen::Map::<[u8; 3]>::new() - .entry(*b"foo", "0") - .entry(*b"bar", "1") - .entry(*b"baz", "2") - .build())?; + writeln!( + &mut file, + "static ARRAY_KEYS: ::phf::Map<[u8; 3], u32> = \n{};", + phf_codegen::Map::<[u8; 3]>::new() + .entry(*b"foo", "0") + .entry(*b"bar", "1") + .entry(*b"baz", "2") + .build() + )?; // key type required here as it will infer `&'static [u8; 3]` instead - writeln!(&mut file, "static BYTE_STR_KEYS: ::phf::Map<&[u8], u32> = \n{};", - phf_codegen::Map::<&[u8]>::new() - .entry(b"foo", "0") - .entry(b"bar", "1") - .entry(b"baz", "2") - .entry(b"quux", "3") - .build()) + writeln!( + &mut file, + "static BYTE_STR_KEYS: ::phf::Map<&[u8], u32> = \n{};", + phf_codegen::Map::<&[u8]>::new() + .entry(b"foo", "0") + .entry(b"bar", "1") + .entry(b"baz", "2") + .entry(b"quux", "3") + .build() + ) } diff --git a/phf_codegen/test/src/lib.rs b/phf_codegen/test/src/lib.rs index 3398d48d..980ff2ec 100644 --- a/phf_codegen/test/src/lib.rs +++ b/phf_codegen/test/src/lib.rs @@ -27,7 +27,10 @@ mod test { assert_eq!("b", ORDERED_MAP[&2]); assert_eq!("c", ORDERED_MAP[&3]); assert!(!ORDERED_MAP.contains_key(&100)); - assert_eq!(&["a", "b", "c"][..], &ORDERED_MAP.values().cloned().collect::>()[..]); + assert_eq!( + &["a", "b", "c"][..], + &ORDERED_MAP.values().cloned().collect::>()[..] + ); } #[test] @@ -36,7 +39,10 @@ mod test { assert!(ORDERED_SET.contains(&2)); assert!(ORDERED_SET.contains(&3)); assert!(!ORDERED_SET.contains(&4)); - assert_eq!(&[1, 2, 3][..], &ORDERED_SET.iter().cloned().collect::>()[..]); + assert_eq!( + &[1, 2, 3][..], + &ORDERED_SET.iter().cloned().collect::>()[..] + ); } #[test] @@ -86,15 +92,13 @@ mod test { assert_eq!(3, BYTE_STR_KEYS[&b"quux"[..]]); } - #[test] + #[test] fn empty_map() { assert_eq!(None, EMPTY.get(&1)); } - #[test] + #[test] fn empty_ordered_map() { assert_eq!(None, EMPTY_ORDERED.get(&1)); } - - } diff --git a/phf_generator/src/bin/gen_hash_test.rs b/phf_generator/src/bin/gen_hash_test.rs index 770412f5..2e1fbec4 100644 --- a/phf_generator/src/bin/gen_hash_test.rs +++ b/phf_generator/src/bin/gen_hash_test.rs @@ -9,7 +9,9 @@ use phf_generator::generate_hash; fn gen_vec(len: usize) -> Vec { let mut rng = SmallRng::seed_from_u64(0xAAAAAAAAAAAAAAAA).sample_iter(Alphanumeric); - (0 .. len).map(move |_| rng.by_ref().take(64).collect::()).collect() + (0..len) + .map(move |_| rng.by_ref().take(64).collect::()) + .collect() } fn main() { diff --git a/phf_generator/src/lib.rs b/phf_generator/src/lib.rs index b1625e7b..6c848ce5 100644 --- a/phf_generator/src/lib.rs +++ b/phf_generator/src/lib.rs @@ -1,8 +1,8 @@ -#![doc(html_root_url="https://docs.rs/phf_generator/0.9")] -use phf_shared::{PhfHash, HashKey}; -use rand::{SeedableRng, Rng}; +#![doc(html_root_url = "https://docs.rs/phf_generator/0.9")] +use phf_shared::{HashKey, PhfHash}; use rand::distributions::Standard; use rand::rngs::SmallRng; +use rand::{Rng, SeedableRng}; const DEFAULT_LAMBDA: usize = 5; @@ -27,20 +27,23 @@ fn try_generate_hash(entries: &[H], key: HashKey) -> Option, } - let hashes: Vec<_> = entries.iter().map(|entry| phf_shared::hash(entry, &key)).collect(); + let hashes: Vec<_> = entries + .iter() + .map(|entry| phf_shared::hash(entry, &key)) + .collect(); let buckets_len = (hashes.len() + DEFAULT_LAMBDA - 1) / DEFAULT_LAMBDA; let mut buckets = (0..buckets_len) - .map(|i| { - Bucket { - idx: i, - keys: vec![], - } - }) - .collect::>(); + .map(|i| Bucket { + idx: i, + keys: vec![], + }) + .collect::>(); for (i, hash) in hashes.iter().enumerate() { - buckets[(hash.g % (buckets_len as u32)) as usize].keys.push(i); + buckets[(hash.g % (buckets_len as u32)) as usize] + .keys + .push(i); } // Sort descending @@ -72,8 +75,8 @@ fn try_generate_hash(entries: &[H], key: HashKey) -> Option $value:expr,)+) => { @@ -108,7 +108,6 @@ mod map { }) } - #[bench] fn bench_hashmap_none(b: &mut Bencher) { let mut map = HashMap::new(); diff --git a/phf_macros/src/lib.rs b/phf_macros/src/lib.rs index 2f13eafb..23a18698 100644 --- a/phf_macros/src/lib.rs +++ b/phf_macros/src/lib.rs @@ -36,8 +36,8 @@ enum ParsedKey { impl PhfHash for ParsedKey { fn phf_hash(&self, state: &mut H) - where - H: Hasher, + where + H: Hasher, { match self { ParsedKey::Str(s) => s.phf_hash(state), @@ -125,32 +125,34 @@ impl ParsedKey { } Expr::Group(group) => ParsedKey::from_expr(&group.expr), #[cfg(feature = "unicase")] - Expr::Call(call) => if let Expr::Path(ep) = call.func.as_ref() { - let segments = &mut ep.path.segments.iter().rev(); - let last = &segments.next()?.ident; - let last_ahead = &segments.next()?.ident; - let is_unicode = last_ahead == "UniCase" && last == "unicode"; - let is_ascii = last_ahead == "UniCase" && last == "ascii"; - if call.args.len() == 1 && (is_unicode || is_ascii) { - if let Some(Expr::Lit(ExprLit { - attrs: _, - lit: Lit::Str(s), - })) = call.args.first() - { - let v = if is_unicode { - UniCase::unicode(s.value()) + Expr::Call(call) => { + if let Expr::Path(ep) = call.func.as_ref() { + let segments = &mut ep.path.segments.iter().rev(); + let last = &segments.next()?.ident; + let last_ahead = &segments.next()?.ident; + let is_unicode = last_ahead == "UniCase" && last == "unicode"; + let is_ascii = last_ahead == "UniCase" && last == "ascii"; + if call.args.len() == 1 && (is_unicode || is_ascii) { + if let Some(Expr::Lit(ExprLit { + attrs: _, + lit: Lit::Str(s), + })) = call.args.first() + { + let v = if is_unicode { + UniCase::unicode(s.value()) + } else { + UniCase::ascii(s.value()) + }; + Some(ParsedKey::UniCase(v)) } else { - UniCase::ascii(s.value()) - }; - Some(ParsedKey::UniCase(v)) + None + } } else { None } } else { None } - } else { - None } _ => None, } @@ -164,8 +166,8 @@ struct Key { impl PhfHash for Key { fn phf_hash(&self, state: &mut H) - where - H: Hasher, + where + H: Hasher, { self.parsed.phf_hash(state) } @@ -188,8 +190,8 @@ struct Entry { impl PhfHash for Entry { fn phf_hash(&self, state: &mut H) - where - H: Hasher, + where + H: Hasher, { self.key.phf_hash(state) } diff --git a/phf_macros/tests/test.rs b/phf_macros/tests/test.rs index 9a150562..d196b733 100644 --- a/phf_macros/tests/test.rs +++ b/phf_macros/tests/test.rs @@ -1,6 +1,6 @@ mod map { - use std::collections::{HashMap, HashSet}; use phf::phf_map; + use std::collections::{HashMap, HashSet}; #[allow(dead_code)] static TRAILING_COMMA: phf::Map<&'static str, isize> = phf_map!( @@ -35,7 +35,10 @@ mod map { "foo" => 10, "bar" => 11, ); - let hash = MAP.entries().map(|(&k, &v)| (k, v)).collect::>(); + let hash = MAP + .entries() + .map(|(&k, &v)| (k, v)) + .collect::>(); assert!(Some(&10) == hash.get(&("foo"))); assert!(Some(&11) == hash.get(&("bar"))); assert_eq!(2, hash.len()); @@ -252,8 +255,8 @@ mod map { } mod set { - use std::collections::HashSet; use phf::phf_set; + use std::collections::HashSet; #[allow(dead_code)] static TRAILING_COMMA: phf::Set<&'static str> = phf_set! { @@ -478,10 +481,7 @@ mod ordered_set { #[test] fn test_index() { - static MAP: phf::OrderedSet<&'static str> = phf_ordered_set!( - "foo", - "bar", - ); + static MAP: phf::OrderedSet<&'static str> = phf_ordered_set!("foo", "bar",); assert_eq!(Some(&"foo"), MAP.index(0)); assert_eq!(Some(&"bar"), MAP.index(1)); assert_eq!(None, MAP.index(2)); @@ -509,9 +509,7 @@ mod ordered_set { #[test] fn test_into_iterator() { - static SET: phf::OrderedSet<&'static str> = phf_ordered_set!( - "foo", - ); + static SET: phf::OrderedSet<&'static str> = phf_ordered_set!("foo",); for e in &SET { assert_eq!(&"foo", e); diff --git a/phf_shared/src/lib.rs b/phf_shared/src/lib.rs index ecfa3365..c0ed0d57 100644 --- a/phf_shared/src/lib.rs +++ b/phf_shared/src/lib.rs @@ -5,7 +5,7 @@ extern crate std as core; use core::fmt; -use core::hash::{Hasher, Hash}; +use core::hash::{Hash, Hasher}; use core::num::Wrapping; use siphasher::sip128::{Hash128, Hasher128, SipHasher13}; @@ -32,7 +32,10 @@ pub fn hash(x: &T, key: &HashKey) -> Hashes { let mut hasher = SipHasher13::new_with_keys(0, *key); x.phf_hash(&mut hasher); - let Hash128 { h1: lower, h2: upper} = hasher.finish128(); + let Hash128 { + h1: lower, + h2: upper, + } = hasher.finish128(); Hashes { g: (lower >> 32) as u32, @@ -63,7 +66,8 @@ pub trait PhfHash { /// Feeds a slice of this type into the state provided. fn phf_hash_slice(data: &[Self], state: &mut H) - where Self: Sized + where + Self: Sized, { for piece in data { piece.phf_hash(state); @@ -159,7 +163,22 @@ macro_rules! impl_reflexive( ) ); -impl_reflexive!(str, char, u8, i8, u16, i16, u32, i32, u64, i64, u128, i128, bool, [u8]); +impl_reflexive!( + str, + char, + u8, + i8, + u16, + i16, + u32, + i32, + u64, + i64, + u128, + i128, + bool, + [u8] +); #[cfg(feature = "std")] impl PhfBorrow for String { @@ -242,7 +261,9 @@ impl FmtConst for [u8] { #[cfg(feature = "unicase")] impl PhfHash for unicase::UniCase - where unicase::UniCase: Hash { +where + unicase::UniCase: Hash, +{ #[inline] fn phf_hash(&self, state: &mut H) { self.hash(state) @@ -250,7 +271,10 @@ impl PhfHash for unicase::UniCase } #[cfg(feature = "unicase")] -impl FmtConst for unicase::UniCase where S: AsRef { +impl FmtConst for unicase::UniCase +where + S: AsRef, +{ fn fmt_const(&self, f: &mut fmt::Formatter) -> fmt::Result { if self.is_ascii() { f.write_str("UniCase::ascii(")?;