1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
#![forbid(unsafe_code)]
#![cfg_attr(not(test), no_std)]

//! This crate offers the [`ArrayTools`] extension trait, which provides
//! a variety of helpful methods for working with fixed-size arrays.
//!
//! [`ArrayTools`]: trait.ArrayTools.html
//!
//! # Examples
//!
//! `Iterator`-like methods over arrays:
//!
//! ```rust
//! use arraytools::ArrayTools;
//!
//! assert_eq!([1, 2, 3].map(|x| x+1), [2, 3, 4]);
//! assert_eq!([1, 2].zip(["one", "two"]), [(1, "one"), (2, "two")]);
//! ```
//!
//! Ways to simplify array creation:
//!
//! ```rust
//! use arraytools::ArrayTools;
//!
//! let mut state = 1;
//! assert_eq!(<[_; 4]>::generate(|| { state *= 2; state }), [2, 4, 8, 16]);
//! assert_eq!(<[usize; 4]>::indices(), [0, 1, 2, 3]);
//!
//! let s = "hello".to_string(); // Something `!Copy`
//! assert_eq!(<[String; 3]>::repeat(s).as_ref_array(), ["hello", "hello", "hello"]);
//! ```
//!
//! Conversion to and from homogeneous tuples:
//!
//! ```rust
//! use arraytools::ArrayTools;
//!
//! let mut array = [2, 3, 5, 7, 11];
//! assert_eq!(array.into_tuple(), (2, 3, 5, 7, 11));
//! array = ArrayTools::from_tuple((1, 1, 2, 3, 5));
//! assert_eq!(array, [1, 1, 2, 3, 5]);
//! ```
//!
//! Like `Option`, most combinators here take `self`.  To not move something,
//! you can use [`.as_ref_array()`] or [`.as_mut_array()`]:
//!
//! [`.as_ref_array()`]: trait.ArrayTools.html#method.as_ref_array
//! [`.as_mut_array()`]: trait.ArrayTools.html#method.as_mut_array
//!
//! ```rust
//! use arraytools::ArrayTools;
//!
//! struct SevenStrings([String; 7]);
//! impl SevenStrings {
//!     fn push_str(&mut self, s: &str) {
//!         self.0.as_mut_array().for_each(|x: &mut String| x.push_str(s));
//!     }
//! }
//! ```
//!

use self::traits::*;

/// An extension trait for working with fixed-length arrays.
///
/// Use it with
/// ```rust
/// use arraytools::ArrayTools;
/// ```
///
/// For an overview, see the [crate-level documentation](index.html).
///
/// (This trait is sealed; you are not allowed to implement it yourself.)
pub trait ArrayTools: Sized + Sealed {
    /// The type of the elements in this array
    ///
    /// ```rust
    /// # type T = usize;
    /// # const N: usize = 1;
    /// use arraytools::ArrayTools;
    ///
    /// # fn _foo() where
    /// [T; N]: ArrayTools<Element = T>
    /// # {}
    /// ```
    type Element;

    /// The number of the elements in this array
    ///
    /// ```rust
    /// # type T = usize;
    /// # const N: usize = 1;
    /// use arraytools::ArrayTools;
    ///
    /// assert_eq!(<[T; N] as ArrayTools>::LEN, N);
    /// ```
    const LEN: usize;

    /// Extracts a slice containing the entire array.
    ///
    /// ```rust
    /// # type T = usize;
    /// # const N: usize = 1;
    /// use arraytools::ArrayTools;
    ///
    /// let array: [i32; 5] = [1, 2, 3, 4, 5];
    /// let slice: &[i32] = array.as_slice();
    /// assert_eq!(slice.len(), 5);
    /// ```
    fn as_slice(&self) -> &[Self::Element];

    /// Extracts a mutable slice containing the entire array.
    ///
    /// ```rust
    /// # type T = usize;
    /// # const N: usize = 1;
    /// use arraytools::ArrayTools;
    ///
    /// let mut array: [i32; 5] = [1, 2, 3, 4, 5];
    /// let slice: &mut [i32] = array.as_mut_slice();
    /// assert_eq!(slice.len(), 5);
    /// ```
    fn as_mut_slice(&mut self) -> &mut [Self::Element];

