1use crate::atom::Atom;
2
3pub struct AtomIter<'a> {
4 pub(crate) iter: Option<std::slice::Iter<'a, Atom>>,
5}
6
7impl<'a> AtomIter<'a> {
8 pub fn from_atom(atom_opt: Option<&'a Atom>) -> Self {
9 Self {
10 iter: atom_opt.map(|atom| atom.children.iter()),
11 }
12 }
13}
14
15impl<'a> Iterator for AtomIter<'a> {
16 type Item = &'a Atom;
17
18 fn next(&mut self) -> Option<Self::Item> {
19 self.iter.as_mut().and_then(std::iter::Iterator::next)
20 }
21}
22
23impl DoubleEndedIterator for AtomIter<'_> {
24 fn next_back(&mut self) -> Option<Self::Item> {
25 self.iter
26 .as_mut()
27 .and_then(std::iter::DoubleEndedIterator::next_back)
28 }
29}
30
31impl ExactSizeIterator for AtomIter<'_> {
32 fn len(&self) -> usize {
33 self.iter
34 .as_ref()
35 .map(ExactSizeIterator::len)
36 .unwrap_or_default()
37 }
38}
39
40pub struct AtomIterMut<'a> {
41 pub(crate) children: &'a mut [Atom],
42 pub(crate) index: usize,
43}
44
45impl<'a> AtomIterMut<'a> {
46 pub fn from_atom(atom: &'a mut Atom) -> Self {
47 Self {
48 children: &mut atom.children,
49 index: 0,
50 }
51 }
52}
53
54impl<'a> Iterator for AtomIterMut<'a> {
55 type Item = &'a mut Atom;
56
57 fn next(&mut self) -> Option<Self::Item> {
58 if self.index >= self.children.len() {
59 return None;
60 }
61
62 let children = std::mem::take(&mut self.children);
63 let (current, rest) = children.split_at_mut(self.index + 1);
64 self.children = rest;
65 let old_index = self.index;
66 self.index = 0;
67
68 current.get_mut(old_index)
69 }
70}