chore(apb): improved iterator impl for Node
This commit is contained in:
parent
2c5304a0d5
commit
6411923a0d
1 changed files with 14 additions and 9 deletions
|
@ -19,18 +19,23 @@ impl<T : super::Base> From<Option<T>> for Node<T> {
|
||||||
// TODO how do i move out of the box for a moment? i need to leave it uninitialized while i update
|
// TODO how do i move out of the box for a moment? i need to leave it uninitialized while i update
|
||||||
// the value and then put it back, i think it should be safe to do so! but i'm not sure how, so i'm
|
// the value and then put it back, i think it should be safe to do so! but i'm not sure how, so i'm
|
||||||
// using a clone (expensive but simple solution)
|
// using a clone (expensive but simple solution)
|
||||||
impl<T : super::Base + Clone> Iterator for Node<T> {
|
impl<T : super::Base> Iterator for Node<T> {
|
||||||
type Item = T;
|
type Item = T;
|
||||||
|
|
||||||
fn next(&mut self) -> Option<Self::Item> {
|
fn next(&mut self) -> Option<Self::Item> {
|
||||||
let x = match self {
|
match std::mem::replace(self, Self::Empty) {
|
||||||
Self::Empty => return None,
|
Self::Empty => None,
|
||||||
Self::Link(_) => return None,
|
Self::Object(res) => Some(*res),
|
||||||
Self::Array(arr) => return arr.pop_front(), // TODO weird that we iter in reverse
|
Self::Link(lnk) => {
|
||||||
Self::Object(x) => *x.clone(), // TODO needed because next() on object can't get value without owning
|
*self = Self::Link(lnk);
|
||||||
};
|
None
|
||||||
*self = Self::Empty;
|
},
|
||||||
Some(x)
|
Self::Array(mut arr) => {
|
||||||
|
let res = arr.pop_front();
|
||||||
|
*self = Self::Array(arr);
|
||||||
|
res
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue