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 currently requires a nightly compiler.
11//!
12//! ```toml
13//! [dependencies]
14//! phf = { version = "0.7.24", features = ["macros"] }
15//! ```
16//!
17//! ```
18//! #![feature(proc_macro_hygiene)]
19//!
20//! use phf::{phf_map, phf_set};
21//!
22//! static MY_MAP: phf::Map<&'static str, u32> = phf_map! {
23//!     "hello" => 1,
24//!     "world" => 2,
25//! };
26//!
27//! static MY_SET: phf::Set<&'static str> = phf_set! {
28//!     "hello world",
29//!     "hola mundo",
30//! };
31//!
32//! fn main() {
33//!     assert_eq!(MY_MAP["hello"], 1);
34//!     assert!(MY_SET.contains("hello world"));
35//! }
36//! ```
37//!
38//! Alternatively, you can use the phf_codegen crate to generate PHF datatypes
39//! in a build script. This method can be used with a stable compiler.
40#![doc(html_root_url="https://docs.rs/phf/0.7")]
41#![warn(missing_docs)]
42#![cfg_attr(feature = "core", no_std)]
43
44#[cfg(not(feature = "core"))]
45extern crate std as core;
46
47extern crate phf_shared;
48#[cfg(feature = "macros")]
49extern crate phf_macros;
50
51#[cfg(feature = "macros")]
52pub use phf_macros::*;
53
54use core::ops::Deref;
55
56pub use phf_shared::PhfHash;
57#[doc(inline)]
58pub use map::Map;
59#[doc(inline)]
60pub use set::Set;
61#[doc(inline)]
62pub use ordered_map::OrderedMap;
63#[doc(inline)]
64pub use ordered_set::OrderedSet;
65
66pub mod map;
67pub mod set;
68pub mod ordered_map;
69pub mod ordered_set;
70
71// WARNING: this is not considered part of phf's public API and is subject to
72// change at any time.
73//
74// Basically Cow, but with the Owned version conditionally compiled
75#[doc(hidden)]
76pub enum Slice<T: 'static> {
77    Static(&'static [T]),
78    #[cfg(not(feature = "core"))]
79    Dynamic(Vec<T>),
80}
81
82impl<T> Deref for Slice<T> {
83    type Target = [T];
84
85    fn deref(&self) -> &[T] {
86        match *self {
87            Slice::Static(t) => t,
88            #[cfg(not(feature = "core"))]
89            Slice::Dynamic(ref t) => t,
90        }
91    }
92}