diff --git a/phf/src/lib.rs b/phf/src/lib.rs index 01830a4c..35a146cb 100644 --- a/phf/src/lib.rs +++ b/phf/src/lib.rs @@ -138,6 +138,18 @@ pub enum Slice { 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]; diff --git a/phf/src/map.rs b/phf/src/map.rs index 3df11b3c..1140f3d9 100644 --- a/phf/src/map.rs +++ b/phf/src/map.rs @@ -46,15 +46,29 @@ where impl Map { /// 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(&self, key: &T) -> bool where diff --git a/phf/src/ordered_map.rs b/phf/src/ordered_map.rs index de888aef..c2066d3b 100644 --- a/phf/src/ordered_map.rs +++ b/phf/src/ordered_map.rs @@ -52,15 +52,29 @@ 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"))] + 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(&self, key: &T) -> Option<&V> where diff --git a/phf/src/ordered_set.rs b/phf/src/ordered_set.rs index d1e93293..35163da0 100644 --- a/phf/src/ordered_set.rs +++ b/phf/src/ordered_set.rs @@ -30,15 +30,29 @@ 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"))] + 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. /// diff --git a/phf/src/set.rs b/phf/src/set.rs index 1a3f1acc..64394d59 100644 --- a/phf/src/set.rs +++ b/phf/src/set.rs @@ -29,15 +29,29 @@ 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"))] + 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. ///