    /// The homogeneous tuple type equivalent to this array type.
    ///
    /// ```rust
    /// # type T = usize;
    /// use arraytools::ArrayTools;
    ///
    /// # fn _foo() where
    /// [T; 4]: ArrayTools<Tuple = (T, T, T, T)>
    /// # {}
    /// ```
    type Tuple;

    /// Converts a homogeneous tuple into the equivalent array.
    ///
    /// Type: `(T, T, ..., T) -> [T; N]`
    ///
    /// ```rust
    /// use arraytools::ArrayTools;
    ///
    /// assert_eq!(<[_; 3]>::from_tuple((1, 2, 3)), [1, 2, 3]);
    /// ```
    fn from_tuple(tuple: Self::Tuple) -> Self;

    /// Converts this array into the equivalent homogeneous tuple.
    ///
    /// Type: `[T; N] -> (T, T, ..., T)`
    ///
    /// ```rust
    /// use arraytools::ArrayTools;
    ///
    /// assert_eq!([1, 2, 3].into_tuple(), (1, 2, 3));
    /// ```
    fn into_tuple(self) -> Self::Tuple;

    /// Builds an array by calling the provided function.
    ///
    /// Type: `F -> [T; N]`
    /// - when `N <= 1` this requires `F: FnOnce() -> T`
    /// - when `N > 1` this requires `F: FnMut() -> T`
    ///
    /// ```rust
    /// use arraytools::ArrayTools;
    ///
    /// let mut x = 1;
    /// let array: [_; 5] = ArrayTools::generate(|| { x *= 2; x });
    /// assert_eq!(array, [2, 4, 8, 16, 32]);
    /// ```
    fn generate<F>(f: F) -> Self
        where Self: ArrayGenerate<F>
    {
        ArrayGenerate::generate(f)
    }

    /// Builds an array by cloning the provided value.
    ///
    /// Type: `T -> [T; N]`
    ///
    /// ```rust
    /// use arraytools::ArrayTools;
    ///
    /// let mut v = Vec::with_capacity(10);
    /// v.push(42);
    /// let array: [_; 5] = ArrayTools::repeat(v);
    /// assert_eq!(array, [[42], [42], [42], [42], [42]]);
    /// assert_eq!(array[3].capacity(), 1);
    /// assert_eq!(array[4].capacity(), 10); // The last one is moved
    /// ```
    fn repeat<T: Clone>(x: T) -> Self
        where Self: ArrayRepeat<T>
    {
        ArrayRepeat::repeat(x)
    }

    /// Builds an array containing a prefix of the provided iterator,
    /// or returns `None` if it didn't contain sufficient items.
    ///
    /// Type: `impl IntoIterator<Item = T> -> Option<[T; N]>`
    ///
    /// ```rust
    /// use arraytools::ArrayTools;
    ///
    /// assert_eq!(<[i16; 4]>::from_iter(1..), Some([1, 2, 3, 4]));
    ///
    /// assert_eq!(<[_; 4]>::from_iter(1..4), None);
    /// assert_eq!(<[_; 4]>::from_iter(1..5), Some([1, 2, 3, 4]));
    /// assert_eq!(<[_; 4]>::from_iter(1..6), Some([1, 2, 3, 4]));
    ///
    /// assert_eq!(<[u8; 1]>::from_iter(Some(1)), Some([1]));
    /// assert_eq!(<[u8; 1]>::from_iter(None), None);
    /// ```
    fn from_iter<I: IntoIterator>(it: I) -> Option<Self>
        where Self: ArrayFromIter<I::IntoIter>
    {
        ArrayFromIter::from_iter(it.into_iter())
    }

    /// Builds the array `[0, 1, 2, ..., LEN-1]`.
    ///
    /// Type: `() -> [usize; N]`
    ///
    /// ```rust
    /// use arraytools::ArrayTools;
    ///
    /// let array: [_; 5] = ArrayTools::indices();
    /// assert_eq!(array, [0, 1, 2, 3, 4]);
    /// ```
    fn indices() -> Self
        where Self: ArrayIndices
    {
        ArrayIndices::indices()
    }

    /// Builds a new array by applying the provided function to each element of this array.
    ///
    /// Type: `([T; N], F) -> [U; N]`
    /// - when `N <= 1` this requires `F: FnOnce(T) -> U`
    /// - when `N > 1` this requires `F: FnMut(T) -> U`
    ///
    /// ```rust
    /// use arraytools::ArrayTools;
    ///
    /// assert_eq!([1, 10, 100].map(|x| x + 10), [11, 20, 110]);
    /// ```
    #[must_use = "if you don't need the result, use `for_each`"]
    fn map<F>(self, f: F) -> <Self as ArrayMap<F>>::Output
        where Self: ArrayMap<F>
    {
        ArrayMap::map(self, f)
    }

    /// Runs the provided function on each element of this array.
    ///
    /// Type: `([T; N], F) -> ()`
    /// - when `N <= 1` this requires `F: FnOnce(T) -> ()`
    /// - when `N > 1` this requires `F: FnMut(T) -> ()`
    ///
    /// ```rust
    /// use arraytools::ArrayTools;
    ///
    /// let mut array = [1, 10, 100];
    /// array.as_mut_array().for_each(|x: &mut u8| *x += 10);
    /// assert_eq!(array, [11, 20, 110]);
    /// ```
    fn for_each<F>(self, f: F)
        where Self: ArrayMap<F, OutputElement = ()>
    {
        ArrayMap::map(self, f);
    }

    /// Combines two equal-length arrays into an array of tuples.
    ///
    /// Type: `([T; N], [U; N]) -> [(T, U); N]`
    ///
    /// ```rust
    /// use arraytools::ArrayTools;
    ///
    /// assert_eq!([10, 20, 30].zip([1.0, 2.0, 3.0]), [(10, 1.0), (20, 2.0), (30, 3.0)]);
    /// ```
    fn zip<T>(self, other: T) -> <Self as ArrayZip<T>>::Output
        where Self: ArrayZip<T>
    {
        ArrayZip::zip(self, other)
    }

    /// Combines two equal-length arrays using the provided function.
    ///
    /// Type: `([T; N], [U; N], F) -> [V; N]`
    /// - when `N <= 1` this requires `F: FnOnce(T, U) -> V`
    /// - when `N > 1` this requires `F: FnMut(T, U) -> V`
    ///
    /// ```rust
    /// use arraytools::ArrayTools;
    ///
    /// assert_eq!([10, 20, 30].zip_with([3, 2, 1], std::ops::Add::add), [13, 22, 31]);
    /// ```
    fn zip_with<T, F>(self, other: T, f: F) -> <Self as ArrayZipWith<T, F>>::Output
        where Self: ArrayZipWith<T, F>
    {
        ArrayZipWith::zip_with(self, other, f)
    }

    /// Builds an array of references to the elements of this array.
    ///
    /// Type: `&'a [T; N] -> [&'a T; N]`
    ///
    /// ```rust
    /// use arraytools::ArrayTools;
    ///
    /// let array = &[1, 2, 3, 4, 5];
    /// assert_eq!(array.as_ref_array(), [&1, &2, &3, &4, &5]);
    /// ```
    fn as_ref_array<'a>(&'a self) -> <Self as ArrayAsRef<'a>>::Output
        where Self: ArrayAsRef<'a>
    {
        ArrayAsRef::as_ref(self)
    }

    /// Builds an array of mutable references to the elements of this array.
    ///
    /// Type: `&'a mut [T; N] -> [&'a mut T; N]`
    ///
    /// ```rust
    /// use arraytools::ArrayTools;
    ///
    /// let array = &mut [1, 2, 3];
    /// assert_eq!(array.as_ref_array(), [&mut 1, &mut 2, &mut 3]);
    /// ```
    fn as_mut_array<'a>(&'a mut self) -> <Self as ArrayAsMut<'a>>::Output
        where Self: ArrayAsMut<'a>
    {
        ArrayAsMut::as_mut(self)
    }

    /// Appends an item to this array, returning the new array
    ///
    /// Type: `([T; N], T) -> [T; N+1]`
    ///
    /// ```rust
    /// use arraytools::ArrayTools;
    ///
    /// assert_eq!([1, 2].push_back(10), [1, 2, 10]);
    /// ```
    #[must_use = "this returns the new array; it doesn't update the existing one"]
    fn push_back<U>(self, item: U) -> <Self as ArrayPush<U>>::Output
        where Self: ArrayPush<U>
    {
        ArrayPush::push_back(self, item)
    }

    /// Prepends an item to this array, returning the new array
    ///
    /// Type: `([T; N], T) -> [T; N+1]`
    ///
    /// ```rust
    /// use arraytools::ArrayTools;
    ///
    /// assert_eq!([1, 2].push_front(10), [10, 1, 2]);
    /// ```
    #[must_use = "this returns the new array; it doesn't update the existing one"]
    fn push_front<U>(self, item: U) -> <Self as ArrayPush<U>>::Output
        where Self: ArrayPush<U>
    {
        ArrayPush::push_front(self, item)
    }

    /// Splits the last item off from this array, returning a tuple of
    /// an array of the other elements and the split-off item.
    ///
    /// Type: `[T; N+1] -> ([T; N], T)`
    ///
    /// ```rust
    /// use arraytools::ArrayTools;
    ///
    /// assert_eq!([1, 2, 3].pop_back(), ([1, 2], 3));
    /// ```
    #[must_use = "this returns the new array; it doesn't update the existing one"]
    fn pop_back<U>(self) -> (<Self as ArrayPop<U>>::Output, U)
        where Self: ArrayPop<U>
    {
        ArrayPop::pop_back(self)
    }

    /// Splits the first item off from this array, returning a tuple of
    /// an array of the other elements and the split-off item.
    ///
    /// Type: `[T; N+1] -> ([T; N], T)`
    ///
    /// ```rust
    /// use arraytools::ArrayTools;
    ///
    /// assert_eq!([1, 2, 3].pop_front(), ([2, 3], 1));
    /// ```
    #[must_use = "this returns the new array; it doesn't update the existing one"]
    fn pop_front<U>(self) -> (<Self as ArrayPop<U>>::Output, U)
        where Self: ArrayPop<U>
    {
        ArrayPop::pop_front(self)
    }
}

mod traits {
    pub trait Sealed {}

    pub trait ArrayGenerate<F> {
        fn generate(f: F) -> Self;
    }

    pub trait ArrayRepeat<T> {
        fn repeat(x: T) -> Self;
    }

    pub trait ArrayFromIter<I> {
        fn from_iter(it: I) -> Option<Self> where Self: Sized;
    }

    pub trait ArrayIndices {
        fn indices() -> Self;
    }

    pub trait ArrayMap<F> {
        type Output;
        type OutputElement;
        fn map(array: Self, f: F) -> Self::Output;
    }

    pub trait ArrayZip<T> {
        type Output;
        fn zip(array: Self, other: T) -> Self::Output;
    }

    pub trait ArrayZipWith<T, F> {
        type Output;
        fn zip_with(array: Self, other: T, f: F) -> Self::Output;
    }

    pub trait ArrayAsRef<'a> {
        type Output: 'a;
        fn as_ref(array: &'a Self) -> Self::Output;
    }

    pub trait ArrayAsMut<'a> {
        type Output: 'a;
        fn as_mut(array: &'a mut Self) -> Self::Output;
    }

    pub trait ArrayPush<T> {
        type Output;
        fn push_back(array: Self, item: T) -> Self::Output;
        fn push_front(array: Self, item: T) -> Self::Output;
    }

