Skip to content

Commit 65deaf7

Browse files
authored
Merge pull request #224 from bhgomes/const-fns
2 parents c746106 + 99d3533 commit 65deaf7

7 files changed

Lines changed: 48 additions & 71 deletions

File tree

‎phf/src/lib.rs‎

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -110,8 +110,6 @@ pub use phf_macros::phf_set;
110110
#[proc_macro_hack::proc_macro_hack]
111111
pub use phf_macros::phf_ordered_set;
112112

113-
use core::ops::Deref;
114-
115113
#[doc(inline)]
116114
pub use self::map::Map;
117115
#[doc(inline)]
@@ -126,26 +124,3 @@ pub mod map;
126124
pub mod ordered_map;
127125
pub mod ordered_set;
128126
pub mod set;
129-
130-
// WARNING: this is not considered part of phf's public API and is subject to
131-
// change at any time.
132-
//
133-
// Basically Cow, but with the Owned version conditionally compiled.
134-
#[doc(hidden)]
135-
pub enum Slice<T: 'static> {
136-
Static(&'static [T]),
137-
#[cfg(feature = "std")]
138-
Dynamic(Vec<T>),
139-
}
140-
141-
impl<T> Deref for Slice<T> {
142-
type Target = [T];
143-
144-
fn deref(&self) -> &[T] {
145-
match *self {
146-
Slice::Static(t) => t,
147-
#[cfg(feature = "std")]
148-
Slice::Dynamic(ref t) => t,
149-
}
150-
}
151-
}

‎phf/src/map.rs‎

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
//! An immutable map constructed at compile time.
2-
use crate::Slice;
32
use core::fmt;
43
use core::iter::IntoIterator;
54
use core::ops::Index;
@@ -17,9 +16,9 @@ pub struct Map<K: 'static, V: 'static> {
1716
#[doc(hidden)]
1817
pub key: HashKey,
1918
#[doc(hidden)]
20-
pub disps: Slice<(u32, u32)>,
19+
pub disps: &'static [(u32, u32)],
2120
#[doc(hidden)]
22-
pub entries: Slice<(K, V)>,
21+
pub entries: &'static [(K, V)],
2322
}
2423

2524
impl<K, V> fmt::Debug for Map<K, V>
@@ -45,16 +44,18 @@ where
4544
}
4645

4746
impl<K, V> Map<K, V> {
48-
/// Returns true if the `Map` is empty.
49-
pub fn is_empty(&self) -> bool {
50-
self.len() == 0
51-
}
52-
5347
/// Returns the number of entries in the `Map`.
54-
pub fn len(&self) -> usize {
48+
#[inline]
49+
pub const fn len(&self) -> usize {
5550
self.entries.len()
5651
}
5752

53+
/// Returns true if the `Map` is empty.
54+
#[inline]
55+
pub const fn is_empty(&self) -> bool {
56+
self.len() == 0
57+
}
58+
5859
/// Determines if `key` is in the `Map`.
5960
pub fn contains_key<T: ?Sized>(&self, key: &T) -> bool
6061
where
@@ -91,7 +92,7 @@ impl<K, V> Map<K, V> {
9192
T: Eq + PhfHash,
9293
K: PhfBorrow<T>,
9394
{
94-
if self.disps.len() == 0 {
95+
if self.disps.is_empty() {
9596
return None;
9697
} //Prevent panic on empty map
9798
let hashes = phf_shared::hash(key, &self.key);

‎phf/src/ordered_map.rs‎

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@ use core::ops::Index;
55
use core::slice;
66
use phf_shared::{self, HashKey, PhfBorrow, PhfHash};
77

8-
use crate::Slice;
9-
108
/// An order-preserving immutable map constructed at compile time.
119
///
1210
/// Unlike a `Map`, iteration order is guaranteed to match the definition
@@ -21,11 +19,11 @@ pub struct OrderedMap<K: 'static, V: 'static> {
2119
#[doc(hidden)]
2220
pub key: HashKey,
2321
#[doc(hidden)]
24-
pub disps: Slice<(u32, u32)>,
22+
pub disps: &'static [(u32, u32)],
2523
#[doc(hidden)]
26-
pub idxs: Slice<usize>,
24+
pub idxs: &'static [usize],
2725
#[doc(hidden)]
28-
pub entries: Slice<(K, V)>,
26+
pub entries: &'static [(K, V)],
2927
}
3028

3129
impl<K, V> fmt::Debug for OrderedMap<K, V>
@@ -51,13 +49,15 @@ where
5149
}
5250

5351
impl<K, V> OrderedMap<K, V> {
54-
/// Returns the number of entries in the `Map`.
55-
pub fn len(&self) -> usize {
52+
/// Returns the number of entries in the `OrderedMap`.
53+
#[inline]
54+
pub const fn len(&self) -> usize {
5655
self.entries.len()
5756
}
5857

59-
/// Returns true if the `Map` is empty.
60-
pub fn is_empty(&self) -> bool {
58+
/// Returns true if the `OrderedMap` is empty.
59+
#[inline]
60+
pub const fn is_empty(&self) -> bool {
6161
self.len() == 0
6262
}
6363

@@ -82,7 +82,7 @@ impl<K, V> OrderedMap<K, V> {
8282
self.get_entry(key).map(|e| e.0)
8383
}
8484

85-
/// Determines if `key` is in the `Map`.
85+
/// Determines if `key` is in the `OrderedMap`.
8686
pub fn contains_key<T: ?Sized>(&self, key: &T) -> bool
8787
where
8888
T: Eq + PhfHash,
@@ -121,7 +121,7 @@ impl<K, V> OrderedMap<K, V> {
121121
T: Eq + PhfHash,
122122
K: PhfBorrow<T>,
123123
{
124-
if self.disps.len() == 0 {
124+
if self.disps.is_empty() {
125125
return None;
126126
} //Prevent panic on empty map
127127
let hashes = phf_shared::hash(key, &self.key);

‎phf/src/ordered_set.rs‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,14 @@ where
3030

3131
impl<T> OrderedSet<T> {
3232
/// Returns the number of elements in the `OrderedSet`.
33-
pub fn len(&self) -> usize {
33+
#[inline]
34+
pub const fn len(&self) -> usize {
3435
self.map.len()
3536
}
3637

3738
/// Returns true if the `OrderedSet` contains no elements.
38-
pub fn is_empty(&self) -> bool {
39+
#[inline]
40+
pub const fn is_empty(&self) -> bool {
3941
self.len() == 0
4042
}
4143

@@ -67,7 +69,7 @@ impl<T> OrderedSet<T> {
6769
self.map.index(index).map(|(k, &())| k)
6870
}
6971

70-
/// Returns true if `value` is in the `Set`.
72+
/// Returns true if `value` is in the `OrderedSet`.
7173
pub fn contains<U: ?Sized>(&self, value: &U) -> bool
7274
where
7375
U: Eq + PhfHash,

‎phf/src/set.rs‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,14 @@ where
2929

3030
impl<T> Set<T> {
3131
/// Returns the number of elements in the `Set`.
32-
pub fn len(&self) -> usize {
32+
#[inline]
33+
pub const fn len(&self) -> usize {
3334
self.map.len()
3435
}
3536

3637
/// Returns true if the `Set` contains no elements.
37-
pub fn is_empty(&self) -> bool {
38+
#[inline]
39+
pub const fn is_empty(&self) -> bool {
3840
self.len() == 0
3941
}
4042

‎phf_codegen/src/lib.rs‎

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -222,8 +222,8 @@ impl<'a, K: FmtConst + 'a> fmt::Display for DisplayMap<'a, K> {
222222
f,
223223
"{}::Map {{
224224
key: {:?},
225-
disps: {}::Slice::Static(&[",
226-
self.path, self.state.key, self.path
225+
disps: &[",
226+
self.path, self.state.key
227227
)?;
228228

229229
// write map displacements
@@ -239,9 +239,8 @@ impl<'a, K: FmtConst + 'a> fmt::Display for DisplayMap<'a, K> {
239239
write!(
240240
f,
241241
"
242-
]),
243-
entries: {}::Slice::Static(&[",
244-
self.path
242+
],
243+
entries: &[",
245244
)?;
246245

247246
// write map entries
@@ -258,7 +257,7 @@ impl<'a, K: FmtConst + 'a> fmt::Display for DisplayMap<'a, K> {
258257
write!(
259258
f,
260259
"
261-
]),
260+
],
262261
}}"
263262
)
264263
}
@@ -383,8 +382,8 @@ impl<'a, K: FmtConst + 'a> fmt::Display for DisplayOrderedMap<'a, K> {
383382
f,
384383
"{}::OrderedMap {{
385384
key: {:?},
386-
disps: {}::Slice::Static(&[",
387-
self.path, self.state.key, self.path
385+
disps: &[",
386+
self.path, self.state.key
388387
)?;
389388
for &(d1, d2) in &self.state.disps {
390389
write!(
@@ -397,9 +396,8 @@ impl<'a, K: FmtConst + 'a> fmt::Display for DisplayOrderedMap<'a, K> {
397396
write!(
398397
f,
399398
"
400-
]),
401-
idxs: {}::Slice::Static(&[",
402-
self.path
399+
],
400+
idxs: &[",
403401
)?;
404402
for &idx in &self.state.map {
405403
write!(
@@ -412,9 +410,8 @@ impl<'a, K: FmtConst + 'a> fmt::Display for DisplayOrderedMap<'a, K> {
412410
write!(
413411
f,
414412
"
415-
]),
416-
entries: {}::Slice::Static(&[",
417-
self.path
413+
],
414+
entries: &[",
418415
)?;
419416
for (key, value) in self.keys.iter().zip(self.values.iter()) {
420417
write!(
@@ -428,7 +425,7 @@ impl<'a, K: FmtConst + 'a> fmt::Display for DisplayOrderedMap<'a, K> {
428425
write!(
429426
f,
430427
"
431-
]),
428+
],
432429
}}"
433430
)
434431
}

‎phf_macros/src/lib.rs‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -257,8 +257,8 @@ fn build_map(entries: &[Entry], state: HashState) -> proc_macro2::TokenStream {
257257
quote! {
258258
phf::Map {
259259
key: #key,
260-
disps: phf::Slice::Static(&[#(#disps),*]),
261-
entries: phf::Slice::Static(&[#(#entries),*]),
260+
disps: &[#(#disps),*],
261+
entries: &[#(#entries),*],
262262
}
263263
}
264264
}
@@ -276,9 +276,9 @@ fn build_ordered_map(entries: &[Entry], state: HashState) -> proc_macro2::TokenS
276276
quote! {
277277
phf::OrderedMap {
278278
key: #key,
279-
disps: phf::Slice::Static(&[#(#disps),*]),
280-
idxs: phf::Slice::Static(&[#(#idxs),*]),
281-
entries: phf::Slice::Static(&[#(#entries),*]),
279+
disps: &[#(#disps),*],
280+
idxs: &[#(#idxs),*],
281+
entries: &[#(#entries),*],
282282
}
283283
}
284284
}

0 commit comments

Comments
 (0)