Skip to content

Commit 796eda7

Browse files
authored
feat(bip0039): use phf_macros instead of phf_codegen (#151)
1 parent a63ac4e commit 796eda7

2 files changed

Lines changed: 8 additions & 17 deletions

File tree

bip0039/Cargo.toml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,15 +65,14 @@ spanish = []
6565
[dependencies]
6666
hmac = { version = "0.13", default-features = false }
6767
pbkdf2 = { version = "0.13", default-features = false }
68-
phf = { version = "0.13", default-features = false }
68+
phf = { version = "0.13", default-features = false, features = ["macros"] }
6969
rand = { version = "0.10", optional = true }
7070
sha2 = { version = "0.11", default-features = false }
7171
unicode-normalization = { version = "0.1", default-features = false }
7272
zeroize = { version = "1.8", default-features = false, features = ["alloc"] }
7373

7474
[build-dependencies]
7575
anyhow = "1.0"
76-
phf_codegen = "0.13"
7776

7877
[dev-dependencies]
7978
const-hex = "1.17"

bip0039/build.rs

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//! Build script to generate BIP-0039 wordlists and PHF indices from `words/*.txt`.
1+
//! Build script to generate BIP-0039 wordlists and PHF macro input from `words/*.txt`.
22
//!
33
//! This script generates one Rust source file per language into `OUT_DIR`:
44
//! - `bip0039_wordlist_<lang>.rs`
@@ -77,18 +77,6 @@ fn generate_one(out_dir: &Path, lang: &str, input_path: &Path) -> Result<()> {
7777
words.len()
7878
);
7979

80-
// Create PHF map (word -> index).
81-
//
82-
// `phf_codegen::Map::entry` stores borrowed `&str` values internally while we build it.
83-
// So we must ensure backing strings live long enough and are not moved/reallocated
84-
// during insertion. We do that by precomputing all value strings first, then inserting.
85-
let index_values: Vec<String> = (0..words.len()).map(|i| format!("{i}u16")).collect();
86-
87-
let mut index_map = phf_codegen::Map::new();
88-
for (w, v) in words.iter().zip(index_values.iter()) {
89-
index_map.entry(w, v.as_str());
90-
}
91-
9280
let out_name = format!("bip0039_wordlist_{lang}.rs");
9381
let out_path = out_dir.join(&out_name);
9482

@@ -113,8 +101,12 @@ fn generate_one(out_dir: &Path, lang: &str, input_path: &Path) -> Result<()> {
113101
writeln!(f, "];\n").context("failed writing WORDS footer")?;
114102

115103
// INDEX
116-
writeln!(f, "pub static INDEX: ::phf::Map<&'static str, u16> = {};\n", index_map.build())
117-
.context("failed writing INDEX")?;
104+
writeln!(f, "pub static INDEX: ::phf::Map<&'static str, u16> = ::phf::phf_map! {{")
105+
.context("failed writing INDEX header")?;
106+
for (i, w) in words.iter().enumerate() {
107+
writeln!(f, " {:?} => {i}u16,", w).context("failed writing an INDEX entry")?;
108+
}
109+
writeln!(f, "}};\n").context("failed writing INDEX footer")?;
118110

119111
// WORDLIST
120112
writeln!(

0 commit comments

Comments
 (0)