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

Remove Slice and upgrade len and is_empty to const-fns #224

Merged
merged 2 commits into from
Jul 26, 2021
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
25 changes: 0 additions & 25 deletions phf/src/lib.rs
Original file line number Diff line number Diff line change
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,26 +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>),
}

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,
}
}
}
21 changes: 11 additions & 10 deletions phf/src/map.rs
Original file line number Diff line number Diff line change
@@ -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,16 +44,18 @@ where
}

impl<K, V> Map<K, V> {
/// Returns true if the `Map` is empty.
pub fn is_empty(&self) -> bool {
self.len() == 0
}

/// Returns the number of entries in the `Map`.
pub fn len(&self) -> usize {
#[inline]
pub const fn len(&self) -> usize {
self.entries.len()
}

/// Returns true if the `Map` is empty.
#[inline]
pub const fn is_empty(&self) -> bool {
self.len() == 0
}

/// Determines if `key` is in the `Map`.
pub fn contains_key<T: ?Sized>(&self, key: &T) -> bool
where
Expand Down Expand Up @@ -91,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
22 changes: 11 additions & 11 deletions phf/src/ordered_map.rs
Original file line number Diff line number Diff line change
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,13 +49,15 @@ where
}

impl<K, V> OrderedMap<K, V> {
/// Returns the number of entries in the `Map`.
pub fn len(&self) -> usize {
/// 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.
pub fn is_empty(&self) -> bool {
/// Returns true if the `OrderedMap` is empty.
#[inline]
pub const fn is_empty(&self) -> bool {
self.len() == 0
}

Expand All @@ -82,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 @@ -121,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
8 changes: 5 additions & 3 deletions phf/src/ordered_set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,14 @@ where

impl<T> OrderedSet<T> {
/// Returns the number of elements in the `OrderedSet`.
pub fn len(&self) -> usize {
#[inline]
pub const fn len(&self) -> usize {
self.map.len()
}

/// Returns true if the `OrderedSet` contains no elements.
pub fn is_empty(&self) -> bool {
#[inline]
pub const fn is_empty(&self) -> bool {
self.len() == 0
}

Expand Down Expand Up @@ -67,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
6 changes: 4 additions & 2 deletions phf/src/set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,14 @@ where

impl<T> Set<T> {
/// Returns the number of elements in the `Set`.
pub fn len(&self) -> usize {
#[inline]
pub const fn len(&self) -> usize {
self.map.len()
}

/// Returns true if the `Set` contains no elements.
pub fn is_empty(&self) -> bool {
#[inline]
pub const fn is_empty(&self) -> bool {
self.len() == 0
}

Expand Down
27 changes: 12 additions & 15 deletions phf_codegen/src/lib.rs
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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