1 //! Rust-PHF is a library to generate efficient lookup tables at compile time using
2 //! [perfect hash functions](http://en.wikipedia.org/wiki/Perfect_hash_function).
4 //! It currently uses the
5 //! [CHD algorithm](http://cmph.sourceforge.net/papers/esa09.pdf) and can generate
6 //! a 100,000 entry map in roughly .4 seconds.
8 //! MSRV (minimum supported rust version) is Rust 1.60.
12 //! PHF data structures can be constructed via either the procedural
13 //! macros in the `phf_macros` crate or code generation supported by the
14 //! `phf_codegen` crate. If you prefer macros, you can easily use them by
15 //! enabling the `macros` feature of the `phf` crate, like:
19 //! phf = { version = "0.11", features = ["macros"] }
22 //! To compile the `phf` crate with a dependency on
23 //! libcore instead of libstd, enabling use in environments where libstd
24 //! will not work, set `default-features = false` for the dependency:
28 //! # to use `phf` in `no_std` environments
29 //! phf = { version = "0.11", default-features = false }
32 //! ## Example (with the `macros` feature enabled)
38 //! pub enum Keyword {
46 //! static KEYWORDS: phf::Map<&'static str, Keyword> = phf_map! {
47 //! "loop" => Keyword::Loop,
48 //! "continue" => Keyword::Continue,
49 //! "break" => Keyword::Break,
50 //! "fn" => Keyword::Fn,
51 //! "extern" => Keyword::Extern,
54 //! pub fn parse_keyword(keyword: &str) -> Option<Keyword> {
55 //! KEYWORDS.get(keyword).cloned()
59 //! Alternatively, you can use the [`phf_codegen`] crate to generate PHF datatypes
60 //! in a build script.
62 //! [`phf_codegen`]: https://docs.rs/phf_codegen
66 //! Currently, the macro syntax has some limitations and may not
67 //! work as you want. See [#183] or [#196] for example.
69 //! [#183]: https://github.com/rust-phf/rust-phf/issues/183
70 //! [#196]: https://github.com/rust-phf/rust-phf/issues/196
72 #![doc(html_root_url = "https://docs.rs/phf/0.11")]
73 #![warn(missing_docs)]
74 #![cfg_attr(not(feature = "std"), no_std)]
76 #[cfg(feature = "std")]
77 extern crate std as core;
79 #[cfg(feature = "macros")]
80 /// Macro to create a `static` (compile-time) [`Map`].
82 /// Requires the `macros` feature.
84 /// Supported key expressions are:
85 /// - literals: bools, (byte) strings, bytes, chars, and integers (these must have a type suffix)
86 /// - arrays of `u8` integers
87 /// - dereferenced byte string literals
88 /// - `UniCase::unicode(string)` or `UniCase::ascii(string)` if the `unicase` feature is enabled
93 /// use phf::{phf_map, Map};
95 /// static MY_MAP: Map<&'static str, u32> = phf_map! {
101 /// assert_eq!(MY_MAP["hello"], 1);
104 pub use phf_macros::phf_map;
106 #[cfg(feature = "macros")]
107 /// Macro to create a `static` (compile-time) [`OrderedMap`].
109 /// Requires the `macros` feature. Same usage as [`phf_map`].
110 pub use phf_macros::phf_ordered_map;
112 #[cfg(feature = "macros")]
113 /// Macro to create a `static` (compile-time) [`Set`].
115 /// Requires the `macros` feature.
120 /// use phf::{phf_set, Set};
122 /// static MY_SET: Set<&'static str> = phf_set! {
128 /// assert!(MY_SET.contains("hello world"));
131 pub use phf_macros::phf_set;
133 #[cfg(feature = "macros")]
134 /// Macro to create a `static` (compile-time) [`OrderedSet`].
136 /// Requires the `macros` feature. Same usage as [`phf_set`].
137 pub use phf_macros::phf_ordered_set;
140 pub use self::map::Map;
142 pub use self::ordered_map::OrderedMap;
144 pub use self::ordered_set::OrderedSet;
146 pub use self::set::Set;
147 pub use phf_shared::PhfHash;