Skip to main content

generic_array/
lib.rs

1//! This crate implements a structure that can be used as a generic array type.
2//!
3//! **Requires minimum Rust version of 1.65.0**
4//!
5//! [Documentation on GH Pages](https://fizyk20.github.io/generic-array/generic_array/)
6//! may be required to view certain types on foreign crates.
7//!
8//! ## Upgrading from 0.14 or using with `hybrid-array 0.4`
9//!
10//! `generic-array 0.14` has been officially deprecated, so here's a quick guide on how to upgrade from `generic-array 0.14` to `1.x`. Note that libraries depending on `generic-array` will need to update their usage as well. Some libraries are moving to `hybrid-array 0.4` instead, which we provide interoperability with `generic-array 1.x` via the `hybrid-array-0_4` feature flag.
11//!
12//! <details>
13//! <summary>Click to expand</summary>
14//!
15//! To upgrade to `1.x`, change your `Cargo.toml` to use the new version:
16//!
17//! ```toml
18//! [dependencies]
19//! generic-array = "1"
20//! ```
21//!
22//! then in your code, go through and remove the `<T>` from `ArrayLength<T>` bounds, as the type parameter has been removed. It's now just `ArrayLength`.
23//!
24//! If you _need_ to interoperate with `generic-array 0.14`, enable the `compat-0_14` feature flag:
25//!
26//! ```toml
27//! [dependencies]
28//! generic-array = { version = "1", features = ["compat-0_14"] }
29//! ```
30//!
31//! then use the `to_0_14`/`from_0_14`/`as_0_14`/`as_0_14_mut` methods on `GenericArray` to convert between versions, or use the `From`/`AsRef`/`AsMut` implementations.
32//!
33//! The `arr!` macro has changed to no longer require a type parameter, so change:
34//!
35//! ```rust,ignore
36//! let array = arr![i32; 1, 2, 3];
37//! // to
38//! let array = arr![1, 2, 3];
39//! ```
40//!
41//! For interoperability with `hybrid-array 0.4`, enable the `hybrid-array-0_4` feature flag:
42//!
43//! ```toml
44//! [dependencies]
45//! generic-array = { version = "1", features = ["hybrid-array-0_4"] }
46//! ```
47//!
48//! then use the `to_ha0_4`/`from_ha0_4`/`as_ha0_4`/`as_ha0_4_mut` methods on `GenericArray` to convert between versions, or use the `From`/`AsRef`/`AsMut` implementations.
49//!
50//! We also implement the `AssocArraySize` and `AsArrayRef`/`AsArrayMut` traits from `hybrid-array` for `GenericArray`.
51//!
52//! </details>
53//!
54//! ## Usage
55//!
56//! Before Rust 1.51, arrays `[T; N]` were problematic in that they couldn't be
57//! generic with respect to the length `N`, so this wouldn't work:
58//!
59//! ```compile_fail
60//! struct Foo<N> {
61//!     data: [i32; N],
62//! }
63//! ```
64//!
65//! Since 1.51, the below syntax is valid:
66//!
67//! ```rust
68//! struct Foo<const N: usize> {
69//!     data: [i32; N],
70//! }
71//! ```
72//!
73//! However, the const-generics we have as of writing this are still the minimum-viable product (`min_const_generics`), so many situations still result in errors, such as this example:
74//!
75//! ```compile_fail
76//! # struct Foo<const N: usize> {
77//! #   data: [i32; N],
78//! # }
79//! trait Bar {
80//!     const LEN: usize;
81//!
82//!     // Error: cannot perform const operation using `Self`
83//!     fn bar(&self) -> Foo<{ Self::LEN }>;
84//! }
85//! ```
86//!
87//! **generic-array** defines a new trait [`ArrayLength`] and a struct [`GenericArray<T, N: ArrayLength>`](GenericArray),
88//! which lets the above be implemented as:
89//!
90//! ```rust
91//! use generic_array::{GenericArray, ArrayLength};
92//!
93//! struct Foo<N: ArrayLength> {
94//!     data: GenericArray<i32, N>
95//! }
96//!
97//! trait Bar {
98//!     type LEN: ArrayLength;
99//!     fn bar(&self) -> Foo<Self::LEN>;
100//! }
101//! ```
102//!
103//! The [`ArrayLength`] trait is implemented for
104//! [unsigned integer types](typenum::Unsigned) from
105//! [typenum]. For example, [`GenericArray<T, U5>`] would work almost like `[T; 5]`:
106//!
107//! ```rust
108//! # use generic_array::{ArrayLength, GenericArray};
109//! use generic_array::typenum::U5;
110//!
111//! struct Foo<T, N: ArrayLength> {
112//!     data: GenericArray<T, N>
113//! }
114//!
115//! let foo = Foo::<i32, U5> { data: GenericArray::default() };
116//! ```
117//!
118//! The `arr!` macro is provided to allow easier creation of literal arrays, as shown below:
119//!
120//! ```rust
121//! # use generic_array::arr;
122//! let array = arr![1, 2, 3];
123//! //  array: GenericArray<i32, typenum::U3>
124//! assert_eq!(array[2], 3);
125//! ```
126//! ## Feature flags
127//!
128//! ```toml
129//! [dependencies.generic-array]
130//! features = [
131//!     "serde",            # Serialize/Deserialize implementation
132//!     "zeroize",          # Zeroize implementation for setting array elements to zero
133//!     "const-default",    # Compile-time const default value support via trait
134//!     "alloc",            # Enables From/TryFrom implementations between GenericArray and Vec<T>/Box<[T]>
135//!     "faster-hex",       # Enables internal use of the `faster-hex` crate for faster hex encoding via SIMD
136//!     "subtle",           # Enables `subtle` crate support for constant-time equality checks and conditional selection
137//!     "arbitrary",        # Enables `arbitrary` crate support for fuzzing
138//!     "bytemuck",         # Enables `bytemuck` crate support
139//!     "bitvec",           # Enables `bitvec` crate support to use GenericArray as a storage backend for bit arrays
140//!     "as_slice",         # Enables `as-slice` crate trait impls
141//!     "compat-0_14",      # Enables interoperability with `generic-array` 0.14
142//!     "hybrid-array-0_4"  # Enables interoperability with `hybrid-array` 0.4
143//! ]
144//! ```
145
146#![no_std]
147#![deny(missing_docs, meta_variable_misuse, clippy::missing_safety_doc)]
148#![cfg_attr(docsrs, feature(doc_cfg))]
149
150pub extern crate typenum;
151
152#[doc(hidden)]
153#[cfg(feature = "alloc")]
154pub extern crate alloc;
155
156mod compat;
157mod hex;
158mod impls;
159mod iter;
160
161mod ext_impls;
162
163/// `BitArray` type alias with `GenericArray` as the backing storage
164#[cfg(feature = "bitvec")]
165pub type GenericBitArray<T, N, O = bitvec::order::Lsb0> =
166    bitvec::array::BitArray<GenericArray<T, N>, O>;
167
168use core::cell::Cell;
169use core::iter::FromIterator;
170use core::marker::PhantomData;
171use core::mem::{ManuallyDrop, MaybeUninit};
172use core::ops::{Deref, DerefMut};
173use core::{mem, ptr, slice};
174use typenum::bit::{B0, B1};
175use typenum::generic_const_mappings::{Const, ToUInt};
176use typenum::uint::{UInt, UTerm, Unsigned};
177
178#[doc(hidden)]
179#[cfg_attr(test, macro_use)]
180pub mod arr;
181
182pub mod functional;
183pub mod sequence;
184
185mod internal;
186
187// re-export to allow doc_auto_cfg to handle it
188#[cfg(feature = "internals")]
189pub mod internals {
190    //! Very unsafe internal functionality.
191    //!
192    //! These are used internally for building and consuming generic arrays. When used correctly,
193    //! they can ensure elements are correctly dropped if something panics while using them.
194    //!
195    //! The API of these is not guaranteed to be stable, as they are not intended for general use.
196
197    pub use crate::internal::{IntrusiveArrayBuilder, IntrusiveArrayConsumer};
198
199    // soft-deprecated
200    pub use crate::internal::{ArrayBuilder, ArrayConsumer};
201}
202
203use internal::{IntrusiveArrayBuilder, IntrusiveArrayConsumer, Sealed};
204
205use self::functional::*;
206use self::sequence::*;
207
208pub use self::iter::GenericArrayIter;
209
210/// `ArrayLength` is a type-level [`Unsigned`] integer used to
211/// define the number of elements in a [`GenericArray`].
212///
213/// Consider `N: ArrayLength` to be equivalent to `const N: usize`
214///
215/// ```
216/// # use generic_array::{GenericArray, ArrayLength};
217/// fn foo<N: ArrayLength>(arr: GenericArray<i32, N>) -> i32 {
218///     arr.iter().sum()
219/// }
220/// ```
221/// is equivalent to:
222/// ```
223/// fn foo<const N: usize>(arr: [i32; N]) -> i32 {
224///     arr.iter().sum()
225/// }
226/// ```
227///
228/// # Safety
229///
230/// This trait is effectively sealed due to only being allowed on [`Unsigned`] types,
231/// and therefore cannot be implemented in user code.
232///
233/// Furthermore, this is limited to lengths less than or equal to `usize::MAX`.
234/// ```compile_fail
235/// # #![recursion_limit = "256"]
236/// # use generic_array::{GenericArray, ArrayLength};
237/// # use generic_array::typenum::{self, Unsigned};
238/// type Empty = core::convert::Infallible; // Uninhabited ZST, size_of::<Empty>() == 0
239///
240/// // 2^64, greater than usize::MAX on 64-bit systems
241/// type TooBig = typenum::operator_aliases::Shleft<typenum::U1, typenum::U64>;
242///
243/// // Compile Error due to ArrayLength not implemented for TooBig
244/// let _ = GenericArray::<Empty, TooBig>::from_slice(&[]);
245/// ```
246pub unsafe trait ArrayLength: Unsigned + 'static {
247    /// Associated type representing the underlying contiguous memory
248    /// that constitutes an array with the given number of elements.
249    ///
250    /// This is an implementation detail, but is required to be public in cases where certain attributes
251    /// of the inner type of [`GenericArray`] cannot be proven, such as [`Copy`] bounds.
252    ///
253    /// [`Copy`] example:
254    /// ```
255    /// # use generic_array::{GenericArray, ArrayLength};
256    /// struct MyType<N: ArrayLength> {
257    ///     data: GenericArray<f32, N>,
258    /// }
259    ///
260    /// impl<N: ArrayLength> Clone for MyType<N> where N::ArrayType<f32>: Copy {
261    ///     fn clone(&self) -> Self { MyType { ..*self } }
262    /// }
263    ///
264    /// impl<N: ArrayLength> Copy for MyType<N> where N::ArrayType<f32>: Copy {}
265    /// ```
266    ///
267    /// Alternatively, using the entire `GenericArray<f32, N>` type as the bounds works:
268    /// ```ignore
269    /// where GenericArray<f32, N>: Copy
270    /// ```
271    type ArrayType<T>: Sealed;
272}
273
274unsafe impl ArrayLength for UTerm {
275    #[doc(hidden)]
276    type ArrayType<T> = [T; 0];
277}
278
279/// Implemented for types which can have an associated [`ArrayLength`],
280/// such as [`Const<N>`] for use with const-generics.
281///
282/// ```
283/// use generic_array::{GenericArray, IntoArrayLength, ConstArrayLength, typenum::Const};
284///
285/// fn some_array_interopt<const N: usize>(value: [u32; N]) -> GenericArray<u32, ConstArrayLength<N>>
286/// where
287///     Const<N>: IntoArrayLength,
288/// {
289///     let ga = GenericArray::from(value);
290///     // do stuff
291///     ga
292/// }
293/// ```
294///
295/// This is mostly to simplify the `where` bounds, equivalent to:
296///
297/// ```
298/// use generic_array::{GenericArray, ArrayLength, typenum::{Const, U, ToUInt}};
299///
300/// fn some_array_interopt<const N: usize>(value: [u32; N]) -> GenericArray<u32, U<N>>
301/// where
302///     Const<N>: ToUInt,
303///     U<N>: ArrayLength,
304/// {
305///     let ga = GenericArray::from(value);
306///     // do stuff
307///     ga
308/// }
309/// ```
310pub trait IntoArrayLength {
311    /// The associated `ArrayLength`
312    type ArrayLength: ArrayLength;
313}
314
315impl<const N: usize> IntoArrayLength for Const<N>
316where
317    Const<N>: ToUInt,
318    typenum::U<N>: ArrayLength,
319{
320    type ArrayLength = typenum::U<N>;
321}
322
323impl<N> IntoArrayLength for N
324where
325    N: ArrayLength,
326{
327    type ArrayLength = Self;
328}
329
330/// Associated [`ArrayLength`] for one [`Const<N>`]
331///
332/// See [`IntoArrayLength`] for more information.
333///
334/// Note that not all `N` values are valid due to limitations inherent to `typenum` and Rust. You
335/// may need to combine [Const] with other typenum operations to get the desired length.
336pub type ConstArrayLength<const N: usize> = <Const<N> as IntoArrayLength>::ArrayLength;
337
338/// [`GenericArray`] with a const-generic `usize` length, using the [`ConstArrayLength`] type alias for `N`.
339///
340/// To construct from a literal array, use [`from_array`](GenericArray::from_array).
341///
342/// Note that not all `N` values are valid due to limitations inherent to `typenum` and Rust. You
343/// may need to combine [Const] with other typenum operations to get the desired length.
344pub type ConstGenericArray<T, const N: usize> = GenericArray<T, ConstArrayLength<N>>;
345
346/// Internal type used to generate a struct of appropriate size
347#[allow(dead_code)]
348#[repr(C)]
349#[doc(hidden)]
350pub struct GenericArrayImplEven<T, U> {
351    parents: [U; 2],
352    _marker: PhantomData<T>,
353}
354
355/// Internal type used to generate a struct of appropriate size
356#[allow(dead_code)]
357#[repr(C)]
358#[doc(hidden)]
359pub struct GenericArrayImplOdd<T, U> {
360    parents: [U; 2],
361    data: T,
362}
363
364impl<T: Clone, U: Clone> Clone for GenericArrayImplEven<T, U> {
365    #[inline(always)]
366    fn clone(&self) -> GenericArrayImplEven<T, U> {
367        // Clone is never called on the GenericArrayImpl types,
368        // as we use `self.map(clone)` elsewhere. This helps avoid
369        // extra codegen for recursive clones when they are never used.
370        unsafe { core::hint::unreachable_unchecked() }
371    }
372}
373
374impl<T: Clone, U: Clone> Clone for GenericArrayImplOdd<T, U> {
375    #[inline(always)]
376    fn clone(&self) -> GenericArrayImplOdd<T, U> {
377        unsafe { core::hint::unreachable_unchecked() }
378    }
379}
380
381// Even if Clone is never used, they can still be byte-copyable.
382impl<T: Copy, U: Copy> Copy for GenericArrayImplEven<T, U> {}
383impl<T: Copy, U: Copy> Copy for GenericArrayImplOdd<T, U> {}
384
385impl<T, U> Sealed for GenericArrayImplEven<T, U> {}
386impl<T, U> Sealed for GenericArrayImplOdd<T, U> {}
387
388// 1 << (size_of::<usize>() << 3) == usize::MAX + 1
389type MaxArrayLengthP1 = typenum::Shleft<
390    typenum::U1,
391    typenum::Shleft<typenum::U<{ mem::size_of::<usize>() }>, typenum::U3>,
392>;
393
394/// Helper trait to hide the complex bound under a simpler name
395trait IsWithinUsizeBound: typenum::IsLess<MaxArrayLengthP1, Output = typenum::consts::True> {}
396
397impl<N> IsWithinUsizeBound for N where
398    N: typenum::IsLess<MaxArrayLengthP1, Output = typenum::consts::True>
399{
400}
401
402unsafe impl<N: ArrayLength> ArrayLength for UInt<N, B0>
403where
404    Self: IsWithinUsizeBound,
405{
406    #[doc(hidden)]
407    type ArrayType<T> = GenericArrayImplEven<T, N::ArrayType<T>>;
408}
409
410unsafe impl<N: ArrayLength> ArrayLength for UInt<N, B1>
411where
412    Self: IsWithinUsizeBound,
413{
414    #[doc(hidden)]
415    type ArrayType<T> = GenericArrayImplOdd<T, N::ArrayType<T>>;
416}
417
418/// Struct representing a generic array - `GenericArray<T, N>` works like `[T; N]`
419///
420/// For how to implement [`Copy`] on structs using a generic-length `GenericArray` internally, see
421/// the docs for [`ArrayLength::ArrayType`].
422///
423/// # Usage Notes
424///
425/// ### Initialization
426///
427/// Initialization of known-length `GenericArray`s can be done via the [`arr![]`](arr!) macro,
428/// or [`from_array`](GenericArray::from_array)/[`from_slice`](GenericArray::from_slice).
429///
430/// For generic arrays of unknown/generic length, several safe methods are included to initialize
431/// them, such as the [`GenericSequence::generate`] method:
432///
433/// ```rust
434/// use generic_array::{GenericArray, sequence::GenericSequence, typenum, arr};
435///
436/// let evens: GenericArray<i32, typenum::U4> =
437///            GenericArray::generate(|i: usize| i as i32 * 2);
438///
439/// assert_eq!(evens, arr![0, 2, 4, 6]);
440/// ```
441///
442/// Furthermore, [`FromIterator`] and [`try_from_iter`](GenericArray::try_from_iter) exist to construct them
443/// from iterators, but will panic/fail if not given exactly the correct number of elements.
444///
445/// ### Utilities
446///
447/// The [`GenericSequence`], [`FunctionalSequence`], [`Lengthen`], [`Shorten`], [`Split`], and [`Concat`] traits implement
448/// some common operations on generic arrays.
449///
450/// ### Optimizations
451///
452/// Prefer to use the slice iterators like `.iter()`/`.iter_mut()` rather than by-value [`IntoIterator`]/[`GenericArrayIter`] if you can.
453/// Slices optimize better. Using the [`FunctionalSequence`] methods also optimize well.
454///
455/// # How it works
456///
457/// The `typenum` crate uses Rust's type system to define binary integers as nested types,
458/// and allows for operations which can be applied to those type-numbers, such as `Add`, `Sub`, etc.
459///
460/// e.g. `6` would be `UInt<UInt<UInt<UTerm, B1>, B1>, B0>`
461///
462/// `generic-array` uses this nested type to recursively allocate contiguous elements, statically.
463/// The [`ArrayLength`] trait is implemented on `UInt<N, B0>`, `UInt<N, B1>` and `UTerm`,
464/// which correspond to even, odd and zero numeric values, respectively.
465/// Together, these three cover all cases of `Unsigned` integers from `typenum`.
466/// For `UInt<N, B0>` and `UInt<N, B1>`, it peels away the highest binary digit and
467/// builds up a recursive structure that looks almost like a binary tree.
468/// Then, within `GenericArray`, the recursive structure is reinterpreted as a contiguous
469/// chunk of memory and allowing access to it as a slice.
470///
471/// <details>
472/// <summary><strong>Expand for internal structure demonstration</strong></summary>
473///
474/// For example, `GenericArray<T, U6>` more or less expands to (at compile time):
475///
476/// ```ignore
477/// GenericArray {
478///     // 6 = UInt<UInt<UInt<UTerm, B1>, B1>, B0>
479///     data: EvenData {
480///         // 3 = UInt<UInt<UTerm, B1>, B1>
481///         left: OddData {
482///             // 1 = UInt<UTerm, B1>
483///             left: OddData {
484///                 left: (),  // UTerm
485///                 right: (), // UTerm
486///                 data: T,   // Element 0
487///             },
488///             // 1 = UInt<UTerm, B1>
489///             right: OddData {
490///                 left: (),  // UTerm
491///                 right: (), // UTerm
492///                 data: T,   // Element 1
493///             },
494///             data: T        // Element 2
495///         },
496///         // 3 = UInt<UInt<UTerm, B1>, B1>
497///         right: OddData {
498///             // 1 = UInt<UTerm, B1>
499///             left: OddData {
500///                 left: (),  // UTerm
501///                 right: (), // UTerm
502///                 data: T,   // Element 3
503///             },
504///             // 1 = UInt<UTerm, B1>
505///             right: OddData {
506///                 left: (),  // UTerm
507///                 right: (), // UTerm
508///                 data: T,   // Element 4
509///             },
510///             data: T        // Element 5
511///         }
512///     }
513/// }
514/// ```
515///
516/// This has the added benefit of only being `log2(N)` deep, which is important for things like `Drop`
517/// to avoid stack overflows, since we can't implement `Drop` manually.
518///
519/// Then, we take the contiguous block of data and cast it to `*const T` or `*mut T` and use it as a slice:
520///
521/// ```ignore
522/// unsafe {
523///     slice::from_raw_parts(
524///         self as *const GenericArray<T, N> as *const T,
525///         <N as Unsigned>::USIZE
526///     )
527/// }
528/// ```
529///
530/// </details>
531#[repr(transparent)]
532pub struct GenericArray<T, N: ArrayLength> {
533    #[allow(dead_code)] // data is never accessed directly
534    data: N::ArrayType<T>,
535}
536
537unsafe impl<T: Send, N: ArrayLength> Send for GenericArray<T, N> {}
538unsafe impl<T: Sync, N: ArrayLength> Sync for GenericArray<T, N> {}
539
540impl<T, N: ArrayLength> Deref for GenericArray<T, N> {
541    type Target = [T];
542
543    #[inline(always)]
544    fn deref(&self) -> &[T] {
545        GenericArray::as_slice(self)
546    }
547}
548
549impl<T, N: ArrayLength> DerefMut for GenericArray<T, N> {
550    #[inline(always)]
551    fn deref_mut(&mut self) -> &mut [T] {
552        GenericArray::as_mut_slice(self)
553    }
554}
555
556impl<'a, T: 'a, N: ArrayLength> IntoIterator for &'a GenericArray<T, N> {
557    type IntoIter = slice::Iter<'a, T>;
558    type Item = &'a T;
559
560    #[inline]
561    fn into_iter(self: &'a GenericArray<T, N>) -> Self::IntoIter {
562        self.as_slice().iter()
563    }
564}
565
566impl<'a, T: 'a, N: ArrayLength> IntoIterator for &'a mut GenericArray<T, N> {
567    type IntoIter = slice::IterMut<'a, T>;
568    type Item = &'a mut T;
569
570    #[inline]
571    fn into_iter(self: &'a mut GenericArray<T, N>) -> Self::IntoIter {
572        self.as_mut_slice().iter_mut()
573    }
574}
575
576impl<T, N: ArrayLength> FromIterator<T> for GenericArray<T, N> {
577    /// Create a `GenericArray` from an iterator.
578    ///
579    /// Will panic if the number of elements is not exactly the array length.
580    ///
581    /// See [`GenericArray::try_from_iter`] for a fallible alternative.
582    #[inline]
583    fn from_iter<I>(iter: I) -> GenericArray<T, N>
584    where
585        I: IntoIterator<Item = T>,
586    {
587        match Self::try_from_iter(iter) {
588            Ok(res) => res,
589            Err(_) => from_iter_length_fail(N::USIZE),
590        }
591    }
592}
593
594#[inline(never)]
595#[cold]
596pub(crate) fn from_iter_length_fail(length: usize) -> ! {
597    panic!("GenericArray::from_iter expected {length} items");
598}
599
600unsafe impl<T, N: ArrayLength> GenericSequence<T> for GenericArray<T, N>
601where
602    Self: IntoIterator<Item = T>,
603{
604    type Length = N;
605    type Sequence = Self;
606
607    #[inline(always)]
608    fn generate<F>(mut f: F) -> GenericArray<T, N>
609    where
610        F: FnMut(usize) -> T,
611    {
612        unsafe {
613            let mut array = MaybeUninit::<GenericArray<T, N>>::uninit();
614            let mut builder = IntrusiveArrayBuilder::new_alt(&mut array);
615
616            let (builder_iter, position) = builder.iter_position();
617
618            builder_iter.enumerate().for_each(|(i, dst)| {
619                dst.write(f(i));
620                *position += 1;
621            });
622
623            builder.finish_and_assume_init()
624        }
625    }
626
627    #[inline(always)]
628    fn inverted_zip<B, U, F>(
629        self,
630        lhs: GenericArray<B, Self::Length>,
631        mut f: F,
632    ) -> MappedSequence<GenericArray<B, Self::Length>, B, U>
633    where
634        GenericArray<B, Self::Length>:
635            GenericSequence<B, Length = Self::Length> + MappedGenericSequence<B, U>,
636        Self: MappedGenericSequence<T, U>,
637        F: FnMut(B, Self::Item) -> U,
638    {
639        unsafe {
640            let mut left = ManuallyDrop::new(lhs);
641            let mut right = ManuallyDrop::new(self);
642
643            if mem::needs_drop::<T>() || mem::needs_drop::<B>() {
644                let mut left = IntrusiveArrayConsumer::new(&mut left);
645                let mut right = IntrusiveArrayConsumer::new(&mut right);
646
647                let (left_array_iter, left_position) = left.iter_position();
648                let (right_array_iter, right_position) = right.iter_position();
649
650                FromIterator::from_iter(left_array_iter.zip(right_array_iter).map(|(l, r)| {
651                    let left_value = ptr::read(l);
652                    let right_value = ptr::read(r);
653
654                    *left_position += 1;
655                    *right_position = *left_position;
656
657                    f(left_value, right_value)
658                }))
659            } else {
660                // Neither right nor left require `Drop` be called, so choose an iterator that's easily optimized,
661                // though we still keep them in `ManuallyDrop` out of paranoia.
662                //
663                // Note that because ArrayConsumer checks for `needs_drop` itself, if `f` panics then nothing
664                // would have been done about it anyway. Only the other branch needs `ArrayConsumer`
665                FromIterator::from_iter(left.iter().zip(right.iter()).map(|(l, r)| {
666                    f(ptr::read(l), ptr::read(r)) //
667                }))
668            }
669        }
670    }
671
672    #[inline(always)]
673    fn inverted_zip2<B, Lhs, U, F>(self, lhs: Lhs, mut f: F) -> MappedSequence<Lhs, B, U>
674    where
675        Lhs: GenericSequence<B, Length = Self::Length> + MappedGenericSequence<B, U>,
676        Self: MappedGenericSequence<T, U>,
677        F: FnMut(Lhs::Item, Self::Item) -> U,
678    {
679        unsafe {
680            if mem::needs_drop::<T>() {
681                let mut right = ManuallyDrop::new(self);
682                let mut right = IntrusiveArrayConsumer::new(&mut right);
683
684                let (right_array_iter, right_position) = right.iter_position();
685
686                FromIterator::from_iter(right_array_iter.zip(lhs).map(|(r, left_value)| {
687                    let right_value = ptr::read(r);
688
689                    *right_position += 1;
690
691                    f(left_value, right_value)
692                }))
693            } else {
694                let right = ManuallyDrop::new(self);
695
696                // Similar logic to `inverted_zip`'s no-drop branch
697                FromIterator::from_iter(right.iter().zip(lhs).map(|(r, left_value)| {
698                    f(left_value, ptr::read(r)) //
699                }))
700            }
701        }
702    }
703}
704
705impl<T, N: ArrayLength> FromFallibleIterator<T> for GenericArray<T, N> {
706    #[inline(always)]
707    fn from_fallible_iter<I, E>(iter: I) -> Result<Self, E>
708    where
709        I: IntoIterator<Item = Result<T, E>>,
710    {
711        match Self::try_from_fallible_iter(iter) {
712            Ok(res) => res,
713            Err(_) => from_iter_length_fail(N::USIZE),
714        }
715    }
716}
717
718unsafe impl<T, N: ArrayLength> FallibleGenericSequence<T> for GenericArray<T, N>
719where
720    Self: IntoIterator<Item = T>,
721{
722    type Error = core::convert::Infallible;
723
724    #[inline(always)]
725    fn try_generate<F, E>(mut f: F) -> Result<Result<Self::Sequence, E>, Self::Error>
726    where
727        F: FnMut(usize) -> Result<T, E>,
728    {
729        unsafe {
730            let mut array = MaybeUninit::<GenericArray<T, N>>::uninit();
731            let mut builder = IntrusiveArrayBuilder::new_alt(&mut array);
732
733            let (builder_iter, position) = builder.iter_position();
734
735            if let Err(e) = builder_iter
736                .enumerate()
737                .try_for_each(|(i, dst)| match f(i) {
738                    // NOTE: Using a match here instead of ? results in better codegen
739                    Ok(value) => {
740                        dst.write(value);
741                        *position += 1;
742                        Ok(())
743                    }
744                    Err(e) => Err(e),
745                })
746            {
747                drop(builder); // explicitly drop to run the destructor and drop any initialized elements
748
749                return Ok(Err(e));
750            }
751
752            Ok(Ok(builder.finish_and_assume_init()))
753        }
754    }
755}
756
757impl<T, U, N: ArrayLength> MappedGenericSequence<T, U> for GenericArray<T, N>
758where
759    GenericArray<U, N>: GenericSequence<U, Length = N>,
760{
761    type Mapped = GenericArray<U, N>;
762}
763
764impl<T, N: ArrayLength> FunctionalSequence<T> for GenericArray<T, N>
765where
766    Self: GenericSequence<T, Item = T, Length = N>,
767{
768    #[inline(always)]
769    fn map<U, F>(self, mut f: F) -> MappedSequence<Self, T, U>
770    where
771        Self: MappedGenericSequence<T, U>,
772        F: FnMut(T) -> U,
773    {
774        unsafe {
775            let mut array = ManuallyDrop::new(self);
776            let mut source = IntrusiveArrayConsumer::new(&mut array);
777
778            let (array_iter, position) = source.iter_position();
779
780            FromIterator::from_iter(array_iter.map(|src| {
781                let value = ptr::read(src);
782
783                *position += 1;
784
785                f(value)
786            }))
787        }
788    }
789
790    #[inline(always)]
791    fn try_map<U, F, E>(self, mut f: F) -> Result<MappedSequence<Self, T, U>, E>
792    where
793        Self: MappedGenericSequence<T, U>,
794        MappedSequence<Self, T, U>: FromFallibleIterator<U>,
795        F: FnMut(Self::Item) -> Result<U, E>,
796    {
797        unsafe {
798            let mut array = ManuallyDrop::new(self);
799            let mut source = IntrusiveArrayConsumer::new(&mut array);
800
801            let (array_iter, position) = source.iter_position();
802
803            FromFallibleIterator::from_fallible_iter(array_iter.map(|src| {
804                let value = ptr::read(src);
805                *position += 1;
806                f(value)
807            }))
808        }
809    }
810
811    #[inline(always)]
812    fn zip<B, Rhs, U, F>(self, rhs: Rhs, f: F) -> MappedSequence<Self, T, U>
813    where
814        Self: MappedGenericSequence<T, U>,
815        Rhs: MappedGenericSequence<B, U, Mapped = MappedSequence<Self, T, U>>,
816        Rhs: GenericSequence<B, Length = Self::Length>,
817        F: FnMut(T, Rhs::Item) -> U,
818    {
819        rhs.inverted_zip(self, f)
820    }
821
822    #[inline(always)]
823    fn fold<U, F>(self, init: U, mut f: F) -> U
824    where
825        F: FnMut(U, T) -> U,
826    {
827        unsafe {
828            let mut array = ManuallyDrop::new(self);
829            let mut source = IntrusiveArrayConsumer::new(&mut array);
830
831            let (array_iter, position) = source.iter_position();
832
833            array_iter.fold(init, |acc, src| {
834                let value = ptr::read(src);
835                *position += 1;
836                f(acc, value)
837            })
838        }
839    }
840
841    #[inline(always)]
842    fn try_fold<U, E, F>(self, init: U, mut f: F) -> Result<U, E>
843    where
844        F: FnMut(U, Self::Item) -> Result<U, E>,
845    {
846        unsafe {
847            let mut array = ManuallyDrop::new(self);
848            let mut source = IntrusiveArrayConsumer::new(&mut array);
849
850            let (mut array_iter, position) = source.iter_position();
851
852            array_iter.try_fold(init, |acc, src| {
853                let value = ptr::read(src);
854                *position += 1;
855                f(acc, value)
856            })
857        }
858    }
859}
860
861impl<T, N: ArrayLength> GenericArray<T, N> {
862    /// Returns the number of elements in the array.
863    ///
864    /// Equivalent to [`<N as Unsigned>::USIZE`](typenum::Unsigned) where `N` is the array length.
865    ///
866    /// Useful for when only a type alias is available.
867    pub const fn len() -> usize {
868        N::USIZE
869    }
870
871    /// Extracts a slice containing the entire array.
872    #[inline(always)]
873    pub const fn as_slice(&self) -> &[T] {
874        unsafe { slice::from_raw_parts(self as *const Self as *const T, N::USIZE) }
875    }
876
877    /// Extracts a mutable slice containing the entire array.
878    ///
879    /// This method is `const` since Rust 1.83.0, but non-`const` before.
880    #[rustversion::attr(since(1.83), const)]
881    #[inline(always)]
882    pub fn as_mut_slice(&mut self) -> &mut [T] {
883        unsafe { slice::from_raw_parts_mut(self as *mut Self as *mut T, N::USIZE) }
884    }
885
886    /// Converts a slice to a generic array reference with inferred length.
887    ///
888    /// # Panics
889    ///
890    /// Panics if the slice is not equal to the length of the array.
891    ///
892    /// Consider [`TryFrom`]/[`TryInto`] for a fallible conversion,
893    /// or [`try_from_slice`](GenericArray::try_from_slice) for use in const expressions.
894    #[inline(always)]
895    pub const fn from_slice(slice: &[T]) -> &GenericArray<T, N> {
896        if slice.len() != N::USIZE {
897            panic!("slice.len() != N in GenericArray::from_slice");
898        }
899
900        unsafe { &*(slice.as_ptr() as *const GenericArray<T, N>) }
901    }
902
903    /// Converts a slice to a generic array reference with inferred length.
904    ///
905    /// This is a fallible alternative to [`from_slice`](GenericArray::from_slice), and can be used in const expressions,
906    /// but [`TryFrom`]/[`TryInto`] are also available to do the same thing.
907    #[inline(always)]
908    pub const fn try_from_slice(slice: &[T]) -> Result<&GenericArray<T, N>, LengthError> {
909        if slice.len() != N::USIZE {
910            return Err(LengthError);
911        }
912
913        Ok(unsafe { &*(slice.as_ptr() as *const GenericArray<T, N>) })
914    }
915
916    /// Converts a mutable slice to a mutable generic array reference with inferred length.
917    ///
918    /// # Panics
919    ///
920    /// Panics if the slice is not equal to the length of the array.
921    ///
922    /// Consider [`TryFrom`]/[`TryInto`] for a fallible conversion.
923    ///
924    /// This method is `const` since Rust 1.83.0, but non-`const` before.
925    #[rustversion::attr(since(1.83), const)]
926    #[inline(always)]
927    pub fn from_mut_slice(slice: &mut [T]) -> &mut GenericArray<T, N> {
928        assert!(
929            slice.len() == N::USIZE,
930            "slice.len() != N in GenericArray::from_mut_slice"
931        );
932
933        unsafe { &mut *(slice.as_mut_ptr() as *mut GenericArray<T, N>) }
934    }
935
936    /// Converts a mutable slice to a mutable generic array reference with inferred length.
937    ///
938    /// This is a fallible alternative to [`from_mut_slice`](GenericArray::from_mut_slice),
939    /// and is equivalent to the [`TryFrom`] implementation with the added benefit of being `const`.
940    ///
941    /// This method is `const` since Rust 1.83.0, but non-`const` before.
942    #[rustversion::attr(since(1.83), const)]
943    #[inline(always)]
944    pub fn try_from_mut_slice(slice: &mut [T]) -> Result<&mut GenericArray<T, N>, LengthError> {
945        match slice.len() == N::USIZE {
946            true => Ok(GenericArray::from_mut_slice(slice)),
947            false => Err(LengthError),
948        }
949    }
950
951    /// Borrows each element and returns a `GenericArray` of references
952    /// with the same length as `self`.
953    ///
954    /// This method is const since Rust 1.83.0, but non-const before.
955    ///
956    /// See also [`each_mut`](GenericArray::each_mut) for mutable references.
957    ///
958    /// # Example
959    ///
960    /// ```
961    /// # use generic_array::{arr, GenericArray};
962    /// let ga = arr![1, 2, 3];
963    /// let refs: GenericArray<&i32, _> = ga.each_ref();
964    /// assert_eq!(*refs[0], 1);
965    /// assert_eq!(*refs[1], 2);
966    /// assert_eq!(*refs[2], 3);
967    /// ```
968    #[rustversion::attr(since(1.83), const)] // needed for `as_mut_slice` to be const
969    pub fn each_ref(&self) -> GenericArray<&T, N> {
970        let mut out: GenericArray<MaybeUninit<*const T>, N> = GenericArray::uninit();
971
972        {
973            // only slices allow `const` indexing
974            let (this, out) = (self.as_slice(), out.as_mut_slice());
975
976            let mut i = 0;
977            while i < N::USIZE {
978                out[i].write(ptr::addr_of!(this[i]));
979                i += 1;
980            }
981        }
982
983        // SAFETY: `*const T` has the same layout as `&T`, and we've also initialized each pointer as a valid reference.
984        unsafe { const_transmute(out) }
985    }
986
987    /// Borrows each element mutably and returns a `GenericArray` of mutable references
988    /// with the same length as `self`.
989    ///
990    /// This method is const since Rust 1.83.0, but non-const before.
991    ///
992    /// # Example
993    ///
994    /// ```
995    /// # use generic_array::{arr, GenericArray};
996    /// let mut ga = arr![1, 2, 3];
997    /// let mut_refs: GenericArray<&mut i32, _> = ga.each_mut();
998    /// for r in mut_refs {
999    ///     *r *= 2;
1000    /// }
1001    /// assert_eq!(ga, arr![2, 4, 6]);
1002    /// ```
1003    #[rustversion::attr(since(1.83), const)]
1004    pub fn each_mut(&mut self) -> GenericArray<&mut T, N> {
1005        let mut out: GenericArray<MaybeUninit<*mut T>, N> = GenericArray::uninit();
1006
1007        {
1008            // only slices allow `const` indexing
1009            let (this, out) = (self.as_mut_slice(), out.as_mut_slice());
1010
1011            let mut i = 0;
1012            while i < N::USIZE {
1013                out[i].write(ptr::addr_of_mut!(this[i]));
1014                i += 1;
1015            }
1016        }
1017
1018        // SAFETY: `*mut T` has the same layout as `&mut T`, and we've also initialized each pointer as a valid reference.
1019        unsafe { const_transmute(out) }
1020    }
1021
1022    /// Converts a slice of `T` elements into a slice of `GenericArray<T, N>` chunks.
1023    ///
1024    /// Any remaining elements that do not fill the array will be returned as a second slice.
1025    ///
1026    /// # Panics
1027    ///
1028    /// Panics if `N` is `U0` _AND_ the input slice is not empty.
1029    pub const fn chunks_from_slice(slice: &[T]) -> (&[GenericArray<T, N>], &[T]) {
1030        if N::USIZE == 0 {
1031            assert!(slice.is_empty(), "GenericArray length N must be non-zero");
1032            return (&[], &[]);
1033        }
1034
1035        // NOTE: Using `slice.split_at` adds an unnecessary assert
1036        let num_chunks = slice.len() / N::USIZE; // integer division
1037        let num_in_chunks = num_chunks * N::USIZE;
1038        let num_remainder = slice.len() - num_in_chunks;
1039
1040        unsafe {
1041            (
1042                slice::from_raw_parts(slice.as_ptr() as *const GenericArray<T, N>, num_chunks),
1043                slice::from_raw_parts(slice.as_ptr().add(num_in_chunks), num_remainder),
1044            )
1045        }
1046    }
1047
1048    /// Converts a mutable slice of `T` elements into a mutable slice `GenericArray<T, N>` chunks.
1049    ///
1050    /// Any remaining elements that do not fill the array will be returned as a second slice.
1051    ///
1052    /// # Panics
1053    ///
1054    /// Panics if `N` is `U0` _AND_ the input slice is not empty.
1055    ///
1056    /// This method is `const` since Rust 1.83.0, but non-`const` before.
1057    #[rustversion::attr(since(1.83), const)]
1058    pub fn chunks_from_slice_mut(slice: &mut [T]) -> (&mut [GenericArray<T, N>], &mut [T]) {
1059        if N::USIZE == 0 {
1060            assert!(slice.is_empty(), "GenericArray length N must be non-zero");
1061            return (&mut [], &mut []);
1062        }
1063
1064        // NOTE: Using `slice.split_at_mut` adds an unnecessary assert
1065        let num_chunks = slice.len() / N::USIZE; // integer division
1066        let num_in_chunks = num_chunks * N::USIZE;
1067        let num_remainder = slice.len() - num_in_chunks;
1068
1069        unsafe {
1070            (
1071                slice::from_raw_parts_mut(
1072                    slice.as_mut_ptr() as *mut GenericArray<T, N>,
1073                    num_chunks,
1074                ),
1075                slice::from_raw_parts_mut(slice.as_mut_ptr().add(num_in_chunks), num_remainder),
1076            )
1077        }
1078    }
1079
1080    /// Convert a slice of `GenericArray<T, N>` into a slice of `T`, effectively flattening the arrays.
1081    #[inline(always)]
1082    pub const fn slice_from_chunks(slice: &[GenericArray<T, N>]) -> &[T] {
1083        unsafe { slice::from_raw_parts(slice.as_ptr() as *const T, slice.len() * N::USIZE) }
1084    }
1085
1086    /// Convert a slice of `GenericArray<T, N>` into a slice of `T`, effectively flattening the arrays.
1087    ///
1088    /// This method is `const` since Rust 1.83.0, but non-`const` before.
1089    #[rustversion::attr(since(1.83), const)]
1090    #[inline(always)]
1091    pub fn slice_from_chunks_mut(slice: &mut [GenericArray<T, N>]) -> &mut [T] {
1092        unsafe { slice::from_raw_parts_mut(slice.as_mut_ptr() as *mut T, slice.len() * N::USIZE) }
1093    }
1094
1095    /// Convert a native array into `GenericArray` of the same length and type.
1096    ///
1097    /// This is the `const` equivalent of using the standard [`From`]/[`Into`] traits methods.
1098    #[inline(always)]
1099    pub const fn from_array<const U: usize>(value: [T; U]) -> Self
1100    where
1101        Const<U>: IntoArrayLength<ArrayLength = N>,
1102    {
1103        unsafe { crate::const_transmute(value) }
1104    }
1105
1106    /// Convert the `GenericArray` into a native array of the same length and type.
1107    ///
1108    /// This is the `const` equivalent of using the standard [`From`]/[`Into`] traits methods.
1109    #[inline(always)]
1110    pub const fn into_array<const U: usize>(self) -> [T; U]
1111    where
1112        Const<U>: IntoArrayLength<ArrayLength = N>,
1113    {
1114        unsafe { crate::const_transmute(self) }
1115    }
1116
1117    /// Convert a slice of native arrays into a slice of `GenericArray`s.
1118    #[inline(always)]
1119    pub const fn from_chunks<const U: usize>(chunks: &[[T; U]]) -> &[GenericArray<T, N>]
1120    where
1121        Const<U>: IntoArrayLength<ArrayLength = N>,
1122    {
1123        unsafe { mem::transmute(chunks) }
1124    }
1125
1126    /// Convert a mutable slice of native arrays into a mutable slice of `GenericArray`s.
1127    ///
1128    /// This method is `const` since Rust 1.83.0, but non-`const` before.
1129    #[rustversion::attr(since(1.83), const)]
1130    #[inline(always)]
1131    pub fn from_chunks_mut<const U: usize>(chunks: &mut [[T; U]]) -> &mut [GenericArray<T, N>]
1132    where
1133        Const<U>: IntoArrayLength<ArrayLength = N>,
1134    {
1135        unsafe { mem::transmute(chunks) }
1136    }
1137
1138    /// Converts a slice `GenericArray<T, N>` into a slice of `[T; N]`
1139    #[inline(always)]
1140    pub const fn into_chunks<const U: usize>(chunks: &[GenericArray<T, N>]) -> &[[T; U]]
1141    where
1142        Const<U>: IntoArrayLength<ArrayLength = N>,
1143    {
1144        unsafe { mem::transmute(chunks) }
1145    }
1146
1147    /// Converts a mutable slice `GenericArray<T, N>` into a mutable slice of `[T; N]`
1148    ///
1149    /// This method is `const` since Rust 1.83.0, but non-`const` before.
1150    #[rustversion::attr(since(1.83), const)]
1151    #[inline(always)]
1152    pub fn into_chunks_mut<const U: usize>(chunks: &mut [GenericArray<T, N>]) -> &mut [[T; U]]
1153    where
1154        Const<U>: IntoArrayLength<ArrayLength = N>,
1155    {
1156        unsafe { mem::transmute(chunks) }
1157    }
1158
1159    /// Returns a `&GenericArray<Cell<T>, N>` from a `&Cell<GenericArray<T, N>>`.
1160    #[inline(always)]
1161    pub const fn as_array_of_cells(cell: &Cell<GenericArray<T, N>>) -> &GenericArray<Cell<T>, N> {
1162        // SAFETY: `Cell<T>` has the same memory layout as `T`.
1163        unsafe { &*(cell as *const Cell<GenericArray<T, N>> as *const GenericArray<Cell<T>, N>) }
1164    }
1165}
1166
1167impl<T, N: ArrayLength> GenericArray<T, N> {
1168    /// Create a new array of `MaybeUninit<T>` items, in an uninitialized state.
1169    ///
1170    /// See [`GenericArray::assume_init`] for a full example.
1171    #[inline(always)]
1172    #[allow(clippy::uninit_assumed_init)]
1173    pub const fn uninit() -> GenericArray<MaybeUninit<T>, N> {
1174        unsafe {
1175            // SAFETY: An uninitialized `[MaybeUninit<_>; N]` is valid, same as regular array
1176            MaybeUninit::<GenericArray<MaybeUninit<T>, N>>::uninit().assume_init()
1177        }
1178    }
1179
1180    /// Extracts the values from a generic array of `MaybeUninit` containers.
1181    ///
1182    /// # Safety
1183    ///
1184    /// It is up to the caller to guarantee that all elements of the array are in an initialized state.
1185    ///
1186    /// # Example
1187    ///
1188    /// ```
1189    /// # use core::mem::MaybeUninit;
1190    /// # use generic_array::{GenericArray, typenum::U3, arr};
1191    /// let mut array: GenericArray<MaybeUninit<i32>, U3> = GenericArray::uninit();
1192    /// array[0].write(0);
1193    /// array[1].write(1);
1194    /// array[2].write(2);
1195    ///
1196    /// // SAFETY: Now safe as we initialised all elements
1197    /// let array = unsafe {
1198    ///     GenericArray::assume_init(array)
1199    /// };
1200    ///
1201    /// assert_eq!(array, arr![0, 1, 2]);
1202    /// ```
1203    #[inline(always)]
1204    pub const unsafe fn assume_init(array: GenericArray<MaybeUninit<T>, N>) -> Self {
1205        const_transmute::<GenericArray<MaybeUninit<T>, N>, GenericArray<T, N>>(array)
1206    }
1207}
1208
1209/// Error type for [`TryFrom`] and [`try_from_iter`](GenericArray::try_from_iter) implementations.
1210#[derive(Debug, Clone, Copy)]
1211pub struct LengthError;
1212
1213#[rustversion::since(1.81)]
1214impl core::error::Error for LengthError {}
1215
1216impl core::fmt::Display for LengthError {
1217    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
1218        f.write_str("LengthError: Slice or iterator does not match GenericArray length")
1219    }
1220}
1221
1222/// Error type for heap allocation failures.
1223///
1224/// Returned by [`FallibleGenericSequence::try_generate`](sequence::FallibleGenericSequence::try_generate)
1225/// on `Box<GenericArray<T, N>>` when the underlying allocation fails.
1226#[cfg(feature = "alloc")]
1227#[derive(Debug, Clone, Copy, PartialEq, Eq)]
1228pub struct AllocError;
1229
1230#[cfg(feature = "alloc")]
1231#[rustversion::since(1.81)]
1232impl core::error::Error for AllocError {}
1233
1234#[cfg(feature = "alloc")]
1235impl core::fmt::Display for AllocError {
1236    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
1237        f.write_str("memory allocation failed")
1238    }
1239}
1240
1241impl<'a, T, N: ArrayLength> TryFrom<&'a [T]> for &'a GenericArray<T, N> {
1242    type Error = LengthError;
1243
1244    #[inline(always)]
1245    fn try_from(slice: &'a [T]) -> Result<Self, Self::Error> {
1246        GenericArray::try_from_slice(slice)
1247    }
1248}
1249
1250impl<'a, T, N: ArrayLength> TryFrom<&'a mut [T]> for &'a mut GenericArray<T, N> {
1251    type Error = LengthError;
1252
1253    #[inline(always)]
1254    fn try_from(slice: &'a mut [T]) -> Result<Self, Self::Error> {
1255        GenericArray::try_from_mut_slice(slice)
1256    }
1257}
1258
1259impl<T, N: ArrayLength> GenericArray<T, N> {
1260    /// Fallible equivalent of [`FromIterator::from_iter`]
1261    ///
1262    /// Given iterator must yield exactly `N` elements or an error will be returned. Using [`.take(N)`](Iterator::take)
1263    /// with an iterator longer than the array may be helpful.
1264    #[inline]
1265    pub fn try_from_iter<I>(iter: I) -> Result<Self, LengthError>
1266    where
1267        I: IntoIterator<Item = T>,
1268    {
1269        let mut iter = iter.into_iter();
1270
1271        // pre-checks
1272        match iter.size_hint() {
1273            // if the lower bound is greater than N, array will overflow
1274            (n, _) if n > N::USIZE => return Err(LengthError),
1275            // if the upper bound is smaller than N, array cannot be filled
1276            (_, Some(n)) if n < N::USIZE => return Err(LengthError),
1277            _ => {}
1278        }
1279
1280        unsafe {
1281            let mut array = MaybeUninit::<GenericArray<T, N>>::uninit();
1282            let mut builder = IntrusiveArrayBuilder::new_alt(&mut array);
1283
1284            builder.extend(&mut iter);
1285
1286            if !builder.is_full() || iter.next().is_some() {
1287                return Err(LengthError);
1288            }
1289
1290            Ok(builder.finish_and_assume_init())
1291        }
1292    }
1293
1294    /// Fallible equivalent of [`FromFallibleIterator::from_fallible_iter`].
1295    ///
1296    /// Unlike `.collect::<Result<GenericArray<T, N>, E>>()`, this method will not panic
1297    /// on length mismatch, instead returning a `LengthError`.
1298    ///
1299    /// Given iterator must yield exactly `N` elements or an error will be returned. Using [`.take(N)`](Iterator::take)
1300    /// with an iterator longer than the array may be helpful.
1301    #[inline]
1302    pub fn try_from_fallible_iter<I, E>(iter: I) -> Result<Result<Self, E>, LengthError>
1303    where
1304        I: IntoIterator<Item = Result<T, E>>,
1305    {
1306        let mut iter = iter.into_iter();
1307
1308        // pre-checks
1309        match iter.size_hint() {
1310            // if the lower bound is greater than N, array will overflow
1311            (n, _) if n > N::USIZE => return Err(LengthError),
1312            // if the upper bound is smaller than N, array cannot be filled
1313            (_, Some(n)) if n < N::USIZE => return Err(LengthError),
1314            _ => {}
1315        }
1316
1317        unsafe {
1318            let mut array = MaybeUninit::<GenericArray<T, N>>::uninit();
1319            let mut builder = IntrusiveArrayBuilder::new_alt(&mut array);
1320
1321            if let Err(e) = builder.try_extend(&mut iter) {
1322                drop(builder); // explicitly drop to run the destructor and drop any initialized elements
1323
1324                return Ok(Err(e));
1325            }
1326
1327            if !builder.is_full() || iter.next().is_some() {
1328                return Err(LengthError);
1329            }
1330
1331            Ok(Ok(builder.finish_and_assume_init()))
1332        }
1333    }
1334}
1335
1336/// A const reimplementation of the [`transmute`](core::mem::transmute) function,
1337/// avoiding problems when the compiler can't prove equal sizes for some reason.
1338///
1339/// This will still check that the sizes of `A` and `B` are equal at compile time:
1340/// ```compile_fail
1341/// # use generic_array::const_transmute;
1342///
1343/// let _ = unsafe { const_transmute::<u32, u64>(0u32) }; // panics at compile time
1344/// ```
1345///
1346/// # Safety
1347/// Treat this the same as [`transmute`](core::mem::transmute), or (preferably) don't use it at all.
1348#[inline(always)]
1349#[cfg_attr(not(feature = "internals"), doc(hidden))]
1350pub const unsafe fn const_transmute<A, B>(a: A) -> B {
1351    struct SizeAsserter<A, B>(PhantomData<(A, B)>);
1352
1353    impl<A, B> SizeAsserter<A, B> {
1354        const ASSERT_SIZE_EQUALITY: () = {
1355            if mem::size_of::<A>() != mem::size_of::<B>() {
1356                panic!("Size mismatch for generic_array::const_transmute");
1357            }
1358        };
1359    }
1360
1361    let () = SizeAsserter::<A, B>::ASSERT_SIZE_EQUALITY;
1362
1363    #[rustversion::since(1.83)]
1364    #[inline(always)]
1365    const unsafe fn do_transmute<A, B>(a: ManuallyDrop<A>) -> B {
1366        mem::transmute_copy(&a)
1367    }
1368
1369    #[rustversion::before(1.83)]
1370    #[inline(always)]
1371    const unsafe fn do_transmute<A, B>(a: ManuallyDrop<A>) -> B {
1372        #[repr(C)]
1373        union Union<A, B> {
1374            a: ManuallyDrop<A>,
1375            b: ManuallyDrop<B>,
1376        }
1377
1378        ManuallyDrop::into_inner(Union { a }.b)
1379    }
1380
1381    do_transmute(ManuallyDrop::new(a))
1382}
1383
1384#[cfg(test)]
1385mod test {
1386    // Compile with:
1387    // cargo rustc --lib --profile test --release --
1388    //      -C target-cpu=native -C opt-level=3 --emit asm
1389    // and view the assembly to make sure test_assembly generates
1390    // SIMD instructions instead of a naive loop.
1391
1392    #[inline(never)]
1393    pub fn black_box<T>(val: T) -> T {
1394        use core::{mem, ptr};
1395
1396        let ret = unsafe { ptr::read_volatile(&val) };
1397        mem::forget(val);
1398        ret
1399    }
1400
1401    #[test]
1402    fn test_assembly() {
1403        use crate::functional::*;
1404
1405        let a = black_box(arr![1, 3, 5, 7]);
1406        let b = black_box(arr![2, 4, 6, 8]);
1407
1408        let c = (&a).zip(b, |l, r| l + r);
1409
1410        let d = a.fold(0, |a, x| a + x);
1411
1412        assert_eq!(c, arr![3, 7, 11, 15]);
1413
1414        assert_eq!(d, 16);
1415    }
1416}