Skip to main content

array_reshape/
lib.rs

1#![no_std]
2#![cfg_attr(feature = "const-trait", feature(const_trait_impl))]
3
4//! Reshaping of arrays, by value, by reference and by mutable reference, all in `const fn`.
5//!
6//! [`<[[T; N]]>::as_flattened()`](slice::as_flattened) and
7//! [`<[T]>::as_chunks()`](slice::as_chunks) in the standard library convert to and from slices,
8//! losing the length in the process. This crate converts between arrays of the same total length
9//! instead, for example `[[T; N]; M]` into `[T; N * M]`:
10//!
11//! ```
12//! use array_reshape::{Flatten, Rechunk, Unflatten};
13//!
14//! let pairs = [[1, 2], [3, 4], [5, 6]];
15//!
16//! let flat: &[u8; 6] = pairs.flatten_ref();
17//! assert_eq!(flat, &[1, 2, 3, 4, 5, 6]);
18//!
19//! let triples = pairs.rechunk_ref::<3, 2>();
20//! assert_eq!(triples, &[[1, 2, 3], [4, 5, 6]]);
21//!
22//! let pairs_again = flat.unflatten::<2, 3>();
23//! assert_eq!(pairs_again, pairs);
24//! ```
25//!
26//! Slices and arrays of arrays can be reshaped element by element too, arrays keep their length:
27//!
28//! ```
29//! use array_reshape::{FlattenEach, UnflattenEach};
30//!
31//! let bytes = [[1, 2, 3, 4], [5, 6, 7, 8]];
32//!
33//! let pairs: &[[[u8; 2]; 2]; 2] = bytes.unflatten_each_ref();
34//! assert_eq!(pairs, &[[[1, 2], [3, 4]], [[5, 6], [7, 8]]]);
35//!
36//! let bytes_again: &[[u8; 4]] = pairs.as_slice().flatten_each_ref();
37//! assert_eq!(bytes_again, &bytes);
38//! ```
39//!
40//! # `const fn` support
41//!
42//! Trait methods can't be called in `const fn` on stable Rust, which is why every conversion is
43//! also available as a free `const fn` with the same name:
44//!
45//! ```
46//! const FLAT: [u8; 4] = array_reshape::flatten([[1, 2], [3, 4]]);
47//! assert_eq!(FLAT, [1, 2, 3, 4]);
48//! ```
49//!
50//! With the `const-trait` feature, which requires nightly Rust, all traits become `const trait`s,
51//! so their methods can be called in `const fn` directly. Crates calling them in `const fn` need to
52//! enable `const_trait_impl` feature themselves.
53//!
54//! # Output length
55//!
56//! Stable Rust doesn't allow computing array lengths from generic constants yet (which would
57//! require `generic_const_exprs` feature), so the length of the output is a separate generic
58//! parameter, which is usually inferred from the context. The lengths are checked at compile
59//! time, but since the check happens after monomorphization, it is only reported by
60//! `cargo build`, not `cargo check`:
61//!
62//! ```compile_fail
63//! use array_reshape::Flatten;
64//!
65//! let pairs = [[1, 2], [3, 4], [5, 6]];
66//!
67//! let flat: &[u8; 5] = pairs.flatten_ref();
68//! ```
69//!
70//! The check also happens for code that is never executed, but is compiled, like `match` arms
71//! that don't match for a particular set of generic parameters. Convert the input into an array
72//! of a concrete size in such cases first (for example with
73//! [`<[T]>::as_array()`](slice::as_array)).
74
75use core::mem::ManuallyDrop;
76use core::{mem, ptr, slice};
77
78/// Checks at compile time that `inner * outer == len`
79#[inline(always)]
80const fn assert_len<const INNER: usize, const OUTER: usize, const LEN: usize>() {
81    const {
82        assert!(
83            matches!(INNER.checked_mul(OUTER), Some(len) if len == LEN),
84            "Arrays must have the same total number of elements"
85        );
86    }
87}
88
89/// Moves `src` into `Dst` of the same size and layout
90///
91/// # Safety
92/// `Src` and `Dst` must be arrays of the same element type with the same total number of elements.
93#[inline(always)]
94const unsafe fn transmute_array<Src, Dst>(src: Src) -> Dst {
95    let src = ManuallyDrop::new(src);
96    // SAFETY: Guaranteed by function contract, the original value is not dropped
97    unsafe { mem::transmute_copy::<ManuallyDrop<Src>, Dst>(&src) }
98}
99
100/// Flatten `[[T; N]; M]` into `[T; K]`, where `K == N * M`
101#[inline(always)]
102pub const fn flatten<T, const N: usize, const M: usize, const K: usize>(
103    array: [[T; N]; M],
104) -> [T; K] {
105    assert_len::<N, M, K>();
106    // SAFETY: The same element type and the same number of elements, checked above
107    unsafe { transmute_array(array) }
108}
109
110/// Flatten `&[[T; N]; M]` into `&[T; K]`, where `K == N * M`
111#[inline(always)]
112pub const fn flatten_ref<T, const N: usize, const M: usize, const K: usize>(
113    array: &[[T; N]; M],
114) -> &[T; K] {
115    assert_len::<N, M, K>();
116    // SAFETY: The same element type and the same number of elements, checked above
117    unsafe { &*ptr::from_ref(array).cast::<[T; K]>() }
118}
119
120/// Flatten `&mut [[T; N]; M]` into `&mut [T; K]`, where `K == N * M`
121#[inline(always)]
122pub const fn flatten_mut<T, const N: usize, const M: usize, const K: usize>(
123    array: &mut [[T; N]; M],
124) -> &mut [T; K] {
125    assert_len::<N, M, K>();
126    // SAFETY: The same element type and the same number of elements, checked above
127    unsafe { &mut *ptr::from_mut(array).cast::<[T; K]>() }
128}
129
130/// Unflatten `[T; K]` into `[[T; N]; M]`, where `K == N * M`
131#[inline(always)]
132pub const fn unflatten<T, const N: usize, const M: usize, const K: usize>(
133    array: [T; K],
134) -> [[T; N]; M] {
135    assert_len::<N, M, K>();
136    // SAFETY: The same element type and the same number of elements, checked above
137    unsafe { transmute_array(array) }
138}
139
140/// Unflatten `&[T; K]` into `&[[T; N]; M]`, where `K == N * M`
141#[inline(always)]
142pub const fn unflatten_ref<T, const N: usize, const M: usize, const K: usize>(
143    array: &[T; K],
144) -> &[[T; N]; M] {
145    assert_len::<N, M, K>();
146    // SAFETY: The same element type and the same number of elements, checked above
147    unsafe { &*ptr::from_ref(array).cast::<[[T; N]; M]>() }
148}
149
150/// Unflatten `&mut [T; K]` into `&mut [[T; N]; M]`, where `K == N * M`
151#[inline(always)]
152pub const fn unflatten_mut<T, const N: usize, const M: usize, const K: usize>(
153    array: &mut [T; K],
154) -> &mut [[T; N]; M] {
155    assert_len::<N, M, K>();
156    // SAFETY: The same element type and the same number of elements, checked above
157    unsafe { &mut *ptr::from_mut(array).cast::<[[T; N]; M]>() }
158}
159
160/// Change chunk size of `[[T; A]; B]` to get `[[T; C]; D]`, where `A * B == C * D`
161#[inline(always)]
162pub const fn rechunk<T, const A: usize, const B: usize, const C: usize, const D: usize>(
163    array: [[T; A]; B],
164) -> [[T; C]; D] {
165    assert_same_total_len::<A, B, C, D>();
166    // SAFETY: The same element type and the same number of elements, checked above
167    unsafe { transmute_array(array) }
168}
169
170/// Change chunk size of `&[[T; A]; B]` to get `&[[T; C]; D]`, where `A * B == C * D`
171#[inline(always)]
172pub const fn rechunk_ref<T, const A: usize, const B: usize, const C: usize, const D: usize>(
173    array: &[[T; A]; B],
174) -> &[[T; C]; D] {
175    assert_same_total_len::<A, B, C, D>();
176    // SAFETY: The same element type and the same number of elements, checked above
177    unsafe { &*ptr::from_ref(array).cast::<[[T; C]; D]>() }
178}
179
180/// Change chunk size of `&mut [[T; A]; B]` to get `&mut [[T; C]; D]`, where `A * B == C * D`
181#[inline(always)]
182pub const fn rechunk_mut<T, const A: usize, const B: usize, const C: usize, const D: usize>(
183    array: &mut [[T; A]; B],
184) -> &mut [[T; C]; D] {
185    assert_same_total_len::<A, B, C, D>();
186    // SAFETY: The same element type and the same number of elements, checked above
187    unsafe { &mut *ptr::from_mut(array).cast::<[[T; C]; D]>() }
188}
189
190/// Flatten each element of `&[[[T; N]; M]]` to get `&[[T; K]]`, where `K == N * M`
191#[inline(always)]
192pub const fn flatten_each_ref<T, const N: usize, const M: usize, const K: usize>(
193    slice: &[[[T; N]; M]],
194) -> &[[T; K]] {
195    assert_len::<N, M, K>();
196    // SAFETY: The same element type and the same number of elements in each slice element, checked
197    // above
198    unsafe { slice::from_raw_parts(slice.as_ptr().cast::<[T; K]>(), slice.len()) }
199}
200
201/// Flatten each element of `&mut [[[T; N]; M]]` to get `&mut [[T; K]]`, where `K == N * M`
202#[inline(always)]
203pub const fn flatten_each_mut<T, const N: usize, const M: usize, const K: usize>(
204    slice: &mut [[[T; N]; M]],
205) -> &mut [[T; K]] {
206    assert_len::<N, M, K>();
207    // SAFETY: The same element type and the same number of elements in each slice element, checked
208    // above
209    unsafe { slice::from_raw_parts_mut(slice.as_mut_ptr().cast::<[T; K]>(), slice.len()) }
210}
211
212/// Unflatten each element of `&[[T; K]]` to get `&[[[T; N]; M]]`, where `K == N * M`
213#[inline(always)]
214pub const fn unflatten_each_ref<T, const N: usize, const M: usize, const K: usize>(
215    slice: &[[T; K]],
216) -> &[[[T; N]; M]] {
217    assert_len::<N, M, K>();
218    // SAFETY: The same element type and the same number of elements in each slice element, checked
219    // above
220    unsafe { slice::from_raw_parts(slice.as_ptr().cast::<[[T; N]; M]>(), slice.len()) }
221}
222
223/// Unflatten each element of `&mut [[T; K]]` to get `&mut [[[T; N]; M]]`, where `K == N * M`
224#[inline(always)]
225pub const fn unflatten_each_mut<T, const N: usize, const M: usize, const K: usize>(
226    slice: &mut [[T; K]],
227) -> &mut [[[T; N]; M]] {
228    assert_len::<N, M, K>();
229    // SAFETY: The same element type and the same number of elements in each slice element, checked
230    // above
231    unsafe { slice::from_raw_parts_mut(slice.as_mut_ptr().cast::<[[T; N]; M]>(), slice.len()) }
232}
233
234/// Change chunk size of each element of `&[[[T; A]; B]]` to get `&[[[T; C]; D]]`, where
235/// `A * B == C * D`
236#[inline(always)]
237pub const fn rechunk_each_ref<T, const A: usize, const B: usize, const C: usize, const D: usize>(
238    slice: &[[[T; A]; B]],
239) -> &[[[T; C]; D]] {
240    assert_same_total_len::<A, B, C, D>();
241    // SAFETY: The same element type and the same number of elements in each slice element, checked
242    // above
243    unsafe { slice::from_raw_parts(slice.as_ptr().cast::<[[T; C]; D]>(), slice.len()) }
244}
245
246/// Change chunk size of each element of `&mut [[[T; A]; B]]` to get `&mut [[[T; C]; D]]`, where
247/// `A * B == C * D`
248#[inline(always)]
249pub const fn rechunk_each_mut<T, const A: usize, const B: usize, const C: usize, const D: usize>(
250    slice: &mut [[[T; A]; B]],
251) -> &mut [[[T; C]; D]] {
252    assert_same_total_len::<A, B, C, D>();
253    // SAFETY: The same element type and the same number of elements in each slice element, checked
254    // above
255    unsafe { slice::from_raw_parts_mut(slice.as_mut_ptr().cast::<[[T; C]; D]>(), slice.len()) }
256}
257
258/// Flatten each element of `&[[[T; N]; M]; L]` to get `&[[T; K]; L]`, where `K == N * M`
259#[inline(always)]
260pub const fn flatten_each_array_ref<
261    T,
262    const N: usize,
263    const M: usize,
264    const K: usize,
265    const L: usize,
266>(
267    array: &[[[T; N]; M]; L],
268) -> &[[T; K]; L] {
269    assert_len::<N, M, K>();
270    // SAFETY: The same element type and the same number of elements in each array element, checked
271    // above
272    unsafe { &*ptr::from_ref(array).cast::<[[T; K]; L]>() }
273}
274
275/// Flatten each element of `&mut [[[T; N]; M]; L]` to get `&mut [[T; K]; L]`, where `K == N * M`
276#[inline(always)]
277pub const fn flatten_each_array_mut<
278    T,
279    const N: usize,
280    const M: usize,
281    const K: usize,
282    const L: usize,
283>(
284    array: &mut [[[T; N]; M]; L],
285) -> &mut [[T; K]; L] {
286    assert_len::<N, M, K>();
287    // SAFETY: The same element type and the same number of elements in each array element, checked
288    // above
289    unsafe { &mut *ptr::from_mut(array).cast::<[[T; K]; L]>() }
290}
291
292/// Unflatten each element of `&[[T; K]; L]` to get `&[[[T; N]; M]; L]`, where `K == N * M`
293#[inline(always)]
294pub const fn unflatten_each_array_ref<
295    T,
296    const N: usize,
297    const M: usize,
298    const K: usize,
299    const L: usize,
300>(
301    array: &[[T; K]; L],
302) -> &[[[T; N]; M]; L] {
303    assert_len::<N, M, K>();
304    // SAFETY: The same element type and the same number of elements in each array element, checked
305    // above
306    unsafe { &*ptr::from_ref(array).cast::<[[[T; N]; M]; L]>() }
307}
308
309/// Unflatten each element of `&mut [[T; K]; L]` to get `&mut [[[T; N]; M]; L]`, where `K == N * M`
310#[inline(always)]
311pub const fn unflatten_each_array_mut<
312    T,
313    const N: usize,
314    const M: usize,
315    const K: usize,
316    const L: usize,
317>(
318    array: &mut [[T; K]; L],
319) -> &mut [[[T; N]; M]; L] {
320    assert_len::<N, M, K>();
321    // SAFETY: The same element type and the same number of elements in each array element, checked
322    // above
323    unsafe { &mut *ptr::from_mut(array).cast::<[[[T; N]; M]; L]>() }
324}
325
326/// Change chunk size of each element of `&[[[T; A]; B]; L]` to get `&[[[T; C]; D]; L]`, where
327/// `A * B == C * D`
328#[inline(always)]
329pub const fn rechunk_each_array_ref<
330    T,
331    const A: usize,
332    const B: usize,
333    const C: usize,
334    const D: usize,
335    const L: usize,
336>(
337    array: &[[[T; A]; B]; L],
338) -> &[[[T; C]; D]; L] {
339    assert_same_total_len::<A, B, C, D>();
340    // SAFETY: The same element type and the same number of elements in each array element, checked
341    // above
342    unsafe { &*ptr::from_ref(array).cast::<[[[T; C]; D]; L]>() }
343}
344
345/// Change chunk size of each element of `&mut [[[T; A]; B]; L]` to get `&mut [[[T; C]; D]; L]`,
346/// where `A * B == C * D`
347#[inline(always)]
348pub const fn rechunk_each_array_mut<
349    T,
350    const A: usize,
351    const B: usize,
352    const C: usize,
353    const D: usize,
354    const L: usize,
355>(
356    array: &mut [[[T; A]; B]; L],
357) -> &mut [[[T; C]; D]; L] {
358    assert_same_total_len::<A, B, C, D>();
359    // SAFETY: The same element type and the same number of elements in each array element, checked
360    // above
361    unsafe { &mut *ptr::from_mut(array).cast::<[[[T; C]; D]; L]>() }
362}
363
364/// Checks at compile time that `a * b == c * d`
365#[inline(always)]
366const fn assert_same_total_len<const A: usize, const B: usize, const C: usize, const D: usize>() {
367    const {
368        assert!(
369            matches!(
370                (A.checked_mul(B), C.checked_mul(D)),
371                (Some(input_len), Some(output_len)) if input_len == output_len
372            ),
373            "Arrays must have the same total number of elements"
374        );
375    }
376}
377
378/// Defines reshaping traits, optionally as `const trait`s.
379///
380/// This is a macro because `const trait` syntax is rejected by stable Rust even in code that is
381/// disabled with `#[cfg]`, but not in unexpanded macro invocations.
382macro_rules! define_traits {
383    ($($const:ident)?) => {
384        /// Reshaping of `[[T; N]; M]`, see crate-level documentation for examples.
385        ///
386        /// The same conversions are available as free `const fn`s, see [`flatten()`] and others.
387        pub $($const)? trait Flatten<T, const N: usize, const M: usize>: Sized {
388            /// Flatten into `[T; K]`, where `K == N * M`
389            fn flatten<const K: usize>(self) -> [T; K];
390
391            /// Flatten into `&[T; K]`, where `K == N * M`
392            fn flatten_ref<const K: usize>(&self) -> &[T; K];
393
394            /// Flatten into `&mut [T; K]`, where `K == N * M`
395            fn flatten_mut<const K: usize>(&mut self) -> &mut [T; K];
396        }
397
398        $($const)? impl<T, const N: usize, const M: usize> Flatten<T, N, M> for [[T; N]; M] {
399            #[inline(always)]
400            fn flatten<const K: usize>(self) -> [T; K] {
401                flatten(self)
402            }
403
404            #[inline(always)]
405            fn flatten_ref<const K: usize>(&self) -> &[T; K] {
406                flatten_ref(self)
407            }
408
409            #[inline(always)]
410            fn flatten_mut<const K: usize>(&mut self) -> &mut [T; K] {
411                flatten_mut(self)
412            }
413        }
414
415        /// Changing chunk size of `[[T; N]; M]`, see crate-level documentation for examples.
416        ///
417        /// The same conversions are available as free `const fn`s, see [`rechunk()`] and others.
418        pub $($const)? trait Rechunk<T, const N: usize, const M: usize>: Sized {
419            /// Change chunk size to get `[[T; C]; D]`, where `N * M == C * D`
420            fn rechunk<const C: usize, const D: usize>(self) -> [[T; C]; D];
421
422            /// Change chunk size to get `&[[T; C]; D]`, where `N * M == C * D`
423            fn rechunk_ref<const C: usize, const D: usize>(&self) -> &[[T; C]; D];
424
425            /// Change chunk size to get `&mut [[T; C]; D]`, where `N * M == C * D`
426            fn rechunk_mut<const C: usize, const D: usize>(&mut self) -> &mut [[T; C]; D];
427        }
428
429        $($const)? impl<T, const N: usize, const M: usize> Rechunk<T, N, M> for [[T; N]; M] {
430            #[inline(always)]
431            fn rechunk<const C: usize, const D: usize>(self) -> [[T; C]; D] {
432                rechunk(self)
433            }
434
435            #[inline(always)]
436            fn rechunk_ref<const C: usize, const D: usize>(&self) -> &[[T; C]; D] {
437                rechunk_ref(self)
438            }
439
440            #[inline(always)]
441            fn rechunk_mut<const C: usize, const D: usize>(&mut self) -> &mut [[T; C]; D] {
442                rechunk_mut(self)
443            }
444        }
445
446        /// Reshaping of `[T; K]`, see crate-level documentation for examples.
447        ///
448        /// The same conversions are available as free `const fn`s, see [`unflatten()`] and others.
449        pub $($const)? trait Unflatten<T, const K: usize>: Sized {
450            /// Unflatten into `[[T; N]; M]`, where `K == N * M`
451            fn unflatten<const N: usize, const M: usize>(self) -> [[T; N]; M];
452
453            /// Unflatten into `&[[T; N]; M]`, where `K == N * M`
454            fn unflatten_ref<const N: usize, const M: usize>(&self) -> &[[T; N]; M];
455
456            /// Unflatten into `&mut [[T; N]; M]`, where `K == N * M`
457            fn unflatten_mut<const N: usize, const M: usize>(&mut self) -> &mut [[T; N]; M];
458        }
459
460        $($const)? impl<T, const K: usize> Unflatten<T, K> for [T; K] {
461            #[inline(always)]
462            fn unflatten<const N: usize, const M: usize>(self) -> [[T; N]; M] {
463                unflatten(self)
464            }
465
466            #[inline(always)]
467            fn unflatten_ref<const N: usize, const M: usize>(&self) -> &[[T; N]; M] {
468                unflatten_ref(self)
469            }
470
471            #[inline(always)]
472            fn unflatten_mut<const N: usize, const M: usize>(&mut self) -> &mut [[T; N]; M] {
473                unflatten_mut(self)
474            }
475        }
476        /// Flattening of each element of `[[[T; N]; M]]` or `[[[T; N]; M]; L]`, see crate-level
477        /// documentation for examples.
478        ///
479        /// The same conversions are available as free `const fn`s, see [`flatten_each_ref()`],
480        /// [`flatten_each_mut()`], [`flatten_each_array_ref()`] and [`flatten_each_array_mut()`].
481        pub $($const)? trait FlattenEach<T, const N: usize, const M: usize> {
482            /// `[[T; K]]` for slices and `[[T; K]; L]` for arrays
483            type Flattened<const K: usize>: ?Sized;
484
485            /// Flatten each element to get `&[[T; K]]` or `&[[T; K]; L]`, where `K == N * M`
486            fn flatten_each_ref<const K: usize>(&self) -> &Self::Flattened<K>;
487
488            /// Flatten each element to get `&mut [[T; K]]` or `&mut [[T; K]; L]`, where
489            /// `K == N * M`
490            fn flatten_each_mut<const K: usize>(&mut self) -> &mut Self::Flattened<K>;
491        }
492
493        $($const)? impl<T, const N: usize, const M: usize> FlattenEach<T, N, M> for [[[T; N]; M]] {
494            type Flattened<const K: usize> = [[T; K]];
495
496            #[inline(always)]
497            fn flatten_each_ref<const K: usize>(&self) -> &[[T; K]] {
498                flatten_each_ref(self)
499            }
500
501            #[inline(always)]
502            fn flatten_each_mut<const K: usize>(&mut self) -> &mut [[T; K]] {
503                flatten_each_mut(self)
504            }
505        }
506
507        $($const)? impl<T, const N: usize, const M: usize, const L: usize> FlattenEach<T, N, M>
508            for [[[T; N]; M]; L]
509        {
510            type Flattened<const K: usize> = [[T; K]; L];
511
512            #[inline(always)]
513            fn flatten_each_ref<const K: usize>(&self) -> &[[T; K]; L] {
514                flatten_each_array_ref(self)
515            }
516
517            #[inline(always)]
518            fn flatten_each_mut<const K: usize>(&mut self) -> &mut [[T; K]; L] {
519                flatten_each_array_mut(self)
520            }
521        }
522
523        /// Unflattening of each element of `[[T; K]]` or `[[T; K]; L]`, see crate-level
524        /// documentation for examples.
525        ///
526        /// The same conversions are available as free `const fn`s, see [`unflatten_each_ref()`],
527        /// [`unflatten_each_mut()`], [`unflatten_each_array_ref()`] and
528        /// [`unflatten_each_array_mut()`].
529        pub $($const)? trait UnflattenEach<T, const K: usize> {
530            /// `[[[T; N]; M]]` for slices and `[[[T; N]; M]; L]` for arrays
531            type Unflattened<const N: usize, const M: usize>: ?Sized;
532
533            /// Unflatten each element to get `&[[[T; N]; M]]` or `&[[[T; N]; M]; L]`, where
534            /// `K == N * M`
535            fn unflatten_each_ref<const N: usize, const M: usize>(&self)
536            -> &Self::Unflattened<N, M>;
537
538            /// Unflatten each element to get `&mut [[[T; N]; M]]` or `&mut [[[T; N]; M]; L]`,
539            /// where `K == N * M`
540            fn unflatten_each_mut<const N: usize, const M: usize>(
541                &mut self,
542            ) -> &mut Self::Unflattened<N, M>;
543        }
544
545        $($const)? impl<T, const K: usize> UnflattenEach<T, K> for [[T; K]] {
546            type Unflattened<const N: usize, const M: usize> = [[[T; N]; M]];
547
548            #[inline(always)]
549            fn unflatten_each_ref<const N: usize, const M: usize>(&self) -> &[[[T; N]; M]] {
550                unflatten_each_ref(self)
551            }
552
553            #[inline(always)]
554            fn unflatten_each_mut<const N: usize, const M: usize>(&mut self) -> &mut [[[T; N]; M]] {
555                unflatten_each_mut(self)
556            }
557        }
558
559        $($const)? impl<T, const K: usize, const L: usize> UnflattenEach<T, K> for [[T; K]; L] {
560            type Unflattened<const N: usize, const M: usize> = [[[T; N]; M]; L];
561
562            #[inline(always)]
563            fn unflatten_each_ref<const N: usize, const M: usize>(&self) -> &[[[T; N]; M]; L] {
564                unflatten_each_array_ref(self)
565            }
566
567            #[inline(always)]
568            fn unflatten_each_mut<const N: usize, const M: usize>(
569                &mut self,
570            ) -> &mut [[[T; N]; M]; L] {
571                unflatten_each_array_mut(self)
572            }
573        }
574
575        /// Changing chunk size of each element of `[[[T; N]; M]]` or `[[[T; N]; M]; L]`, see
576        /// crate-level documentation for examples.
577        ///
578        /// The same conversions are available as free `const fn`s, see [`rechunk_each_ref()`],
579        /// [`rechunk_each_mut()`], [`rechunk_each_array_ref()`] and [`rechunk_each_array_mut()`].
580        pub $($const)? trait RechunkEach<T, const N: usize, const M: usize> {
581            /// `[[[T; C]; D]]` for slices and `[[[T; C]; D]; L]` for arrays
582            type Rechunked<const C: usize, const D: usize>: ?Sized;
583
584            /// Change chunk size of each element to get `&[[[T; C]; D]]` or `&[[[T; C]; D]; L]`,
585            /// where `N * M == C * D`
586            fn rechunk_each_ref<const C: usize, const D: usize>(&self) -> &Self::Rechunked<C, D>;
587
588            /// Change chunk size of each element to get `&mut [[[T; C]; D]]` or
589            /// `&mut [[[T; C]; D]; L]`, where `N * M == C * D`
590            fn rechunk_each_mut<const C: usize, const D: usize>(
591                &mut self,
592            ) -> &mut Self::Rechunked<C, D>;
593        }
594
595        $($const)? impl<T, const N: usize, const M: usize> RechunkEach<T, N, M> for [[[T; N]; M]] {
596            type Rechunked<const C: usize, const D: usize> = [[[T; C]; D]];
597
598            #[inline(always)]
599            fn rechunk_each_ref<const C: usize, const D: usize>(&self) -> &[[[T; C]; D]] {
600                rechunk_each_ref(self)
601            }
602
603            #[inline(always)]
604            fn rechunk_each_mut<const C: usize, const D: usize>(&mut self) -> &mut [[[T; C]; D]] {
605                rechunk_each_mut(self)
606            }
607        }
608
609        $($const)? impl<T, const N: usize, const M: usize, const L: usize> RechunkEach<T, N, M>
610            for [[[T; N]; M]; L]
611        {
612            type Rechunked<const C: usize, const D: usize> = [[[T; C]; D]; L];
613
614            #[inline(always)]
615            fn rechunk_each_ref<const C: usize, const D: usize>(&self) -> &[[[T; C]; D]; L] {
616                rechunk_each_array_ref(self)
617            }
618
619            #[inline(always)]
620            fn rechunk_each_mut<const C: usize, const D: usize>(
621                &mut self,
622            ) -> &mut [[[T; C]; D]; L] {
623                rechunk_each_array_mut(self)
624            }
625        }
626    };
627}
628
629#[cfg(feature = "const-trait")]
630define_traits!(const);
631#[cfg(not(feature = "const-trait"))]
632define_traits!();