Debian Codegraph @

debian/rust-vendor/phf/README.md

raw
1 # Rust-PHF
2
3 [![CI](https://github.com/rust-phf/rust-phf/actions/workflows/ci.yml/badge.svg)](https://github.com/rust-phf/rust-phf/actions/workflows/ci.yml) [![Latest Version](https://img.shields.io/crates/v/phf.svg)](https://crates.io/crates/phf)
4
5 [Documentation](https://docs.rs/phf)
6
7 Rust-PHF is a library to generate efficient lookup tables at compile time using
8 [perfect hash functions](http://en.wikipedia.org/wiki/Perfect_hash_function).
9
10 It currently uses the
11 [CHD algorithm](http://cmph.sourceforge.net/papers/esa09.pdf) by default and
12 also ships an experimental `ptrhash` feature for an alternative MPHF layout.
13
14 MSRV (minimum supported rust version) is Rust 1.85.
15
16 ## Usage
17
18 PHF data structures can be constructed via either the procedural
19 macros in the `phf_macros` crate or code generation supported by the
20 `phf_codegen` crate.
21
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:
25
26 ```toml
27 [dependencies]
28 # to use `phf` in `no_std` environments
29 phf = { version = "0.14.0", default-features = false }
30 ```
31
32 ### phf_macros
33
34 ```rust
35 use phf::phf_map;
36
37 #[derive(Clone)]
38 pub enum Keyword {
39 Loop,
40 Continue,
41 Break,
42 Fn,
43 Extern,
44 }
45
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,
52 };
53
54 // You can also use OR (`|`) patterns to map multiple keys to the same value:
55 static OPERATORS: phf::Map<&'static str, &'static str> = phf_map! {
56 "+" | "add" | "plus" => "addition",
57 "-" | "sub" | "minus" => "subtraction",
58 "*" | "mul" | "times" => "multiplication",
59 };
60
61 pub fn parse_keyword(keyword: &str) -> Option<Keyword> {
62 KEYWORDS.get(keyword).cloned()
63 }
64
65 pub fn parse_operator(operator: &str) -> Option<&'static str> {
66 OPERATORS.get(operator).copied()
67 }
68 ```
69
70 ```toml
71 [dependencies]
72 phf = { version = "0.14.0", features = ["macros"] }
73 ```
74
75 To try the experimental alternative, enable `ptrhash` on both the generator and
76 runtime crates you use:
77
78 ```toml
79 [dependencies]
80 phf = { version = "0.14.0", features = ["macros", "ptrhash"] }
81 ```
82
83 #### Note
84
85 Currently, the macro syntax has some limitations and may not
86 work as you want. See [#196] for example.
87 Integer literals in the first key's type shape must use explicit suffixes, such
88 as `0u32`, `[0u8, 1]`, or `(0u32, 1u32)`; later keys infer unsuffixed integer
89 literals from the same position in that first key.
90 Tuple keys are supported up to 12 elements.
91 All keys must use the same supported key expression type as the first key.
92
93 [#196]: https://github.com/rust-phf/rust-phf/issues/196
94
95 ### phf_codegen
96
97 To use `phf_codegen` on build.rs, you have to add dependencies under `[build-dependencies]`:
98
99 ```toml
100 [build-dependencies]
101 phf = { version = "0.14.0", default-features = false }
102 phf_codegen = "0.14.0"
103 ```
104
105 When using the experimental `ptrhash` layout, enable the `ptrhash` feature on
106 both `phf_codegen` and the runtime `phf` dependency so the generated constants
107 match the runtime struct layout.
108
109 Then put code on build.rs:
110
111 ```rust
112 use std::env;
113 use std::fs::File;
114 use std::io::{BufWriter, Write};
115 use std::path::Path;
116
117 fn main() {
118 let path = Path::new(&env::var("OUT_DIR").unwrap()).join("codegen.rs");
119 let mut file = BufWriter::new(File::create(&path).unwrap());
120
121 write!(
122 &mut file,
123 "static KEYWORDS: phf::Map<&'static str, Keyword> = {}",
124 phf_codegen::Map::new()
125 .entry("loop", "Keyword::Loop")
126 .entry("continue", "Keyword::Continue")
127 .entry("break", "Keyword::Break")
128 .entry("fn", "Keyword::Fn")
129 .entry("extern", "Keyword::Extern")
130 .build()
131 )
132 .unwrap();
133 write!(&mut file, ";\n").unwrap();
134
135 // Example with OR patterns (note: phf_codegen doesn't support OR patterns directly)
136 write!(
137 &mut file,
138 "static OPERATORS: phf::Map<&'static str, &'static str> = {}",
139 phf_codegen::Map::new()
140 .entry("+", "\"addition\"")
141 .entry("add", "\"addition\"")
142 .entry("plus", "\"addition\"")
143 .entry("-", "\"subtraction\"")
144 .entry("sub", "\"subtraction\"")
145 .entry("minus", "\"subtraction\"")
146 .build()
147 )
148 .unwrap();
149 write!(&mut file, ";\n").unwrap();
150 }
151 ```
152
153 and lib.rs:
154
155 ```rust
156 #[derive(Clone)]
157 enum Keyword {
158 Loop,
159 Continue,
160 Break,
161 Fn,
162 Extern,
163 }
164
165 include!(concat!(env!("OUT_DIR"), "/codegen.rs"));
166
167 pub fn parse_keyword(keyword: &str) -> Option<Keyword> {
168 KEYWORDS.get(keyword).cloned()
169 }
170
171 pub fn parse_operator(operator: &str) -> Option<&'static str> {
172 OPERATORS.get(operator).copied()
173 }
174 ```