Skip to main content

libutils_array/
iterators.rs

1//^
2//^ HEAD
3//^
4
5//> HEAD -> SUPER
6use super::Array;
7
8//> HEAD -> CORE
9use core::{
10    mem::{
11        MaybeUninit,
12        forget
13    },
14    slice::{
15        Iter,
16        IterMut
17    },
18    iter::{
19        ExactSizeIterator,
20        DoubleEndedIterator,
21        FusedIterator,
22        TrustedLen
23    },
24    marker::Destruct
25};
26
27
28//^
29//^ ITERABLE
30//^
31
32//> ITERABLE -> STRUCT
33pub struct Iterable<Type, const N: usize> {
34    index: usize,
35    reduced: usize,
36    length: usize,
37    data: MaybeUninit<[Type; N]>
38} 
39
40//> ITERABLE -> DROP
41const impl<Type: [const] Destruct, const N: usize> Drop for Iterable<Type, N> {
42    fn drop(&mut self) {while let Some(value) = self.next() {drop(value)}}
43}
44
45//> ITERABLE -> ITERATOR
46const impl<Type, const N: usize> Iterator for Iterable<Type, N> {
47    type Item = Type;
48    fn next(&mut self) -> Option<Self::Item> {
49        return if self.length - self.reduced - self.index == 0 {None} else {
50            let value = unsafe {(self.data.as_ptr() as *const Type).add(self.index).read()};
51            self.index += 1;
52            Some(value)
53        }
54    }
55    fn size_hint(&self) -> (usize, Option<usize>) {return (self.length - self.index, Some(self.length - self.index))}
56}
57
58//> ITERABLE -> EXACT SIZE
59impl<Type, const N: usize> ExactSizeIterator for Iterable<Type, N> {}
60
61//> ITERABLE -> DOUBLE ENDED
62const impl<Type, const N: usize> DoubleEndedIterator for Iterable<Type, N> {
63    fn next_back(&mut self) -> Option<Self::Item> {
64        return if self.length - self.reduced - self.index == 0 {None} else {
65            let value = unsafe {(self.data.as_ptr() as *const Type).add(self.length - 1 - self.reduced).read()};
66            self.reduced += 1;
67            Some(value)
68        }
69    }
70}
71
72//> ITERABLE -> FUSED
73impl<Type, const N: usize> FusedIterator for Iterable<Type, N> {}
74
75//> ITERABLE -> TRUSTED LEN
76unsafe impl<Type, const N: usize> TrustedLen for Iterable<Type, N> {}
77
78
79//^
80//^ ITERATORS
81//^
82
83//> ITERATORS -> FROM
84impl<Type, const N: usize> FromIterator<Type> for Array<Type, N> {
85    fn from_iter<T: IntoIterator<Item = Type>>(iter: T) -> Self {
86        let mut array = Self::new();
87        array.extend(iter);
88        return array;
89    }
90}
91
92//> ITERATORS -> INTO
93const impl<Type, const N: usize> IntoIterator for Array<Type, N> {
94    type Item = Type;
95    type IntoIter = Iterable<Type, N>;
96    fn into_iter(self) -> Self::IntoIter {
97        let iterator = Self::IntoIter {
98            index: 0,
99            reduced: 0,
100            length: self.length,
101            data: unsafe {MaybeUninit::new(self.data.as_ptr().read())}
102        };
103        forget(self);
104        return iterator;
105    }
106}
107
108//> ITERATORS -> BORROWED
109const impl<'valid, Type, const N: usize> IntoIterator for &'valid Array<Type, N> {
110    type Item = &'valid Type;
111    type IntoIter = Iter<'valid, Type>;
112    fn into_iter(self) -> Self::IntoIter {self.iter()}
113}
114
115//> ITERATORS -> BORROWED MUTABLY
116const impl<'valid, Type, const N: usize> IntoIterator for &'valid mut Array<Type, N> {
117    type Item = &'valid mut Type;
118    type IntoIter = IterMut<'valid, Type>;
119    fn into_iter(self) -> Self::IntoIter {self.iter_mut()}
120}