keyfork-mnemonic-util: optimize Default::default() for Wordlist
This commit is contained in:
parent
883e0cdf65
commit
31e51f65a5
|
@ -48,19 +48,26 @@ impl Error for MnemonicGenerationError {}
|
|||
#[derive(Debug, Clone)]
|
||||
pub struct Wordlist(Vec<String>);
|
||||
|
||||
static ENGLISH: OnceLock<Wordlist> = OnceLock::new();
|
||||
|
||||
impl Default for Wordlist {
|
||||
/// Returns the English wordlist in the Bitcoin BIP-0039 specification.
|
||||
fn default() -> Self {
|
||||
// TODO: English is the only supported language.
|
||||
let wordlist_file = include_str!("data/wordlist.txt");
|
||||
Wordlist(
|
||||
wordlist_file
|
||||
.lines()
|
||||
// skip 1: comment at top of file to point to BIP-0039 source.
|
||||
.skip(1)
|
||||
.map(|x| x.trim().to_string())
|
||||
.collect(),
|
||||
)
|
||||
ENGLISH
|
||||
.get_or_init(|| {
|
||||
let wordlist_file = include_str!("data/wordlist.txt");
|
||||
Wordlist(
|
||||
wordlist_file
|
||||
.lines()
|
||||
// skip 1: comment at top of file to point to BIP-0039 source.
|
||||
.skip(1)
|
||||
.map(|x| x.trim().to_string())
|
||||
.collect(),
|
||||
)
|
||||
.shrank()
|
||||
})
|
||||
.clone()
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -71,6 +78,12 @@ impl Wordlist {
|
|||
Arc::new(self)
|
||||
}
|
||||
|
||||
/// Return a shrank version of the Wordlist
|
||||
pub fn shrank(mut self) -> Self {
|
||||
self.0.shrink_to_fit();
|
||||
self
|
||||
}
|
||||
|
||||
/// Determine whether the Wordlist contains a given word.
|
||||
pub fn contains(&self, word: &str) -> bool {
|
||||
self.0.iter().any(|w| w.as_str() == word)
|
||||
|
|
Loading…
Reference in New Issue