1 //! Defines the `IntoIter` owned iterator for arrays.
5 iter::{self, ExactSizeIterator, FusedIterator, TrustedLen},
6 mem::{self, MaybeUninit},
11 /// A by-value [array] iterator.
12 #[stable(feature = "array_value_iter", since = "1.51.0")]
13 #[rustc_insignificant_dtor]
14 pub struct IntoIter<T, const N: usize> {
15 /// This is the array we are iterating over.
17 /// Elements with index `i` where `alive.start <= i < alive.end` have not
18 /// been yielded yet and are valid array entries. Elements with indices `i
19 /// < alive.start` or `i >= alive.end` have been yielded already and must
20 /// not be accessed anymore! Those dead elements might even be in a
21 /// completely uninitialized state!
23 /// So the invariants are:
24 /// - `data[alive]` is alive (i.e. contains valid elements)
25 /// - `data[..alive.start]` and `data[alive.end..]` are dead (i.e. the
26 /// elements were already read and must not be touched anymore!)
27 data: [MaybeUninit<T>; N],
29 /// The elements in `data` that have not been yielded yet.
32 /// - `alive.start <= alive.end`
33 /// - `alive.end <= N`
37 // Note: the `#[rustc_skip_array_during_method_dispatch]` on `trait IntoIterator`
38 // hides this implementation from explicit `.into_iter()` calls on editions < 2021,
39 // so those calls will still resolve to the slice implementation, by reference.
40 #[stable(feature = "array_into_iter_impl", since = "1.53.0")]
41 impl<T, const N: usize> IntoIterator for [T; N] {
43 type IntoIter = IntoIter<T, N>;
45 /// Creates a consuming iterator, that is, one that moves each value out of
46 /// the array (from start to end). The array cannot be used after calling
47 /// this unless `T` implements `Copy`, so the whole array is copied.
49 /// Arrays have special behavior when calling `.into_iter()` prior to the
50 /// 2021 edition -- see the [array] Editions section for more information.
52 /// [array]: prim@array
53 fn into_iter(self) -> Self::IntoIter {
54 // SAFETY: The transmute here is actually safe. The docs of `MaybeUninit`
57 // > `MaybeUninit<T>` is guaranteed to have the same size and alignment
60 // The docs even show a transmute from an array of `MaybeUninit<T>` to
63 // With that, this initialization satisfies the invariants.
65 // FIXME(LukasKalbertodt): actually use `mem::transmute` here, once it
66 // works with const generics:
67 // `mem::transmute::<[T; N], [MaybeUninit<T>; N]>(array)`
69 // Until then, we can use `mem::transmute_copy` to create a bitwise copy
70 // as a different type, then forget `array` so that it is not dropped.
72 let iter = IntoIter { data: mem::transmute_copy(&self), alive: 0..N };
79 impl<T, const N: usize> IntoIter<T, N> {
80 /// Creates a new iterator over the given `array`.
81 #[stable(feature = "array_value_iter", since = "1.51.0")]
82 #[rustc_deprecated(since = "1.59.0", reason = "use `IntoIterator::into_iter` instead")]
83 pub fn new(array: [T; N]) -> Self {
84 IntoIterator::into_iter(array)
87 /// Creates an iterator over the elements in a partially-initialized buffer.
89 /// If you have a fully-initialized array, then use [`IntoIterator`].
90 /// But this is useful for returning partial results from unsafe code.
94 /// - The `buffer[initialized]` elements must all be initialized.
95 /// - The range must be canonical, with `initialized.start <= initialized.end`.
96 /// - The range must be in-bounds for the buffer, with `initialized.end <= N`.
97 /// (Like how indexing `[0][100..100]` fails despite the range being empty.)
99 /// It's sound to have more elements initialized than mentioned, though that
100 /// will most likely result in them being leaked.
105 /// #![feature(array_into_iter_constructors)]
107 /// #![feature(maybe_uninit_array_assume_init)]
108 /// #![feature(maybe_uninit_uninit_array)]
109 /// use std::array::IntoIter;
110 /// use std::mem::MaybeUninit;
112 /// # // Hi! Thanks for reading the code. This is restricted to `Copy` because
113 /// # // otherwise it could leak. A fully-general version this would need a drop
114 /// # // guard to handle panics from the iterator, but this works for an example.
115 /// fn next_chunk<T: Copy, const N: usize>(
116 /// it: &mut impl Iterator<Item = T>,
117 /// ) -> Result<[T; N], IntoIter<T, N>> {
118 /// let mut buffer = MaybeUninit::uninit_array();
121 /// match it.next() {
123 /// buffer[i].write(x);
127 /// // SAFETY: We've initialized the first `i` items
129 /// return Err(IntoIter::new_unchecked(buffer, 0..i));
135 /// // SAFETY: We've initialized all N items
136 /// unsafe { Ok(MaybeUninit::array_assume_init(buffer)) }
139 /// let r: [_; 4] = next_chunk(&mut (10..16)).unwrap();
140 /// assert_eq!(r, [10, 11, 12, 13]);
141 /// let r: IntoIter<_, 40> = next_chunk(&mut (10..16)).unwrap_err();
142 /// assert_eq!(r.collect::<Vec<_>>(), vec![10, 11, 12, 13, 14, 15]);
144 #[unstable(feature = "array_into_iter_constructors", issue = "91583")]
145 #[rustc_const_unstable(feature = "const_array_into_iter_constructors", issue = "91583")]
146 pub const unsafe fn new_unchecked(
147 buffer: [MaybeUninit<T>; N],
148 initialized: Range<usize>,
150 Self { data: buffer, alive: initialized }
153 /// Creates an iterator over `T` which returns no elements.
155 /// If you just need an empty iterator, then use
156 /// [`iter::empty()`](crate::iter::empty) instead.
157 /// And if you need an empty array, use `[]`.
159 /// But this is useful when you need an `array::IntoIter<T, N>` *specifically*.
164 /// #![feature(array_into_iter_constructors)]
165 /// use std::array::IntoIter;
167 /// let empty = IntoIter::<i32, 3>::empty();
168 /// assert_eq!(empty.len(), 0);
169 /// assert_eq!(empty.as_slice(), &[]);
171 /// let empty = IntoIter::<std::convert::Infallible, 200>::empty();
172 /// assert_eq!(empty.len(), 0);
175 /// `[1, 2].into_iter()` and `[].into_iter()` have different types
176 /// ```should_fail,edition2021
177 /// #![feature(array_into_iter_constructors)]
178 /// use std::array::IntoIter;
180 /// pub fn get_bytes(b: bool) -> IntoIter<i8, 4> {
182 /// [1, 2, 3, 4].into_iter()
184 /// [].into_iter() // error[E0308]: mismatched types
189 /// But using this method you can get an empty iterator of appropriate size:
191 /// #![feature(array_into_iter_constructors)]
192 /// use std::array::IntoIter;
194 /// pub fn get_bytes(b: bool) -> IntoIter<i8, 4> {
196 /// [1, 2, 3, 4].into_iter()
198 /// IntoIter::empty()
202 /// assert_eq!(get_bytes(true).collect::<Vec<_>>(), vec![1, 2, 3, 4]);
203 /// assert_eq!(get_bytes(false).collect::<Vec<_>>(), vec![]);
205 #[unstable(feature = "array_into_iter_constructors", issue = "91583")]
206 #[rustc_const_unstable(feature = "const_array_into_iter_constructors", issue = "91583")]
207 pub const fn empty() -> Self {
208 let buffer = MaybeUninit::uninit_array();
209 let initialized = 0..0;
211 // SAFETY: We're telling it that none of the elements are initialized,
212 // which is trivially true. And ∀N: usize, 0 <= N.
213 unsafe { Self::new_unchecked(buffer, initialized) }
216 /// Returns an immutable slice of all elements that have not been yielded
218 #[stable(feature = "array_value_iter", since = "1.51.0")]
219 pub fn as_slice(&self) -> &[T] {
220 // SAFETY: We know that all elements within `alive` are properly initialized.
222 let slice = self.data.get_unchecked(self.alive.clone());
223 MaybeUninit::slice_assume_init_ref(slice)
227 /// Returns a mutable slice of all elements that have not been yielded yet.
228 #[stable(feature = "array_value_iter", since = "1.51.0")]
229 pub fn as_mut_slice(&mut self) -> &mut [T] {
230 // SAFETY: We know that all elements within `alive` are properly initialized.
232 let slice = self.data.get_unchecked_mut(self.alive.clone());
233 MaybeUninit::slice_assume_init_mut(slice)
238 #[stable(feature = "array_value_iter_impls", since = "1.40.0")]
239 impl<T, const N: usize> Iterator for IntoIter<T, N> {
241 fn next(&mut self) -> Option<Self::Item> {
242 // Get the next index from the front.
244 // Increasing `alive.start` by 1 maintains the invariant regarding
245 // `alive`. However, due to this change, for a short time, the alive
246 // zone is not `data[alive]` anymore, but `data[idx..alive.end]`.
247 self.alive.next().map(|idx| {
248 // Read the element from the array.
249 // SAFETY: `idx` is an index into the former "alive" region of the
250 // array. Reading this element means that `data[idx]` is regarded as
251 // dead now (i.e. do not touch). As `idx` was the start of the
252 // alive-zone, the alive zone is now `data[alive]` again, restoring
254 unsafe { self.data.get_unchecked(idx).assume_init_read() }
258 fn size_hint(&self) -> (usize, Option<usize>) {
259 let len = self.len();
264 fn fold<Acc, Fold>(mut self, init: Acc, mut fold: Fold) -> Acc
266 Fold: FnMut(Acc, Self::Item) -> Acc,
268 let data = &mut self.data;
269 self.alive.by_ref().fold(init, |acc, idx| {
270 // SAFETY: idx is obtained by folding over the `alive` range, which implies the
271 // value is currently considered alive but as the range is being consumed each value
272 // we read here will only be read once and then considered dead.
273 fold(acc, unsafe { data.get_unchecked(idx).assume_init_read() })
277 fn count(self) -> usize {
281 fn last(mut self) -> Option<Self::Item> {
285 fn advance_by(&mut self, n: usize) -> Result<(), usize> {
286 let len = self.len();
288 // The number of elements to drop. Always in-bounds by construction.
289 let delta = cmp::min(n, len);
291 let range_to_drop = self.alive.start..(self.alive.start + delta);
293 // Moving the start marks them as conceptually "dropped", so if anything
294 // goes bad then our drop impl won't double-free them.
295 self.alive.start += delta;
297 // SAFETY: These elements are currently initialized, so it's fine to drop them.
299 let slice = self.data.get_unchecked_mut(range_to_drop);
300 ptr::drop_in_place(MaybeUninit::slice_assume_init_mut(slice));
303 if n > len { Err(len) } else { Ok(()) }
307 #[stable(feature = "array_value_iter_impls", since = "1.40.0")]
308 impl<T, const N: usize> DoubleEndedIterator for IntoIter<T, N> {
309 fn next_back(&mut self) -> Option<Self::Item> {
310 // Get the next index from the back.
312 // Decreasing `alive.end` by 1 maintains the invariant regarding
313 // `alive`. However, due to this change, for a short time, the alive
314 // zone is not `data[alive]` anymore, but `data[alive.start..=idx]`.
315 self.alive.next_back().map(|idx| {
316 // Read the element from the array.
317 // SAFETY: `idx` is an index into the former "alive" region of the
318 // array. Reading this element means that `data[idx]` is regarded as
319 // dead now (i.e. do not touch). As `idx` was the end of the
320 // alive-zone, the alive zone is now `data[alive]` again, restoring
322 unsafe { self.data.get_unchecked(idx).assume_init_read() }
326 fn advance_back_by(&mut self, n: usize) -> Result<(), usize> {
327 let len = self.len();
329 // The number of elements to drop. Always in-bounds by construction.
330 let delta = cmp::min(n, len);
332 let range_to_drop = (self.alive.end - delta)..self.alive.end;
334 // Moving the end marks them as conceptually "dropped", so if anything
335 // goes bad then our drop impl won't double-free them.
336 self.alive.end -= delta;
338 // SAFETY: These elements are currently initialized, so it's fine to drop them.
340 let slice = self.data.get_unchecked_mut(range_to_drop);
341 ptr::drop_in_place(MaybeUninit::slice_assume_init_mut(slice));
344 if n > len { Err(len) } else { Ok(()) }
348 #[stable(feature = "array_value_iter_impls", since = "1.40.0")]
349 impl<T, const N: usize> Drop for IntoIter<T, N> {
351 // SAFETY: This is safe: `as_mut_slice` returns exactly the sub-slice
352 // of elements that have not been moved out yet and that remain
354 unsafe { ptr::drop_in_place(self.as_mut_slice()) }
358 #[stable(feature = "array_value_iter_impls", since = "1.40.0")]
359 impl<T, const N: usize> ExactSizeIterator for IntoIter<T, N> {
360 fn len(&self) -> usize {
361 // Will never underflow due to the invariant `alive.start <=
363 self.alive.end - self.alive.start
365 fn is_empty(&self) -> bool {
366 self.alive.is_empty()
370 #[stable(feature = "array_value_iter_impls", since = "1.40.0")]
371 impl<T, const N: usize> FusedIterator for IntoIter<T, N> {}
373 // The iterator indeed reports the correct length. The number of "alive"
374 // elements (that will still be yielded) is the length of the range `alive`.
375 // This range is decremented in length in either `next` or `next_back`. It is
376 // always decremented by 1 in those methods, but only if `Some(_)` is returned.
377 #[stable(feature = "array_value_iter_impls", since = "1.40.0")]
378 unsafe impl<T, const N: usize> TrustedLen for IntoIter<T, N> {}
380 #[stable(feature = "array_value_iter_impls", since = "1.40.0")]
381 impl<T: Clone, const N: usize> Clone for IntoIter<T, N> {
382 fn clone(&self) -> Self {
383 // Note, we don't really need to match the exact same alive range, so
384 // we can just clone into offset 0 regardless of where `self` is.
385 let mut new = Self { data: MaybeUninit::uninit_array(), alive: 0..0 };
387 // Clone all alive elements.
388 for (src, dst) in iter::zip(self.as_slice(), &mut new.data) {
389 // Write a clone into the new array, then update its alive range.
390 // If cloning panics, we'll correctly drop the previous items.
391 dst.write(src.clone());
399 #[stable(feature = "array_value_iter_impls", since = "1.40.0")]
400 impl<T: fmt::Debug, const N: usize> fmt::Debug for IntoIter<T, N> {
401 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
402 // Only print the elements that were not yielded yet: we cannot
403 // access the yielded elements anymore.
404 f.debug_tuple("IntoIter").field(&self.as_slice()).finish()