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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
use crate::{
    iter_raw::{iter_with_raw, IterRaw, IterRawAdapter},
    Slice, Soa, SoaRaw, Soars,
};
use std::{
    fmt::Debug,
    iter::FusedIterator,
    mem::{needs_drop, size_of},
};

/// An iterator that moves out of a [`Soa`].
///
/// This struct is created by the [`into_iter`] method, provided by the
/// [`IntoIterator`] trait.
///
/// [`Soa`]: crate::Soa
/// [`into_iter`]: crate::Soa::into_iter
pub struct IntoIter<T>
where
    T: Soars,
{
    pub(crate) iter_raw: IterRaw<T, Self>,
    pub(crate) ptr: *mut u8,
    pub(crate) cap: usize,
}

impl<T> IterRawAdapter<T> for IntoIter<T>
where
    T: Soars,
{
    type Item = T;

    fn item_from_raw(raw: T::Raw) -> Self::Item {
        unsafe { raw.get() }
    }
}

impl<T> Default for IntoIter<T>
where
    T: Soars,
{
    fn default() -> Self {
        Soa::<T>::new().into_iter()
    }
}

impl<T> Debug for IntoIter<T>
where
    T: Soars,
    for<'a> T::Ref<'a>: Debug,
{
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{:?}", self.as_slice())
    }
}

impl<T> IntoIter<T>
where
    T: Soars,
{
    /// Returns an immutable slice of all elements that have not been yielded
    /// yet.
    pub fn as_slice(&self) -> &Slice<T> {
        unsafe { self.iter_raw.as_slice() }
    }

    /// Returns a mutable slice of all elements that have not been yielded yet.
    pub fn as_mut_slice(&mut self) -> &mut Slice<T> {
        unsafe { self.iter_raw.as_mut_slice() }
    }
}

impl<T> Drop for IntoIter<T>
where
    T: Soars,
{
    fn drop(&mut self) {
        if needs_drop::<T>() {
            for _ in self.by_ref() {}
        }

        if size_of::<T>() > 0 && self.cap > 0 {
            unsafe { <T::Raw as SoaRaw>::from_parts(self.ptr, self.cap).dealloc(self.cap) }
        }
    }
}

iter_with_raw!(IntoIter<T>);