1use super::Array;
7
8use core::{
10 mem::MaybeUninit,
11 slice::{
12 Iter,
13 IterMut
14 },
15 iter::{
16 ExactSizeIterator,
17 DoubleEndedIterator,
18 FusedIterator,
19 TrustedLen
20 },
21 marker::Destruct
22};
23
24
25pub struct Iterable<Type, const N: usize> {
31 index: usize,
32 reduced: usize,
33 length: usize,
34 data: [MaybeUninit<Type>; N]
35}
36
37const impl<Type: [const] Destruct, const N: usize> Drop for Iterable<Type, N> {
39 fn drop(&mut self) {while let Some(value) = self.next() {drop(value)}}
40}
41
42const impl<Type, const N: usize> Iterator for Iterable<Type, N> {
44 type Item = Type;
45 fn next(&mut self) -> Option<Self::Item> {
46 return if self.length - self.reduced - self.index == 0 {None} else {
47 let value = unsafe {self.data[self.index].assume_init_read()};
48 self.index += 1;
49 Some(value)
50 }
51 }
52 fn size_hint(&self) -> (usize, Option<usize>) {
53 return (self.length - self.index, Some(self.length - self.index));
54 }
55}
56
57impl<Type, const N: usize> ExactSizeIterator for Iterable<Type, N> {}
59
60const impl<Type, const N: usize> DoubleEndedIterator for Iterable<Type, N> {
62 fn next_back(&mut self) -> Option<Self::Item> {
63 return if self.length - self.reduced - self.index == 0 {None} else {
64 self.reduced += 1;
65 Some(unsafe {self.data[self.length - self.reduced].assume_init_read()})
66 }
67 }
68}
69
70impl<Type, const N: usize> FusedIterator for Iterable<Type, N> {}
72
73unsafe impl<Type, const N: usize> TrustedLen for Iterable<Type, N> {}
75
76
77impl<Type, const N: usize> FromIterator<Type> for Array<Type, N> {
83 fn from_iter<T: IntoIterator<Item = Type>>(iter: T) -> Self {
84 let mut array = Self::new();
85 array.extend(iter);
86 return array;
87 }
88}
89
90const impl<Type, const N: usize> IntoIterator for Array<Type, N> {
92 type Item = Type;
93 type IntoIter = Iterable<Type, N>;
94 fn into_iter(self) -> Self::IntoIter {
95 let (length, data) = self.into();
96 let iterator = Self::IntoIter {
97 index: 0,
98 reduced: 0,
99 length: length,
100 data: data
101 };
102 return iterator;
103 }
104}
105
106const impl<'valid, Type, const N: usize> IntoIterator for &'valid Array<Type, N> {
108 type Item = &'valid Type;
109 type IntoIter = Iter<'valid, Type>;
110 fn into_iter(self) -> Self::IntoIter {self.iter()}
111}
112
113const impl<'valid, Type, const N: usize> IntoIterator for &'valid mut Array<Type, N> {
115 type Item = &'valid mut Type;
116 type IntoIter = IterMut<'valid, Type>;
117 fn into_iter(self) -> Self::IntoIter {self.iter_mut()}
118}