From 99d353390f8124a283da9202fd4d163e68bc1949 Mon Sep 17 00:00:00 2001 From: "Brandon H. Gomes" Date: Sun, 25 Jul 2021 14:11:45 -0400 Subject: [PATCH] remove Slice type and fix some docs --- phf/src/lib.rs | 37 ------------------------------------- phf/src/map.rs | 29 ++++++++--------------------- phf/src/ordered_map.rs | 32 +++++++++----------------------- phf/src/ordered_set.rs | 18 +++--------------- phf/src/set.rs | 16 ++-------------- phf_codegen/src/lib.rs | 27 ++++++++++++--------------- phf_macros/src/lib.rs | 10 +++++----- 7 files changed, 39 insertions(+), 130 deletions(-) diff --git a/phf/src/lib.rs b/phf/src/lib.rs index 35a146cb..ba3b8172 100644 --- a/phf/src/lib.rs +++ b/phf/src/lib.rs @@ -110,8 +110,6 @@ pub use phf_macros::phf_set; #[proc_macro_hack::proc_macro_hack] pub use phf_macros::phf_ordered_set; -use core::ops::Deref; - #[doc(inline)] pub use self::map::Map; #[doc(inline)] @@ -126,38 +124,3 @@ pub mod map; 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. -// -// Basically Cow, but with the Owned version conditionally compiled. -#[doc(hidden)] -pub enum Slice { - Static(&'static [T]), - #[cfg(feature = "std")] - Dynamic(Vec), -} - -// NOTE: we need this because `Deref::deref` is not a `const-fn` so we can't use -// `len` as written in `Map`/`Set`/`OrderedMap`/`OrderedSet` since it involves -// an implicit indirection -#[cfg(not(feature = "std"))] -impl Slice { - pub(crate) const fn len(&self) -> usize { - match self { - Self::Static(slice) => slice.len(), - } - } -} - -impl Deref for Slice { - type Target = [T]; - - fn deref(&self) -> &[T] { - match *self { - Slice::Static(t) => t, - #[cfg(feature = "std")] - Slice::Dynamic(ref t) => t, - } - } -} diff --git a/phf/src/map.rs b/phf/src/map.rs index 1140f3d9..5d13592d 100644 --- a/phf/src/map.rs +++ b/phf/src/map.rs @@ -1,5 +1,4 @@ //! An immutable map constructed at compile time. -use crate::Slice; use core::fmt; use core::iter::IntoIterator; use core::ops::Index; @@ -17,9 +16,9 @@ pub struct Map { #[doc(hidden)] pub key: HashKey, #[doc(hidden)] - pub disps: Slice<(u32, u32)>, + pub disps: &'static [(u32, u32)], #[doc(hidden)] - pub entries: Slice<(K, V)>, + pub entries: &'static [(K, V)], } impl fmt::Debug for Map @@ -45,30 +44,18 @@ where } impl Map { - /// Returns true if the `Map` is empty. - #[cfg(feature = "std")] - pub fn is_empty(&self) -> bool { - self.len() == 0 + /// Returns the number of entries in the `Map`. + #[inline] + pub const fn len(&self) -> usize { + self.entries.len() } /// Returns true if the `Map` is empty. - #[cfg(not(feature = "std"))] + #[inline] pub const fn is_empty(&self) -> bool { self.len() == 0 } - /// Returns the number of entries in the `Map`. - #[cfg(feature = "std")] - pub fn len(&self) -> usize { - self.entries.len() - } - - /// Returns the number of entries in the `Map`. - #[cfg(not(feature = "std"))] - pub const fn len(&self) -> usize { - self.entries.len() - } - /// Determines if `key` is in the `Map`. pub fn contains_key(&self, key: &T) -> bool where @@ -105,7 +92,7 @@ impl Map { T: Eq + PhfHash, K: PhfBorrow, { - if self.disps.len() == 0 { + if self.disps.is_empty() { return None; } //Prevent panic on empty map let hashes = phf_shared::hash(key, &self.key); diff --git a/phf/src/ordered_map.rs b/phf/src/ordered_map.rs index c2066d3b..bd7d566b 100644 --- a/phf/src/ordered_map.rs +++ b/phf/src/ordered_map.rs @@ -5,8 +5,6 @@ use core::ops::Index; use core::slice; use phf_shared::{self, HashKey, PhfBorrow, PhfHash}; -use crate::Slice; - /// An order-preserving immutable map constructed at compile time. /// /// Unlike a `Map`, iteration order is guaranteed to match the definition @@ -21,11 +19,11 @@ pub struct OrderedMap { #[doc(hidden)] pub key: HashKey, #[doc(hidden)] - pub disps: Slice<(u32, u32)>, + pub disps: &'static [(u32, u32)], #[doc(hidden)] - pub idxs: Slice, + pub idxs: &'static [usize], #[doc(hidden)] - pub entries: Slice<(K, V)>, + pub entries: &'static [(K, V)], } impl fmt::Debug for OrderedMap @@ -51,26 +49,14 @@ where } impl OrderedMap { - /// Returns the number of entries in the `Map`. - #[cfg(feature = "std")] - pub fn len(&self) -> usize { - self.entries.len() - } - - /// Returns the number of entries in the `Map`. - #[cfg(not(feature = "std"))] + /// Returns the number of entries in the `OrderedMap`. + #[inline] pub const fn len(&self) -> usize { self.entries.len() } - /// Returns true if the `Map` is empty. - #[cfg(feature = "std")] - pub fn is_empty(&self) -> bool { - self.len() == 0 - } - - /// Returns true if the `Map` is empty. - #[cfg(not(feature = "std"))] + /// Returns true if the `OrderedMap` is empty. + #[inline] pub const fn is_empty(&self) -> bool { self.len() == 0 } @@ -96,7 +82,7 @@ impl OrderedMap { self.get_entry(key).map(|e| e.0) } - /// Determines if `key` is in the `Map`. + /// Determines if `key` is in the `OrderedMap`. pub fn contains_key(&self, key: &T) -> bool where T: Eq + PhfHash, @@ -135,7 +121,7 @@ impl OrderedMap { T: Eq + PhfHash, K: PhfBorrow, { - if self.disps.len() == 0 { + if self.disps.is_empty() { return None; } //Prevent panic on empty map let hashes = phf_shared::hash(key, &self.key); diff --git a/phf/src/ordered_set.rs b/phf/src/ordered_set.rs index 35163da0..d62428dd 100644 --- a/phf/src/ordered_set.rs +++ b/phf/src/ordered_set.rs @@ -30,25 +30,13 @@ where impl OrderedSet { /// Returns the number of elements in the `OrderedSet`. - #[cfg(feature = "std")] - pub fn len(&self) -> usize { - self.map.len() - } - - /// Returns the number of elements in the `OrderedSet`. - #[cfg(not(feature = "std"))] + #[inline] pub const fn len(&self) -> usize { self.map.len() } /// Returns true if the `OrderedSet` contains no elements. - #[cfg(feature = "std")] - pub fn is_empty(&self) -> bool { - self.len() == 0 - } - - /// Returns true if the `OrderedSet` contains no elements. - #[cfg(not(feature = "std"))] + #[inline] pub const fn is_empty(&self) -> bool { self.len() == 0 } @@ -81,7 +69,7 @@ impl OrderedSet { self.map.index(index).map(|(k, &())| k) } - /// Returns true if `value` is in the `Set`. + /// Returns true if `value` is in the `OrderedSet`. pub fn contains(&self, value: &U) -> bool where U: Eq + PhfHash, diff --git a/phf/src/set.rs b/phf/src/set.rs index 64394d59..a7223e47 100644 --- a/phf/src/set.rs +++ b/phf/src/set.rs @@ -29,25 +29,13 @@ where impl Set { /// Returns the number of elements in the `Set`. - #[cfg(feature = "std")] - pub fn len(&self) -> usize { - self.map.len() - } - - /// Returns the number of elements in the `Set`. - #[cfg(not(feature = "std"))] + #[inline] pub const fn len(&self) -> usize { self.map.len() } /// Returns true if the `Set` contains no elements. - #[cfg(feature = "std")] - pub fn is_empty(&self) -> bool { - self.len() == 0 - } - - /// Returns true if the `Set` contains no elements. - #[cfg(not(feature = "std"))] + #[inline] pub const fn is_empty(&self) -> bool { self.len() == 0 } diff --git a/phf_codegen/src/lib.rs b/phf_codegen/src/lib.rs index 5d51a816..dfcf6ed0 100644 --- a/phf_codegen/src/lib.rs +++ b/phf_codegen/src/lib.rs @@ -222,8 +222,8 @@ impl<'a, K: FmtConst + 'a> fmt::Display for DisplayMap<'a, K> { f, "{}::Map {{ key: {:?}, - disps: {}::Slice::Static(&[", - self.path, self.state.key, self.path + disps: &[", + self.path, self.state.key )?; // write map displacements @@ -239,9 +239,8 @@ impl<'a, K: FmtConst + 'a> fmt::Display for DisplayMap<'a, K> { write!( f, " - ]), - entries: {}::Slice::Static(&[", - self.path + ], + entries: &[", )?; // write map entries @@ -258,7 +257,7 @@ impl<'a, K: FmtConst + 'a> fmt::Display for DisplayMap<'a, K> { write!( f, " - ]), + ], }}" ) } @@ -383,8 +382,8 @@ impl<'a, K: FmtConst + 'a> fmt::Display for DisplayOrderedMap<'a, K> { f, "{}::OrderedMap {{ key: {:?}, - disps: {}::Slice::Static(&[", - self.path, self.state.key, self.path + disps: &[", + self.path, self.state.key )?; for &(d1, d2) in &self.state.disps { write!( @@ -397,9 +396,8 @@ impl<'a, K: FmtConst + 'a> fmt::Display for DisplayOrderedMap<'a, K> { write!( f, " - ]), - idxs: {}::Slice::Static(&[", - self.path + ], + idxs: &[", )?; for &idx in &self.state.map { write!( @@ -412,9 +410,8 @@ impl<'a, K: FmtConst + 'a> fmt::Display for DisplayOrderedMap<'a, K> { write!( f, " - ]), - entries: {}::Slice::Static(&[", - self.path + ], + entries: &[", )?; for (key, value) in self.keys.iter().zip(self.values.iter()) { write!( @@ -428,7 +425,7 @@ impl<'a, K: FmtConst + 'a> fmt::Display for DisplayOrderedMap<'a, K> { write!( f, " - ]), + ], }}" ) } diff --git a/phf_macros/src/lib.rs b/phf_macros/src/lib.rs index bd277360..a7a3d703 100644 --- a/phf_macros/src/lib.rs +++ b/phf_macros/src/lib.rs @@ -257,8 +257,8 @@ fn build_map(entries: &[Entry], state: HashState) -> proc_macro2::TokenStream { quote! { phf::Map { key: #key, - disps: phf::Slice::Static(&[#(#disps),*]), - entries: phf::Slice::Static(&[#(#entries),*]), + disps: &[#(#disps),*], + entries: &[#(#entries),*], } } } @@ -276,9 +276,9 @@ fn build_ordered_map(entries: &[Entry], state: HashState) -> proc_macro2::TokenS quote! { phf::OrderedMap { key: #key, - disps: phf::Slice::Static(&[#(#disps),*]), - idxs: phf::Slice::Static(&[#(#idxs),*]), - entries: phf::Slice::Static(&[#(#entries),*]), + disps: &[#(#disps),*], + idxs: &[#(#idxs),*], + entries: &[#(#entries),*], } } }