diff options
author | zaaarf <me@zaaarf.foo> | 2024-05-17 02:23:56 +0200 |
---|---|---|
committer | zaaarf <me@zaaarf.foo> | 2024-05-17 02:23:56 +0200 |
commit | 6411923a0d668d0d5401b42f7e1573d9e77dd27c (patch) | |
tree | 283475e33583f8695294b1df7d22c705ce9908e0 | |
parent | 2c5304a0d592f99becec10f8a9283c403046d01a (diff) |
-rw-r--r-- | apb/src/node.rs | 23 |
1 files changed, 14 insertions, 9 deletions
diff --git a/apb/src/node.rs b/apb/src/node.rs index fd5f47c..b51eea9 100644 --- a/apb/src/node.rs +++ b/apb/src/node.rs @@ -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 // 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) -impl<T : super::Base + Clone> Iterator for Node<T> { +impl<T : super::Base> Iterator for Node<T> { type Item = T; fn next(&mut self) -> Option<Self::Item> { - let x = match self { - Self::Empty => return None, - Self::Link(_) => return None, - Self::Array(arr) => return arr.pop_front(), // TODO weird that we iter in reverse - Self::Object(x) => *x.clone(), // TODO needed because next() on object can't get value without owning - }; - *self = Self::Empty; - Some(x) + match std::mem::replace(self, Self::Empty) { + Self::Empty => None, + Self::Object(res) => Some(*res), + Self::Link(lnk) => { + *self = Self::Link(lnk); + None + }, + Self::Array(mut arr) => { + let res = arr.pop_front(); + *self = Self::Array(arr); + res + } + } } } |