phf/
lib.rs

1//! Compile-time generated maps and sets.
2//!
3//! The `phf::Map` and `phf::Set` types have roughly comparable performance to
4//! a standard hash table, but can be generated as compile-time static values.
5//!
6//! # Usage
7//!
8//! If the `macros` Cargo feature is enabled, the `phf_map`, `phf_set`,
9//! `phf_ordered_map`, and `phf_ordered_set` macros can be used to construct
10//! the PHF type. This method can be used with a stable compiler
11//! (`rustc` version 1.30+)
12//!
13//! ```toml
14//! [dependencies]
15//! phf = { version = "0.7.24", features = ["macros"] }
16//! ```
17//!
18//! ```
19//! use phf::{phf_map, phf_set};
20//!
21//! static MY_MAP: phf::Map<&'static str, u32> = phf_map! {
22//!     "hello" => 1,
23//!     "world" => 2,
24//! };
25//!
26//! static MY_SET: phf::Set<&'static str> = phf_set! {
27//!     "hello world",
28//!     "hola mundo",
29//! };
30//!
31//! fn main() {
32//!     assert_eq!(MY_MAP["hello"], 1);
33//!     assert!(MY_SET.contains("hello world"));
34//! }
35//! ```
36//!
37//! (Alternatively, you can use the phf_codegen crate to generate PHF datatypes
38//! in a build script)
39#![doc(html_root_url="https://docs.rs/phf/0.7")]
40#![warn(missing_docs)]
41#![cfg_attr(not(feature = "std"), no_std)]
42
43#[cfg(feature = "std")]
44extern crate std as core;
45
46#[cfg(feature = "macros")]
47/// Macro to create a `static` (compile-time) [`Map`].
48///
49/// Requires the `"macros"` feature.
50///
51/// # Example
52///
53/// ```rust,edition2018
54/// use ::phf::{phf_map, Map};
55///
56/// static MY_MAP: Map<&'static str, u32> = phf_map! {
57///     "hello" => 1,
58///     "world" => 2,
59/// };
60///
61/// fn main ()
62/// {
63///     assert_eq!(MY_MAP["hello"], 1);
64/// }
65/// ```
66#[::proc_macro_hack::proc_macro_hack]
67pub use phf_macros:: phf_map;
68
69#[cfg(feature = "macros")]
70/// Macro to create a `static` (compile-time) [`Set`].
71///
72/// Requires the `"macros"` feature.
73///
74/// # Example
75///
76/// ```rust,edition2018
77/// use ::phf::{phf_set, Set};
78///
79/// static MY_SET: Set<&'static str> = phf_set! {
80///     "hello world",
81///     "hola mundo",
82/// };
83///
84/// fn main ()
85/// {
86///     assert!(MY_SET.contains("hello world"));
87/// }
88/// ```
89#[::proc_macro_hack::proc_macro_hack]
90pub use phf_macros::phf_set;
91
92use core::ops::Deref;
93
94pub use phf_shared::PhfHash;
95#[doc(inline)]
96pub use self::map::Map;
97#[doc(inline)]
98pub use self::set::Set;
99
100pub mod map;
101pub mod set;
102
103// WARNING: this is not considered part of phf's public API and is subject to
104// change at any time.
105//
106// Basically Cow, but with the Owned version conditionally compiled
107#[doc(hidden)]
108pub enum Slice<T: 'static> {
109    Static(&'static [T]),
110    #[cfg(feature = "std")]
111    Dynamic(Vec<T>),
112}
113
114impl<T> Deref for Slice<T> {
115    type Target = [T];
116
117    fn deref(&self) -> &[T] {
118        match *self {
119            Slice::Static(t) => t,
120            #[cfg(feature = "std")]
121            Slice::Dynamic(ref t) => t,
122        }
123    }
124}