use crate::tree::NodeRef;
#[derive(Debug, Clone)]
pub(super) struct State<T> {
pub(super) next: T,
pub(super) next_back: T,
}
#[derive(Debug, Clone)]
pub struct Siblings(pub(super) Option<State<NodeRef>>);
macro_rules! siblings_next {
($next: ident, $next_back: ident, $next_sibling: ident) => {
fn $next(&mut self) -> Option<NodeRef> {
#![allow(non_shorthand_field_patterns)]
self.0.take().map(
|State {
$next: next,
$next_back: next_back,
}| {
if let Some(sibling) = next.$next_sibling() {
if next != next_back {
self.0 = Some(State {
$next: sibling,
$next_back: next_back,
})
}
}
next
},
)
}
};
}
impl Iterator for Siblings {
type Item = NodeRef;
siblings_next!(next, next_back, next_sibling);
}
impl DoubleEndedIterator for Siblings {
siblings_next!(next_back, next, previous_sibling);
}