    pub trait ArrayPop<T> {
        type Output;
        fn pop_back(array: Self) -> (Self::Output, T);
        fn pop_front(array: Self) -> (Self::Output, T);
    }
}

#[allow(unused_mut, unused_variables)]
mod impls {
    use super::*;

    macro_rules! replace_ident {
        ($i:ident => $($j:tt)*) => ($($j)*)
    }

    macro_rules! array_by_cloning {
        ($x:ident:) => ( [] );
        ($x:ident: $first:ident $($i:ident)*) => ( [$(replace_ident!($i => $x.clone()),)* $x] );
    }

    macro_rules! impl_for_size {
        ($n:expr; $fn_trait:ident => $($i:ident)* / $($j:ident)*) => (

            impl<T> Sealed for [T; $n] {}
            impl<T> ArrayTools for [T; $n] {
                type Element = T;
                const LEN: usize = $n;
                fn as_slice(&self) -> &[Self::Element] { self }
                fn as_mut_slice(&mut self) -> &mut [Self::Element] { self }

                type Tuple = ($(replace_ident!($i => T),)*);
                fn from_tuple(tuple: Self::Tuple) -> Self {
                    let ($($i,)*) = tuple;
                    [$($i,)*]
                }
                fn into_tuple(self) -> Self::Tuple {
                    let [$($i,)*] = self;
                    ($($i,)*)
                }
            }
            impl<T, F> ArrayGenerate<F> for [T; $n]
                where F: $fn_trait() -> T
            {
                fn generate(mut f: F) -> Self {
                    [$(replace_ident!($i => f()),)*]
                }
            }
            impl<T> ArrayRepeat<T> for [T; $n]
                where T: Clone
            {
                fn repeat(x: T) -> Self {
                    array_by_cloning!(x: $($i)*)
                }
            }
            impl<T, I> ArrayFromIter<I> for [T; $n]
                where I: Iterator<Item = T>
            {
                fn from_iter(mut it: I) -> Option<Self> {
                    Some([$(replace_ident!($i => it.next()?),)*])
                }
            }
            impl ArrayIndices for [usize; $n] {
                fn indices() -> Self {
                    let mut i = 0;
                    ArrayTools::generate(|| { let t = i; i += 1; t })
                }
            }
            impl<T, U, F> ArrayMap<F> for [T; $n]
                where F: $fn_trait(T) -> U
            {
                type Output = [U; $n];
                type OutputElement = U;
                fn map(array: Self, mut f: F) -> Self::Output {
                    let [$($i,)*] = array;
                    [$(f($i),)*]
                }
            }
            impl<T, U> ArrayZip<[U; $n]> for [T; $n] {
                type Output = [(T, U); $n];
                fn zip(array: Self, other: [U; $n]) -> Self::Output {
                    let [$($i,)*] = array;
                    let [$($j,)*] = other;
                    [$(($i,$j),)*]
                }
            }
            impl<T, U, V, F> ArrayZipWith<[U; $n], F> for [T; $n]
                where F: $fn_trait(T, U) -> V
            {
                type Output = [V; $n];
                fn zip_with(array: Self, other: [U; $n], mut f: F) -> Self::Output {
                    let [$($i,)*] = array;
                    let [$($j,)*] = other;
                    [$(f($i,$j),)*]
                }
            }
            impl<'a, T: 'a> ArrayAsRef<'a> for [T; $n]
            {
                type Output = [&'a T; $n];
                fn as_ref(array: &'a Self) -> Self::Output {
                    let [$($i,)*] = array;
                    [$($i,)*]
                }
            }
            impl<'a, T: 'a> ArrayAsMut<'a> for [T; $n]
            {
                type Output = [&'a mut T; $n];
                fn as_mut(array: &'a mut Self) -> Self::Output {
                    let [$($i,)*] = array;
                    [$($i,)*]
                }
            }
            impl<T> ArrayPush<T> for [T; $n] {
                type Output = [T; $n+1];
                fn push_back(array: Self, item: T) -> Self::Output {
                    let [$($i,)*] = array;
                    [$($i,)* item]
                }
                fn push_front(array: Self, item: T) -> Self::Output {
                    let [$($i,)*] = array;
                    [item, $($i,)*]
                }
            }
            impl<T> ArrayPop<T> for [T; $n+1] {
                type Output = [T; $n];
                fn pop_back(array: Self) -> (Self::Output, T) {
                    let [$($i,)* item] = array;
                    ([$($i,)*], item)
                }
                fn pop_front(array: Self) -> (Self::Output, T) {
                    let [item, $($i,)*] = array;
                    ([$($i,)*], item)
                }
            }

        )
    }

    // <https://play.rust-lang.org/?gist=10a054305dfabf05f0c652e2df75fdcc>
    impl_for_size!(0; FnOnce => /);
    impl_for_size!(1; FnOnce => a0 / b0);
    impl_for_size!(2; FnMut => a0 a1 / b0 b1);
    impl_for_size!(3; FnMut => a0 a1 a2 / b0 b1 b2);
    impl_for_size!(4; FnMut => a0 a1 a2 a3 / b0 b1 b2 b3);
    impl_for_size!(5; FnMut => a0 a1 a2 a3 a4 / b0 b1 b2 b3 b4);
    impl_for_size!(6; FnMut => a0 a1 a2 a3 a4 a5 / b0 b1 b2 b3 b4 b5);
    impl_for_size!(7; FnMut => a0 a1 a2 a3 a4 a5 a6 / b0 b1 b2 b3 b4 b5 b6);
    impl_for_size!(8; FnMut => a0 a1 a2 a3 a4 a5 a6 a7 / b0 b1 b2 b3 b4 b5 b6 b7);
    impl_for_size!(9; FnMut => a0 a1 a2 a3 a4 a5 a6 a7 a8 / b0 b1 b2 b3 b4 b5 b6 b7 b8);
    impl_for_size!(10; FnMut => a0 a1 a2 a3 a4 a5 a6 a7 a8 a9 / b0 b1 b2 b3 b4 b5 b6 b7 b8 b9);
    impl_for_size!(11; FnMut => a0 a1 a2 a3 a4 a5 a6 a7 a8 a9 a10 / b0 b1 b2 b3 b4 b5 b6 b7 b8 b9 b10);
    impl_for_size!(12; FnMut => a0 a1 a2 a3 a4 a5 a6 a7 a8 a9 a10 a11 / b0 b1 b2 b3 b4 b5 b6 b7 b8 b9 b10 b11);
    impl_for_size!(13; FnMut => a0 a1 a2 a3 a4 a5 a6 a7 a8 a9 a10 a11 a12 / b0 b1 b2 b3 b4 b5 b6 b7 b8 b9 b10 b11 b12);
    impl_for_size!(14; FnMut => a0 a1 a2 a3 a4 a5 a6 a7 a8 a9 a10 a11 a12 a13 / b0 b1 b2 b3 b4 b5 b6 b7 b8 b9 b10 b11 b12 b13);
    impl_for_size!(15; FnMut => a0 a1 a2 a3 a4 a5 a6 a7 a8 a9 a10 a11 a12 a13 a14 / b0 b1 b2 b3 b4 b5 b6 b7 b8 b9 b10 b11 b12 b13 b14);
    impl_for_size!(16; FnMut => a0 a1 a2 a3 a4 a5 a6 a7 a8 a9 a10 a11 a12 a13 a14 a15 / b0 b1 b2 b3 b4 b5 b6 b7 b8 b9 b10 b11 b12 b13 b14 b15);
    impl_for_size!(17; FnMut => a0 a1 a2 a3 a4 a5 a6 a7 a8 a9 a10 a11 a12 a13 a14 a15 a16 / b0 b1 b2 b3 b4 b5 b6 b7 b8 b9 b10 b11 b12 b13 b14 b15 b16);
    impl_for_size!(18; FnMut => a0 a1 a2 a3 a4 a5 a6 a7 a8 a9 a10 a11 a12 a13 a14 a15 a16 a17 / b0 b1 b2 b3 b4 b5 b6 b7 b8 b9 b10 b11 b12 b13 b14 b15 b16 b17);
    impl_for_size!(19; FnMut => a0 a1 a2 a3 a4 a5 a6 a7 a8 a9 a10 a11 a12 a13 a14 a15 a16 a17 a18 / b0 b1 b2 b3 b4 b5 b6 b7 b8 b9 b10 b11 b12 b13 b14 b15 b16 b17 b18);
    impl_for_size!(20; FnMut => a0 a1 a2 a3 a4 a5 a6 a7 a8 a9 a10 a11 a12 a13 a14 a15 a16 a17 a18 a19 / b0 b1 b2 b3 b4 b5 b6 b7 b8 b9 b10 b11 b12 b13 b14 b15 b16 b17 b18 b19);
    impl_for_size!(21; FnMut => a0 a1 a2 a3 a4 a5 a6 a7 a8 a9 a10 a11 a12 a13 a14 a15 a16 a17 a18 a19 a20 / b0 b1 b2 b3 b4 b5 b6 b7 b8 b9 b10 b11 b12 b13 b14 b15 b16 b17 b18 b19 b20);
    impl_for_size!(22; FnMut => a0 a1 a2 a3 a4 a5 a6 a7 a8 a9 a10 a11 a12 a13 a14 a15 a16 a17 a18 a19 a20 a21 / b0 b1 b2 b3 b4 b5 b6 b7 b8 b9 b10 b11 b12 b13 b14 b15 b16 b17 b18 b19 b20 b21);
    impl_for_size!(23; FnMut => a0 a1 a2 a3 a4 a5 a6 a7 a8 a9 a10 a11 a12 a13 a14 a15 a16 a17 a18 a19 a20 a21 a22 / b0 b1 b2 b3 b4 b5 b6 b7 b8 b9 b10 b11 b12 b13 b14 b15 b16 b17 b18 b19 b20 b21 b22);
    impl_for_size!(24; FnMut => a0 a1 a2 a3 a4 a5 a6 a7 a8 a9 a10 a11 a12 a13 a14 a15 a16 a17 a18 a19 a20 a21 a22 a23 / b0 b1 b2 b3 b4 b5 b6 b7 b8 b9 b10 b11 b12 b13 b14 b15 b16 b17 b18 b19 b20 b21 b22 b23);
    impl_for_size!(25; FnMut => a0 a1 a2 a3 a4 a5 a6 a7 a8 a9 a10 a11 a12 a13 a14 a15 a16 a17 a18 a19 a20 a21 a22 a23 a24 / b0 b1 b2 b3 b4 b5 b6 b7 b8 b9 b10 b11 b12 b13 b14 b15 b16 b17 b18 b19 b20 b21 b22 b23 b24);
    impl_for_size!(26; FnMut => a0 a1 a2 a3 a4 a5 a6 a7 a8 a9 a10 a11 a12 a13 a14 a15 a16 a17 a18 a19 a20 a21 a22 a23 a24 a25 / b0 b1 b2 b3 b4 b5 b6 b7 b8 b9 b10 b11 b12 b13 b14 b15 b16 b17 b18 b19 b20 b21 b22 b23 b24 b25);
    impl_for_size!(27; FnMut => a0 a1 a2 a3 a4 a5 a6 a7 a8 a9 a10 a11 a12 a13 a14 a15 a16 a17 a18 a19 a20 a21 a22 a23 a24 a25 a26 / b0 b1 b2 b3 b4 b5 b6 b7 b8 b9 b10 b11 b12 b13 b14 b15 b16 b17 b18 b19 b20 b21 b22 b23 b24 b25 b26);
    impl_for_size!(28; FnMut => a0 a1 a2 a3 a4 a5 a6 a7 a8 a9 a10 a11 a12 a13 a14 a15 a16 a17 a18 a19 a20 a21 a22 a23 a24 a25 a26 a27 / b0 b1 b2 b3 b4 b5 b6 b7 b8 b9 b10 b11 b12 b13 b14 b15 b16 b17 b18 b19 b20 b21 b22 b23 b24 b25 b26 b27);
    impl_for_size!(29; FnMut => a0 a1 a2 a3 a4 a5 a6 a7 a8 a9 a10 a11 a12 a13 a14 a15 a16 a17 a18 a19 a20 a21 a22 a23 a24 a25 a26 a27 a28 / b0 b1 b2 b3 b4 b5 b6 b7 b8 b9 b10 b11 b12 b13 b14 b15 b16 b17 b18 b19 b20 b21 b22 b23 b24 b25 b26 b27 b28);
    impl_for_size!(30; FnMut => a0 a1 a2 a3 a4 a5 a6 a7 a8 a9 a10 a11 a12 a13 a14 a15 a16 a17 a18 a19 a20 a21 a22 a23 a24 a25 a26 a27 a28 a29 / b0 b1 b2 b3 b4 b5 b6 b7 b8 b9 b10 b11 b12 b13 b14 b15 b16 b17 b18 b19 b20 b21 b22 b23 b24 b25 b26 b27 b28 b29);
    impl_for_size!(31; FnMut => a0 a1 a2 a3 a4 a5 a6 a7 a8 a9 a10 a11 a12 a13 a14 a15 a16 a17 a18 a19 a20 a21 a22 a23 a24 a25 a26 a27 a28 a29 a30 / b0 b1 b2 b3 b4 b5 b6 b7 b8 b9 b10 b11 b12 b13 b14 b15 b16 b17 b18 b19 b20 b21 b22 b23 b24 b25 b26 b27 b28 b29 b30);
    impl_for_size!(32; FnMut => a0 a1 a2 a3 a4 a5 a6 a7 a8 a9 a10 a11 a12 a13 a14 a15 a16 a17 a18 a19 a20 a21 a22 a23 a24 a25 a26 a27 a28 a29 a30 a31 / b0 b1 b2 b3 b4 b5 b6 b7 b8 b9 b10 b11 b12 b13 b14 b15 b16 b17 b18 b19 b20 b21 b22 b23 b24 b25 b26 b27 b28 b29 b30 b31);
}

#[cfg(test)]
mod tests {
    use super::ArrayTools;

    #[test]
    fn it_works() {
        let mut a = [1];
        *a.as_mut_array()[0] = 2;
        assert_eq!(a, [2]);

        a = ArrayTools::from_tuple((3,));
        assert_eq!(a, [3]);
        assert_eq!(a.into_tuple(), (3,));

        let a = a.map(|x| x as f32);
        assert_eq!(a, [3.0]);

        let a0: [u8; 0] = [];
        let a1 = a0.push_back(Default::default());
        assert_eq!(a1, [0]);
        let a2 = a1.push_back(2);
        assert_eq!(a2, [0, 2]);
        let b1 = a2.pop_back();
        assert_eq!(b1, ([0], 2));

        let iota: [_; 10] = ArrayTools::indices();
        assert_eq!(iota, [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]);

        let mut v = Vec::with_capacity(111);
        v.push(1);
        let a: [_; 3] = ArrayTools::repeat(v);
        assert_eq!(a[0], [1]);
        assert_eq!(a[1], [1]);
        assert_eq!(a[2], [1]);
        assert_eq!(a[0].capacity(), 1);
        assert_eq!(a[1].capacity(), 1);
        assert_eq!(a[2].capacity(), 111);

        let sums = [1, 2, 3].zip_with([30, 20, 10], std::ops::Add::add);
        assert_eq!(sums, [31, 22, 13]);
    }

    #[test]
    fn from_iter_is_not_ambiguous_with_std() {
        #[allow(unused_imports)]
        use std::iter::FromIterator;
        <[i16; 5]>::from_iter(1..);
    }
}