orx_split_vec/common_traits/iterator/
iter_mut.rs1use crate::{Growth, SplitVec, fragment::fragment_struct::Fragment};
2use core::iter::FusedIterator;
3
4impl<'a, T, G: Growth> IntoIterator for &'a mut SplitVec<T, G> {
5 type Item = &'a mut T;
6 type IntoIter = IterMut<'a, T>;
7
8 fn into_iter(self) -> Self::IntoIter {
9 Self::IntoIter::new(&mut self.fragments)
10 }
11}
12
13#[derive(Debug)]
17#[must_use = "iterators are lazy and do nothing unless consumed"]
18pub struct IterMut<'a, T> {
19 iter_outer: core::slice::IterMut<'a, Fragment<T>>,
20 iter_inner: core::slice::IterMut<'a, T>,
21}
22
23impl<'a, T> IterMut<'a, T> {
24 pub(crate) fn new(fragments: &'a mut [Fragment<T>]) -> Self {
25 let mut iter_outer = fragments.iter_mut();
26 let iter_inner = iter_outer
27 .next()
28 .map(|x| x.iter_mut())
29 .unwrap_or([].iter_mut());
30 Self {
31 iter_outer,
32 iter_inner,
33 }
34 }
35
36 fn next_fragment(&mut self) -> Option<&'a mut T> {
37 match self.iter_outer.next() {
38 Some(f) => {
39 self.iter_inner = f.iter_mut();
40 self.next()
41 }
42 None => None,
43 }
44 }
45}
46
47impl<T> FusedIterator for IterMut<'_, T> {}
48
49impl<'a, T> Iterator for IterMut<'a, T> {
50 type Item = &'a mut T;
51
52 #[inline(always)]
53 fn next(&mut self) -> Option<Self::Item> {
54 let next_element = self.iter_inner.next();
55 if next_element.is_some() {
56 next_element
57 } else {
58 self.next_fragment()
59 }
60 }
61}