Skip to content

Commit

Permalink
remove Slice type and fix some docs
Browse files Browse the repository at this point in the history
  • Loading branch information
bhgomes committed Jul 25, 2021
1 parent f474922 commit 99d3533
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 130 deletions.
37 changes: 0 additions & 37 deletions phf/src/lib.rs
Expand Up @@ -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)]
Expand All @@ -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<T: 'static> {
Static(&'static [T]),
#[cfg(feature = "std")]
Dynamic(Vec<T>),
}

// 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<T> Slice<T> {
pub(crate) const fn len(&self) -> usize {
match self {
Self::Static(slice) => slice.len(),
}
}
}

impl<T> Deref for Slice<T> {
type Target = [T];

fn deref(&self) -> &[T] {
match *self {
Slice::Static(t) => t,
#[cfg(feature = "std")]
Slice::Dynamic(ref t) => t,
}
}
}
29 changes: 8 additions & 21 deletions 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;
Expand All @@ -17,9 +16,9 @@ pub struct Map<K: 'static, V: 'static> {
#[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<K, V> fmt::Debug for Map<K, V>
Expand All @@ -45,30 +44,18 @@ where
}

impl<K, V> Map<K, V> {
/// 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<T: ?Sized>(&self, key: &T) -> bool
where
Expand Down Expand Up @@ -105,7 +92,7 @@ impl<K, V> Map<K, V> {
T: Eq + PhfHash,
K: PhfBorrow<T>,
{
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);
Expand Down
32 changes: 9 additions & 23 deletions phf/src/ordered_map.rs
Expand Up @@ -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
Expand All @@ -21,11 +19,11 @@ pub struct OrderedMap<K: 'static, V: 'static> {
#[doc(hidden)]
pub key: HashKey,
#[doc(hidden)]
pub disps: Slice<(u32, u32)>,
pub disps: &'static [(u32, u32)],
#[doc(hidden)]
pub idxs: Slice<usize>,
pub idxs: &'static [usize],
#[doc(hidden)]
pub entries: Slice<(K, V)>,
pub entries: &'static [(K, V)],
}

impl<K, V> fmt::Debug for OrderedMap<K, V>
Expand All @@ -51,26 +49,14 @@ where
}

impl<K, V> OrderedMap<K, V> {
/// 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
}
Expand All @@ -96,7 +82,7 @@ impl<K, V> OrderedMap<K, V> {
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<T: ?Sized>(&self, key: &T) -> bool
where
T: Eq + PhfHash,
Expand Down Expand Up @@ -135,7 +121,7 @@ impl<K, V> OrderedMap<K, V> {
T: Eq + PhfHash,
K: PhfBorrow<T>,
{
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);
Expand Down
18 changes: 3 additions & 15 deletions phf/src/ordered_set.rs
Expand Up @@ -30,25 +30,13 @@ where

impl<T> OrderedSet<T> {
/// 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
}
Expand Down Expand Up @@ -81,7 +69,7 @@ impl<T> OrderedSet<T> {
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<U: ?Sized>(&self, value: &U) -> bool
where
U: Eq + PhfHash,
Expand Down
16 changes: 2 additions & 14 deletions phf/src/set.rs
Expand Up @@ -29,25 +29,13 @@ where

impl<T> Set<T> {
/// 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
}
Expand Down
27 changes: 12 additions & 15 deletions phf_codegen/src/lib.rs
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -258,7 +257,7 @@ impl<'a, K: FmtConst + 'a> fmt::Display for DisplayMap<'a, K> {
write!(
f,
"
]),
],
}}"
)
}
Expand Down Expand Up @@ -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!(
Expand All @@ -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!(
Expand All @@ -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!(
Expand All @@ -428,7 +425,7 @@ impl<'a, K: FmtConst + 'a> fmt::Display for DisplayOrderedMap<'a, K> {
write!(
f,
"
]),
],
}}"
)
}
Expand Down
10 changes: 5 additions & 5 deletions phf_macros/src/lib.rs
Expand Up @@ -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),*],
}
}
}
Expand All @@ -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),*],
}
}
}
Expand Down

0 comments on commit 99d3533

Please sign in to comment.