1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
//! Iterator over the components of an algebraic vector

use super::{Component, Vector};
use core::marker::PhantomData;

/// Iterator over the components of an algebraic vector
#[derive(Clone, Debug)]
pub struct Iter<'a, V, C>
where
    V: Vector<C>,
    C: Component,
{
    /// Reference to the original vector
    vector: &'a V,

    /// Iteration position within the vector
    position: usize,

    /// Component type
    component: PhantomData<C>,
}

impl<'a, V, C> Iter<'a, V, C>
where
    V: Vector<C>,
    C: Component,
{
    /// Create a new iterator over the vector's components
    pub(super) fn new(vector: &'a V) -> Self {
        Self {
            vector,
            position: 0,
            component: PhantomData,
        }
    }
}

impl<'a, V, C> Iterator for Iter<'a, V, C>
where
    V: Vector<C>,
    C: Component,
{
    type Item = C;

    fn next(&mut self) -> Option<C> {
        let item = self.vector.get(self.position);

        if item.is_some() {
            self.position += 1;
        }

        item
    }
}