Wed, 07 Dec 2022 07:00:27 +0200
Added tag v0.1.0 for changeset 51bfde513cfa
| 5 | 1 | /*! |
| 2 | Utilities for mapping over various container types. | |
| 3 | */ | |
| 0 | 4 | |
| 5 | use std::mem::MaybeUninit; | |
| 6 | use itertools::izip; | |
| 7 | ||
| 5 | 8 | /// Trait for a fixed-length container type. |
| 9 | /// | |
| 10 | /// Implemented by [`Loc`][crate::loc::Loc] vectors, [`Cube`][crate::sets::Cube]s, | |
| 11 | /// and basic arrays. | |
| 0 | 12 | pub trait FixedLength<const N : usize> { |
| 5 | 13 | /// Type of elements of the container. |
| 0 | 14 | type Elem; |
| 5 | 15 | /// Type of iterators over the elements of the container. |
| 16 | type Iter : Iterator<Item = Self::Elem>; | |
| 17 | ||
| 18 | /// Returns an iteartor over the elements of the container. | |
| 0 | 19 | fn fl_iter(self) -> Self::Iter; |
| 20 | } | |
| 21 | ||
| 5 | 22 | /// Trait for a mutable fixed-length container type. |
| 0 | 23 | pub trait FixedLengthMut<const N : usize> : FixedLength<N> { |
| 5 | 24 | /// Type of iterators over references to mutable elements of the container. |
| 0 | 25 | type IterMut<'a> : Iterator<Item=&'a mut Self::Elem> where Self : 'a; |
| 5 | 26 | |
| 27 | /// Returns an iterator over mutable references to elements of the container. | |
| 0 | 28 | fn fl_iter_mut(&mut self) -> Self::IterMut<'_>; |
| 29 | } | |
| 30 | ||
| 31 | impl<A, const N : usize> FixedLength<N> for [A; N] { | |
| 32 | type Elem = A; | |
| 33 | type Iter = std::array::IntoIter<A, N>; | |
| 34 | #[inline] | |
| 35 | fn fl_iter(self) -> Self::Iter { | |
| 36 | self.into_iter() | |
| 37 | } | |
| 38 | } | |
| 39 | ||
| 40 | impl<A, const N : usize> FixedLengthMut<N> for [A; N] { | |
| 41 | type IterMut<'a> = std::slice::IterMut<'a, A> where A : 'a; | |
| 42 | #[inline] | |
| 43 | fn fl_iter_mut(&mut self) -> Self::IterMut<'_> { | |
| 44 | self.iter_mut() | |
| 45 | } | |
| 46 | } | |
| 47 | ||
| 48 | impl<'a, A, const N : usize> FixedLength<N> for &'a [A; N] { | |
| 49 | type Elem = &'a A; | |
| 50 | type Iter = std::slice::Iter<'a, A>; | |
| 51 | #[inline] | |
| 52 | fn fl_iter(self) -> Self::Iter { | |
| 53 | self.iter() | |
| 54 | } | |
| 55 | } | |
| 56 | ||
| 57 | macro_rules! tuple_or_singleton { | |
| 58 | ($a:ident,) => { $a }; | |
| 59 | ($($a:ident),+) => { ($($a),+) } | |
| 60 | } | |
| 61 | ||
| 62 | macro_rules! make_mapmany { | |
| 63 | ($name:ident, $name_indexed:ident, $var0:ident $($var:ident)* ; | |
| 64 | $etype0:ident $($etype:ident)*, $ctype0:ident $($ctype:ident)*) => { | |
| 5 | 65 | /// Map over [`FixedLength`] container(s), returning an array. |
| 0 | 66 | #[inline] |
| 67 | pub fn $name< | |
| 68 | $etype0, | |
| 69 | $($etype,)* | |
| 70 | $ctype0 : FixedLength<N,Elem=$etype0>, | |
| 71 | $($ctype : FixedLength<N,Elem=$etype>,)* | |
| 72 | Res, | |
| 73 | const N : usize | |
| 74 | >( | |
| 75 | $var0 : $ctype0, | |
| 76 | $($var : $ctype,)* | |
| 77 | f : impl Fn($etype0, $($etype),*) -> Res | |
| 78 | ) -> [Res; N] { | |
| 79 | let zipit = izip!($var0.fl_iter(), $($var.fl_iter()),*); | |
| 80 | let map = zipit.map(|tuple_or_singleton!($var0, $($var),*)| f($var0, $($var),*)); | |
| 81 | collect_into_array_unchecked(map) | |
| 82 | } | |
| 83 | ||
| 5 | 84 | /// Map over [`FixedLength`] containers(s) and element indices, returning an array. |
| 0 | 85 | #[inline] |
| 86 | pub fn $name_indexed< | |
| 87 | $etype0, | |
| 88 | $($etype,)* | |
| 89 | $ctype0 : FixedLength<N,Elem=$etype0>, | |
| 90 | $($ctype : FixedLength<N,Elem=$etype>,)* | |
| 91 | Res, | |
| 92 | const N : usize | |
| 93 | >( | |
| 94 | $var0 : $ctype0, | |
| 95 | $($var : $ctype,)* | |
| 96 | f : impl Fn(usize, $etype0, $($etype),*) -> Res | |
| 97 | ) -> [Res; N] { | |
| 98 | let zipit = (0..N).zip(izip!($var0.fl_iter(), $($var.fl_iter()),*)); | |
| 99 | let map = zipit.map(|(i, tuple_or_singleton!($var0, $($var),*))| f(i, $var0, $($var),*)); | |
| 100 | collect_into_array_unchecked(map) | |
| 101 | } | |
| 102 | } | |
| 103 | } | |
| 104 | ||
| 105 | make_mapmany!(map1, map1_indexed, a; A, CA); | |
| 106 | make_mapmany!(map2, map2_indexed, a b; A B, CA CB); | |
| 107 | make_mapmany!(map3, map3_indexed, a b c; A B C, CA CB CC); | |
| 108 | make_mapmany!(map4, map4_indexed, a b c d; A B C D, CA CB CC CD); | |
| 109 | make_mapmany!(map5, map5_indexed, a b c d e; A B C D E, CA CB CC CD CE); | |
| 110 | make_mapmany!(map6, map6_indexed, a b c d e f; A B C D E F, CA CB CC CD CE CF); | |
| 111 | ||
| 112 | macro_rules! make_mapmany_mut{ | |
| 113 | ($name:ident, $name_indexed:ident, $var0:ident $($var:ident)* ; | |
| 114 | $etype0:ident $($etype:ident)*, $ctype0:ident $($ctype:ident)*) => { | |
|
2
ac84e995e119
Convert iteration utilities to GATs
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
115 | /// Map over [`FixedLength`] container(s) with mutable references to the first container. |
| 0 | 116 | #[inline] |
| 117 | pub fn $name< | |
| 118 | $etype0, | |
| 119 | $($etype,)* | |
| 120 | $ctype0 : FixedLengthMut<N,Elem=$etype0>, | |
| 121 | $($ctype : FixedLength<N,Elem=$etype>,)* | |
| 122 | const N : usize | |
| 123 | > ( | |
| 124 | $var0 : &mut $ctype0, | |
| 125 | $($var : $ctype,)* | |
| 126 | f : impl Fn(&mut $etype0, $($etype),*) | |
| 127 | ) { | |
| 128 | let zipit = izip!($var0.fl_iter_mut(), $($var.fl_iter()),*); | |
| 129 | zipit.for_each(|tuple_or_singleton!($var0, $($var),*)| f($var0, $($var),*)); | |
| 130 | } | |
| 131 | ||
|
2
ac84e995e119
Convert iteration utilities to GATs
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
132 | /// Map over [`FixedLength`] container(s) and element indices |
|
ac84e995e119
Convert iteration utilities to GATs
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
133 | /// with mutable references to the first container. |
| 0 | 134 | #[inline] |
| 135 | pub fn $name_indexed< | |
| 136 | $etype0, | |
| 137 | $($etype,)* | |
| 138 | $ctype0 : FixedLengthMut<N,Elem=$etype0>, | |
| 139 | $($ctype : FixedLength<N,Elem=$etype>,)* | |
| 140 | const N : usize | |
| 141 | > ( | |
| 142 | $var0 : &mut $ctype0, | |
| 143 | $($var : $ctype,)* | |
| 144 | f : impl Fn(usize, &mut $etype0, $($etype),*) | |
| 145 | ) { | |
| 146 | let zipit = (0..N).zip(izip!($var0.fl_iter_mut(), $($var.fl_iter()),*)); | |
| 147 | zipit.for_each(|(i, tuple_or_singleton!($var0, $($var),*))| f(i, $var0, $($var),*)); | |
| 148 | } | |
| 149 | } | |
| 150 | } | |
| 151 | ||
| 152 | make_mapmany_mut!(map1_mut, map1_indexed_mut, a; A, CA); | |
| 153 | make_mapmany_mut!(map2_mut, map2_indexed_mut, a b; A B, CA CB); | |
| 154 | make_mapmany_mut!(map3_mut, map3_indexed_mut, a b c; A B C, CA CB CC); | |
| 155 | make_mapmany_mut!(map4_mut, map4_indexed_mut, a b c d; A B C D, CA CB CC CD); | |
| 156 | make_mapmany_mut!(map5_mut, map5_indexed_mut, a b c d e; A B C D E, CA CB CC CD CE); | |
| 157 | make_mapmany_mut!(map6_mut, map6_indexed_mut, a b c d e f; A B C D E F, CA CB CC CD CE CF); | |
| 158 | ||
| 159 | ||
| 160 | /// Initialise an array of length `N` by calling `f` multiple times. | |
| 161 | #[inline] | |
| 162 | pub fn array_init<A, F : Fn() -> A, const N : usize>(f : F) -> [A; N] { | |
| 163 | //[(); N].map(|_| f()) | |
| 164 | core::array::from_fn(|_| f()) | |
| 165 | } | |
| 166 | ||
| 167 | // /// Initialise an array of length `N` by calling `f` with the index of each element. | |
| 168 | // #[inline] | |
| 169 | // pub fn array_gen<A, F : Fn(usize) -> A, const N : usize>(f : F) -> [A; N] { | |
| 170 | // //[(); N].indexmap(|i, _| f(i)) | |
| 171 | // core::array::from_fn(f) | |
| 172 | // } | |
| 173 | ||
| 174 | ||
| 5 | 175 | |
|
2
ac84e995e119
Convert iteration utilities to GATs
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
176 | /// Iterator returned by [`foldmap`][FoldMappable::foldmap] applied to an iterator. |
| 5 | 177 | |
| 0 | 178 | pub struct FoldMap<I : Iterator<Item=A>, A, B, J : Copy, F : Fn(J, A) -> (J, B)> { |
| 179 | iter : I, | |
| 180 | f : F, | |
| 181 | j : J, | |
| 182 | } | |
| 183 | ||
| 184 | impl<A, B, I : Iterator<Item=A>, J : Copy, F : Fn(J, A) -> (J, B)> Iterator for FoldMap<I, A, B, J, F> { | |
| 185 | type Item = B; | |
| 186 | #[inline] | |
| 187 | fn next(&mut self) -> Option<B> { | |
| 188 | self.iter.next().map(|a| { | |
| 189 | let (jnew, b) = (self.f)(self.j, a); | |
| 190 | self.j = jnew; | |
| 191 | b | |
| 192 | }) | |
| 193 | } | |
| 194 | } | |
| 195 | ||
|
2
ac84e995e119
Convert iteration utilities to GATs
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
196 | /// Iterator returned by [`indexmap`][IndexMappable::indexmap] applied to an iterator. |
| 0 | 197 | pub struct IndexMap<I : Iterator<Item=A>, A, B, F : Fn(usize, A) -> B> { |
| 198 | iter : I, | |
| 199 | f : F, | |
| 200 | j : usize, | |
| 201 | } | |
| 202 | ||
| 203 | impl<A, B, I : Iterator<Item=A>, F : Fn(usize, A) -> B> Iterator for IndexMap<I, A, B, F> { | |
| 204 | type Item = B; | |
| 205 | #[inline] | |
| 206 | fn next(&mut self) -> Option<B> { | |
| 207 | self.iter.next().map(|a| { | |
| 208 | let b = (self.f)(self.j, a); | |
| 209 | self.j = self.j+1; | |
| 210 | b | |
| 211 | }) | |
| 212 | } | |
| 213 | } | |
| 214 | ||
|
2
ac84e995e119
Convert iteration utilities to GATs
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
215 | /// Trait for things that can be foldmapped. |
|
ac84e995e119
Convert iteration utilities to GATs
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
216 | /// |
|
ac84e995e119
Convert iteration utilities to GATs
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
217 | /// `A` is the type of elements of `Self`, and `J` the accumulator type for the folding. |
|
ac84e995e119
Convert iteration utilities to GATs
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
218 | pub trait FoldMappable<A, J> : Sized { |
|
ac84e995e119
Convert iteration utilities to GATs
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
219 | type Output<B, F> where F : Fn(J, A) -> (J, B); |
|
ac84e995e119
Convert iteration utilities to GATs
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
220 | /// Fold and map over `self` with `f`. `j` is the initial accumulator for folding. |
|
ac84e995e119
Convert iteration utilities to GATs
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
221 | /// |
|
ac84e995e119
Convert iteration utilities to GATs
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
222 | /// The output type depends on the implementation, but will generally have elements of |
|
ac84e995e119
Convert iteration utilities to GATs
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
223 | /// type `B`. |
|
ac84e995e119
Convert iteration utilities to GATs
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
224 | fn foldmap<B, F : Fn(J, A) -> (J, B)>(self, j : J, f : F) -> Self::Output<B, F>; |
| 0 | 225 | } |
| 226 | ||
|
2
ac84e995e119
Convert iteration utilities to GATs
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
227 | /// Trait for things that can be indexmapped. |
|
ac84e995e119
Convert iteration utilities to GATs
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
228 | /// |
|
ac84e995e119
Convert iteration utilities to GATs
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
229 | /// `A` is the type of elements of `Self`. |
|
ac84e995e119
Convert iteration utilities to GATs
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
230 | pub trait IndexMappable<A> : Sized { |
|
ac84e995e119
Convert iteration utilities to GATs
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
231 | type Output<B, F> where F : Fn(usize, A) -> B; |
|
ac84e995e119
Convert iteration utilities to GATs
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
232 | /// Map over element indices and elements of `self`. |
|
ac84e995e119
Convert iteration utilities to GATs
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
233 | /// |
|
ac84e995e119
Convert iteration utilities to GATs
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
234 | /// The output type depends on the implementation, but will generally have elements of |
|
ac84e995e119
Convert iteration utilities to GATs
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
235 | /// type `B`. |
|
ac84e995e119
Convert iteration utilities to GATs
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
236 | fn indexmap<B, F : Fn(usize, A) -> B>(self, f : F) -> Self::Output<B, F>; |
| 0 | 237 | } |
| 238 | ||
|
2
ac84e995e119
Convert iteration utilities to GATs
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
239 | impl<'a, A, J : Copy> FoldMappable<&'a A, J> |
| 0 | 240 | for std::slice::Iter<'a, A> { |
|
2
ac84e995e119
Convert iteration utilities to GATs
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
241 | type Output<B, F> = FoldMap<Self, &'a A, B, J, F> where F : Fn(J, &'a A) -> (J, B); |
| 0 | 242 | #[inline] |
|
2
ac84e995e119
Convert iteration utilities to GATs
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
243 | fn foldmap<B, F : Fn(J, &'a A) -> (J, B)>(self, j : J, f : F) -> Self::Output<B, F> { |
| 0 | 244 | FoldMap { iter : self, j, f } |
| 245 | } | |
| 246 | } | |
| 247 | ||
|
2
ac84e995e119
Convert iteration utilities to GATs
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
248 | impl<'a, A> IndexMappable<&'a A> |
| 0 | 249 | for std::slice::Iter<'a, A> { |
|
2
ac84e995e119
Convert iteration utilities to GATs
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
250 | type Output<B, F> = IndexMap<Self, &'a A, B, F> where F : Fn(usize, &'a A) -> B; |
| 0 | 251 | #[inline] |
|
2
ac84e995e119
Convert iteration utilities to GATs
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
252 | fn indexmap<B, F : Fn(usize, &'a A) -> B>(self, f : F) -> Self::Output<B, F> { |
| 0 | 253 | IndexMap { iter : self, j : 0, f } |
| 254 | } | |
| 255 | } | |
| 256 | ||
| 257 | ||
|
2
ac84e995e119
Convert iteration utilities to GATs
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
258 | impl<A, J : Copy, const N : usize> FoldMappable<A, J> |
| 0 | 259 | for std::array::IntoIter<A, N> { |
|
2
ac84e995e119
Convert iteration utilities to GATs
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
260 | type Output<B, F> = FoldMap<Self, A, B, J, F> where F : Fn(J, A) -> (J, B); |
| 0 | 261 | #[inline] |
|
2
ac84e995e119
Convert iteration utilities to GATs
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
262 | fn foldmap<B, F : Fn(J, A) -> (J, B)>(self, j : J, f : F) -> Self::Output<B, F> { |
| 0 | 263 | FoldMap { iter : self, j, f } |
| 264 | } | |
| 265 | } | |
| 266 | ||
|
2
ac84e995e119
Convert iteration utilities to GATs
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
267 | impl<'a, A, const N : usize> IndexMappable<A> |
| 0 | 268 | for std::array::IntoIter<A, N> { |
|
2
ac84e995e119
Convert iteration utilities to GATs
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
269 | type Output<B, F> = IndexMap<Self, A, B, F> where F : Fn(usize, A) -> B; |
| 0 | 270 | #[inline] |
|
2
ac84e995e119
Convert iteration utilities to GATs
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
271 | fn indexmap<B, F : Fn(usize, A) -> B>(self, f : F) -> Self::Output<B, F> { |
| 0 | 272 | IndexMap { iter : self, j : 0, f } |
| 273 | } | |
| 274 | } | |
| 275 | ||
|
2
ac84e995e119
Convert iteration utilities to GATs
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
276 | impl<A, J : Copy, const N : usize> FoldMappable<A, J> for [A; N] { |
|
ac84e995e119
Convert iteration utilities to GATs
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
277 | type Output<B, F> = [B; N] where F : Fn(J, A) -> (J, B); |
| 0 | 278 | #[inline] |
|
2
ac84e995e119
Convert iteration utilities to GATs
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
279 | fn foldmap<B, F : Fn(J, A) -> (J, B)>(self, j : J, f : F) -> [B; N] { |
| 0 | 280 | // //let mut res : [MaybeUninit<B>; N] = unsafe { MaybeUninit::uninit().assume_init() }; |
| 281 | // let mut res = MaybeUninit::uninit_array::<N>(); | |
| 282 | // for (a, i) in self.into_iter().zip(0..N) { | |
| 283 | // let (jnew, b) = f(j, a); | |
| 284 | // unsafe { *(res.get_unchecked_mut(i)) = MaybeUninit::new(b) }; | |
| 285 | // j = jnew; | |
| 286 | // } | |
| 287 | // //unsafe { res.as_mut_ptr().cast::<[B; N]>().read() } | |
| 288 | // unsafe { MaybeUninit::array_assume_init(res) } | |
| 289 | let it = self.into_iter().foldmap(j, f); | |
| 290 | collect_into_array_unchecked(it) | |
| 291 | } | |
| 292 | } | |
| 293 | ||
|
2
ac84e995e119
Convert iteration utilities to GATs
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
294 | impl<A, const N : usize> IndexMappable<A> for [A; N] { |
|
ac84e995e119
Convert iteration utilities to GATs
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
295 | type Output<B, F> = [B; N] where F : Fn(usize, A) -> B; |
| 0 | 296 | #[inline] |
|
2
ac84e995e119
Convert iteration utilities to GATs
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
297 | fn indexmap<B, F : Fn(usize, A) -> B>(self, f : F) -> [B; N] { |
| 0 | 298 | // //let mut res : [MaybeUninit<B>; N] = unsafe { MaybeUninit::uninit().assume_init() }; |
| 299 | // let mut res = MaybeUninit::uninit_array::<N>(); | |
| 300 | // for (a, i) in self.into_iter().zip(0..N) { | |
| 301 | // let b = f(i, a); | |
| 302 | // unsafe { *(res.get_unchecked_mut(i)) = MaybeUninit::new(b) }; | |
| 303 | // } | |
| 304 | // //unsafe { res.as_mut_ptr().cast::<[B; N]>().read() } | |
| 305 | // unsafe { MaybeUninit::array_assume_init(res) } | |
| 306 | let it = self.into_iter().indexmap(f); | |
| 307 | collect_into_array_unchecked(it) | |
| 308 | } | |
| 309 | } | |
| 310 | ||
| 311 | /// This is taken and simplified from core::array to not involve `ControlFlow`, | |
| 312 | /// `Try` etc. (Pulling everything including `NeverShortCircuit` turned out | |
| 313 | /// too much to maintain here.) | |
| 314 | /// | |
| 315 | /// Pulls `N` items from `iter` and returns them as an array. If the iterator | |
| 316 | /// yields fewer than `N` items, `None` is returned and all already yielded | |
| 317 | /// items are dropped. | |
| 318 | /// | |
| 319 | /// Since the iterator is passed as a mutable reference and this function calls | |
| 320 | /// `next` at most `N` times, the iterator can still be used afterwards to | |
| 321 | /// retrieve the remaining items. | |
| 322 | /// | |
| 323 | /// If `iter.next()` panicks, all items already yielded by the iterator are | |
| 324 | /// dropped. | |
| 325 | #[inline] | |
| 326 | pub(crate) fn collect_into_array_unchecked<T, I : Iterator<Item=T>, const N: usize>(mut iter: I) -> [T; N] | |
| 327 | { | |
| 328 | if N == 0 { | |
| 329 | // SAFETY: An empty array is always inhabited and has no validity invariants. | |
| 330 | return unsafe { core::mem::zeroed() }; | |
| 331 | } | |
| 332 | ||
| 333 | struct Guard<'a, T, const N: usize> { | |
| 334 | array_mut: &'a mut [MaybeUninit<T>; N], | |
| 335 | initialized: usize, | |
| 336 | } | |
| 337 | ||
| 338 | impl<T, const N: usize> Drop for Guard<'_, T, N> { | |
| 339 | #[inline] | |
| 340 | fn drop(&mut self) { | |
| 341 | debug_assert!(self.initialized <= N); | |
| 342 | ||
| 343 | // SAFETY: this slice will contain only initialized objects. | |
| 344 | unsafe { | |
| 345 | core::ptr::drop_in_place(MaybeUninit::slice_assume_init_mut( | |
| 346 | &mut self.array_mut.get_unchecked_mut(..self.initialized), | |
| 347 | )); | |
| 348 | } | |
| 349 | } | |
| 350 | } | |
| 351 | ||
| 352 | let mut array = MaybeUninit::uninit_array::<N>(); | |
| 353 | let mut guard = Guard { array_mut: &mut array, initialized: 0 }; | |
| 354 | ||
| 355 | while let Some(item) = iter.next() { | |
| 356 | // SAFETY: `guard.initialized` starts at 0, is increased by one in the | |
| 357 | // loop and the loop is aborted once it reaches N (which is | |
| 358 | // `array.len()`). | |
| 359 | unsafe { | |
| 360 | guard.array_mut.get_unchecked_mut(guard.initialized).write(item); | |
| 361 | } | |
| 362 | guard.initialized += 1; | |
| 363 | ||
| 364 | // Check if the whole array was initialized. | |
| 365 | if guard.initialized == N { | |
| 366 | core::mem::forget(guard); | |
| 367 | ||
| 368 | // SAFETY: the condition above asserts that all elements are | |
| 369 | // initialized. | |
| 370 | let out = unsafe { MaybeUninit::array_assume_init(array) }; | |
| 371 | return out; | |
| 372 | } | |
| 373 | } | |
| 374 | ||
| 375 | unreachable!("Something went wrong with iterator length") | |
| 376 | } | |
| 377 | ||
| 378 | ||
| 379 | #[cfg(test)] | |
| 380 | mod tests { | |
| 381 | use super::*; | |
| 382 | ||
| 383 | #[test] | |
| 384 | fn mapx_test() { | |
| 385 | let a = [0,1,2]; | |
| 386 | let mut b = [2,1,0]; | |
| 387 | assert_eq!(map1(a, |x| x+1), [1,2,3]); | |
| 388 | assert_eq!(map2(a, b, |x, y| x+y), [2,2,2]); | |
| 389 | assert_eq!(map1_indexed(a, |i, y| y-i), [0,0,0]); | |
| 390 | map1_indexed_mut(&mut b, |i, y| *y=i); | |
| 391 | assert_eq!(b, a); | |
| 392 | } | |
| 393 | } |