Skip to content

Commit

Permalink
add bip32 word list iterator
Browse files Browse the repository at this point in the history
  • Loading branch information
aspect committed Sep 20, 2023
1 parent 59e965f commit c1a40b1
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions wallet/bip32/src/mnemonic/language.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,29 @@ impl WordList {
pub fn get_word(&self, bits: Bits11) -> &'static str {
self.inner[bits.bits() as usize]
}

pub fn iter(&self) -> WordListIterator<'_> {
WordListIterator { wordlist: self, index: 0 }
}
}

pub struct WordListIterator<'a> {
wordlist: &'a WordList,
index: usize,
}

impl Iterator for WordListIterator<'_> {
type Item = &'static str;

fn next(&mut self) -> Option<Self::Item> {
if self.index < self.wordlist.inner.len() {
let word = self.wordlist.inner[self.index];
self.index += 1;
Some(word)
} else {
None
}
}
}

// TODO(tarcieri): use `const fn` instead of `Lazy`
Expand Down

0 comments on commit c1a40b1

Please sign in to comment.