use super::{super::*, IssuanceAccountIndexIter};
use std::mem;
enum State {
Root(AccountId),
Issuance(IssuanceAccountIndexIter),
Leaf(AccountId),
Done,
}
pub struct AccountIndexIter {
state: State,
}
impl AccountIndexIter {
#[inline]
pub(in super::super) fn new(id: AccountId) -> Self {
Self {
state: State::Root(id),
}
}
}
impl Iterator for AccountIndexIter {
type Item = AccountIndex;
fn next(&mut self) -> Option<Self::Item> {
match mem::replace(&mut self.state, State::Done) {
State::Root(id) => {
let index = id.root_account_index();
self.state = State::Issuance(id.issuance_account_indexes());
Some(index.into())
}
State::Issuance(mut iter) => match iter.next() {
Some(index) => {
self.state = State::Issuance(iter);
Some(index.into())
}
None => {
self.state = State::Leaf(iter.id());
self.next()
}
},
State::Leaf(id) => {
let index_opt = id.leaf_account_index();
index_opt.map(|index| index.into())
}
State::Done => None,
}
}
#[inline]
fn size_hint(&self) -> (usize, Option<usize>) {
let size = match &self.state {
State::Root(id) => {
1 + id.issuance_account_indexes().len()
+ id.leaf_account_index().map(|_| 1).unwrap_or(0)
}
State::Issuance(iter) => {
iter.len() + iter.id().leaf_account_index().map(|_| 1).unwrap_or(0)
}
State::Leaf(id) => id.leaf_account_index().map(|_| 1).unwrap_or(0),
State::Done => 0,
};
(size, Some(size))
}
}
impl ExactSizeIterator for AccountIndexIter {}
impl FusedIterator for AccountIndexIter {}