soa_rs/
into_iter.rs

1use crate::{
2    iter_raw::{iter_with_raw, IterRaw, IterRawAdapter},
3    Slice, Soa, SoaRaw, Soars,
4};
5use std::{
6    fmt::Debug,
7    iter::FusedIterator,
8    mem::{needs_drop, size_of},
9    ptr::NonNull,
10};
11
12/// An iterator that moves out of a [`Soa`].
13///
14/// This struct is created by the [`into_iter`] method, provided by the
15/// [`IntoIterator`] trait.
16///
17/// [`Soa`]: crate::Soa
18/// [`into_iter`]: crate::Soa::into_iter
19pub struct IntoIter<T>
20where
21    T: Soars,
22{
23    pub(crate) iter_raw: IterRaw<T, Self>,
24    pub(crate) ptr: NonNull<u8>,
25    pub(crate) cap: usize,
26}
27
28impl<T> IterRawAdapter<T> for IntoIter<T>
29where
30    T: Soars,
31{
32    type Item = T;
33
34    unsafe fn item_from_raw(raw: T::Raw) -> Self::Item {
35        unsafe { raw.get() }
36    }
37}
38
39impl<T> Default for IntoIter<T>
40where
41    T: Soars,
42{
43    fn default() -> Self {
44        Soa::<T>::new().into_iter()
45    }
46}
47
48impl<T> Debug for IntoIter<T>
49where
50    T: Soars,
51    for<'a> T::Ref<'a>: Debug,
52{
53    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
54        write!(f, "{:?}", self.as_slice())
55    }
56}
57
58impl<T> IntoIter<T>
59where
60    T: Soars,
61{
62    /// Returns an immutable slice of all elements that have not been yielded
63    /// yet.
64    pub fn as_slice(&self) -> &Slice<T> {
65        unsafe { self.iter_raw.as_slice() }
66    }
67
68    /// Returns a mutable slice of all elements that have not been yielded yet.
69    pub fn as_mut_slice(&mut self) -> &mut Slice<T> {
70        unsafe { self.iter_raw.as_mut_slice() }
71    }
72}
73
74impl<T> Drop for IntoIter<T>
75where
76    T: Soars,
77{
78    fn drop(&mut self) {
79        if needs_drop::<T>() {
80            for _ in self.by_ref() {}
81        }
82
83        if size_of::<T>() > 0 && self.cap > 0 {
84            unsafe { <T::Raw as SoaRaw>::from_parts(self.ptr, self.cap).dealloc(self.cap) }
85        }
86    }
87}
88
89iter_with_raw!(IntoIter<T>);