Skip to content

Commit

Permalink
add len/is_empty const-fns
Browse files Browse the repository at this point in the history
  • Loading branch information
bhgomes committed Jul 23, 2021
1 parent c746106 commit f474922
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 0 deletions.
12 changes: 12 additions & 0 deletions phf/src/lib.rs
Expand Up @@ -138,6 +138,18 @@ pub enum Slice<T: 'static> {
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];

Expand Down
14 changes: 14 additions & 0 deletions phf/src/map.rs
Expand Up @@ -46,15 +46,29 @@ 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 true if the `Map` is empty.
#[cfg(not(feature = "std"))]
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
14 changes: 14 additions & 0 deletions phf/src/ordered_map.rs
Expand Up @@ -52,15 +52,29 @@ 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"))]
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"))]
pub const fn is_empty(&self) -> bool {
self.len() == 0
}

/// Returns a reference to the value that `key` maps to.
pub fn get<T: ?Sized>(&self, key: &T) -> Option<&V>
where
Expand Down
14 changes: 14 additions & 0 deletions phf/src/ordered_set.rs
Expand Up @@ -30,15 +30,29 @@ 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"))]
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"))]
pub const fn is_empty(&self) -> bool {
self.len() == 0
}

/// Returns a reference to the set's internal static instance of the given
/// key.
///
Expand Down
14 changes: 14 additions & 0 deletions phf/src/set.rs
Expand Up @@ -29,15 +29,29 @@ 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"))]
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"))]
pub const fn is_empty(&self) -> bool {
self.len() == 0
}

/// Returns a reference to the set's internal static instance of the given
/// key.
///
Expand Down

0 comments on commit f474922

Please sign in to comment.