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
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
//! `numeric-array` is a wrapper around [`generic-array`](https://github.com/fizyk20/generic-array) that adds efficient numeric trait implementations, often times making use of SIMD instructions and compile-time evaluations.
//!
//! All stable `std::ops` traits are implemented for `NumericArray` itself, plus the thin `NumericConstant` type, which is required to differeniate constant values from `NumericArray` itself.
//!
//! Additionally, most of `num_traits` are implemented, including `Num` itself. So you can even use a whole array as a generic number.
//!
//! Example:
//!
//! ```rust
//! #[macro_use]
//! extern crate generic_array;
//! #[macro_use]
//! extern crate numeric_array;
//!
//! use numeric_array::NumericArray;
//!
//! fn main() {
//!     let a = narr![i32; 1, 3, 5, 7];
//!     let b = narr![i32; 2, 4, 6, 8];
//!
//!     let c = a + b * nconstant!(2);
//!
//!     assert_eq!(c, narr![i32; 5, 11, 17, 23]);
//! }
//! ```
//!
//! When used with `RUSTFLAGS = "-C opt-level=3 -C target-cpu=native"`, then Rust and LLVM are smart enough to turn almost all operations into SIMD instructions, or even just evaluate them at compile time. The above example is actually evaluated at compile time, so if you were to view the assembly it would show the result only. Rust is pretty smart.
//!
//! Therefore, this is ideal for situations where simple component-wise operations are required for arrays.
//!
#![deny(missing_docs)]

extern crate num_traits;
extern crate typenum;

#[cfg_attr(test, macro_use)]
extern crate generic_array;
extern crate nodrop;

use std::{mem, ptr, slice};

use std::borrow::{Borrow, BorrowMut};
use std::ops::{Deref, DerefMut, Index, IndexMut};
use std::ops::{Range, RangeFrom, RangeTo, RangeFull};
use std::ops::{Add, Sub};

use std::fmt::{Debug, Formatter, Result as FmtResult};

use nodrop::NoDrop;
use typenum::*;
use generic_array::{ArrayLength, GenericArray, GenericArrayIter};

/// Defines any `NumericArray` type as its element and length types
///
/// This is useful for situations where the element type and length can be variable,
/// but you need a generic type bound and don't want to deal with
/// `T` and `N: ArrayLength<T>` from `NumericArray<T, N>` directly.
pub trait NumericSequence {
    /// Array element type
    type Element;

    /// Array length type
    type Length: ArrayLength<Self::Element>;
}

/// A numeric wrapper for a `GenericArray`, allowing for easy numerical operations
/// on the whole sequence.
///
/// This has the added bonus of allowing SIMD optimizations for almost all operations
/// when compiled with `RUSTFLAGS = "-C opt-level=3 -C target-cpu=native"`
///
/// For example, adding together four-element `NumericArray`'s will result
/// in a single SIMD instruction for all elements at once.
#[repr(C)]
pub struct NumericArray<T, N: ArrayLength<T>>(GenericArray<T, N>);

impl<T, N: ArrayLength<T>> NumericSequence for NumericArray<T, N> {
    type Element = T;
    type Length = N;
}

/// Sugar for `NumericArray::new(arr![...])`
///
/// ```ignore
/// #[macro_use]
/// extern crate generic_array;
/// ```
///
/// is required to use this, as it still uses the `arr!` macro internally.
#[macro_export]
macro_rules! narr {
    ($($t:tt)*) => {
        $crate::NumericArray::new(arr!($($t)*))
    }
}

/// This is required to allow `NumericArray` to be operated on by both other `NumericArray`
/// instances and constants, with generic types, because some type `U` supercedes `NumericArray<U, N>`
///
/// As a result, constants must be wrapped in this totally
/// transparent wrapper type to differentiate the types to Rust.
#[derive(Debug, Clone, Copy)]
pub struct NumericConstant<T>(pub T);

/// Creates a new `NumericConstant` from the given expression.
#[macro_export]
macro_rules! nconstant {
    ($value:expr) => { $crate::NumericConstant($value) }
}

impl<T> Deref for NumericConstant<T> {
    type Target = T;

    fn deref(&self) -> &T {
        &self.0
    }
}

impl<T> DerefMut for NumericConstant<T> {
    fn deref_mut(&mut self) -> &mut T {
        &mut self.0
    }
}

impl<T: Debug, N: ArrayLength<T>> Debug for NumericArray<T, N> {
    fn fmt(&self, f: &mut Formatter) -> FmtResult {
        f.debug_tuple("NumericArray").field(&self.0).finish()
    }
}

impl<T, N: ArrayLength<T>> From<GenericArray<T, N>> for NumericArray<T, N> {
    fn from(arr: GenericArray<T, N>) -> NumericArray<T, N> {
        NumericArray(arr)
    }
}

impl<T: Clone, N: ArrayLength<T>> Clone for NumericArray<T, N> {
    fn clone(&self) -> NumericArray<T, N> {
        self.map(|x| x.clone())
    }
}

impl<T: Copy, N: ArrayLength<T>> Copy for NumericArray<T, N>
where
    N::ArrayType: Copy,
{
}

impl<T, N: ArrayLength<T>> Deref for NumericArray<T, N> {
    type Target = [T];

    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

impl<T, N: ArrayLength<T>> DerefMut for NumericArray<T, N> {
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.0
    }
}

impl<T, U, N: ArrayLength<T> + ArrayLength<U>> PartialEq<NumericArray<U, N>> for NumericArray<T, N>
where
    T: PartialEq<U>,
{
    fn eq(&self, rhs: &NumericArray<U, N>) -> bool {
        **self == **rhs
    }
}

impl<T, U, N: ArrayLength<T> + ArrayLength<U>> PartialEq<GenericArray<U, N>> for NumericArray<T, N> where T: PartialEq<U> {
    fn eq(&self, rhs: &GenericArray<U, N>) -> bool {
        **self == **rhs
    }

    fn ne(&self, rhs: &GenericArray<U, N>) -> bool {
        **self == **rhs
    }
}

impl<T, N: ArrayLength<T>> ::std::cmp::Eq for NumericArray<T, N>
where
    T: ::std::cmp::Eq,
{
}

impl<T, N: ArrayLength<T>> PartialOrd<Self> for NumericArray<T, N>
where
    T: PartialOrd,
{
    #[inline]
    fn partial_cmp(&self, rhs: &Self) -> Option<::std::cmp::Ordering> {
        PartialOrd::partial_cmp(&self.0, &rhs.0)
    }

    #[inline]
    fn lt(&self, rhs: &Self) -> bool {
        PartialOrd::lt(&self.0, &rhs.0)
    }

    #[inline]
    fn le(&self, rhs: &Self) -> bool {
        PartialOrd::le(&self.0, &rhs.0)
    }

    #[inline]
    fn gt(&self, rhs: &Self) -> bool {
        PartialOrd::gt(&self.0, &rhs.0)
    }

    #[inline]
    fn ge(&self, rhs: &Self) -> bool {
        PartialOrd::ge(&self.0, &rhs.0)
    }
}

impl<T, N: ArrayLength<T>> PartialOrd<GenericArray<T, N>> for NumericArray<T, N>
where
    T: PartialOrd,
{
    #[inline]
    fn partial_cmp(&self, rhs: &GenericArray<T, N>) -> Option<::std::cmp::Ordering> {
        PartialOrd::partial_cmp(&self.0, rhs)
    }

    #[inline]
    fn lt(&self, rhs: &GenericArray<T, N>) -> bool {
        PartialOrd::lt(&self.0, rhs)
    }

    #[inline]
    fn le(&self, rhs: &GenericArray<T, N>) -> bool {
        PartialOrd::le(&self.0, rhs)
    }

    #[inline]
    fn gt(&self, rhs: &GenericArray<T, N>) -> bool {
        PartialOrd::gt(&self.0, rhs)
    }

    #[inline]
    fn ge(&self, rhs: &GenericArray<T, N>) -> bool {
        PartialOrd::ge(&self.0, rhs)
    }
}

impl<T, N: ArrayLength<T>> ::std::cmp::Ord for NumericArray<T, N>
where
    T: ::std::cmp::Ord,
{
    #[inline]
    fn cmp(&self, rhs: &Self) -> ::std::cmp::Ordering {
        ::std::cmp::Ord::cmp(&self.0, &rhs.0)
    }
}

impl<T, N: ArrayLength<T>> NumericArray<T, N> {
    /// Creates a new `NumericArray` instance from a `GenericArray` instance.
    ///
    /// Example:
    ///
    /// ```
    /// #[macro_use]
    /// extern crate generic_array;
    /// extern crate numeric_array;
    ///
    /// use numeric_array::NumericArray;
    ///
    /// fn main() {
    ///     let arr = NumericArray::new(arr![i32; 1, 2, 3, 4]);
    ///
    ///     println!("{:?}", arr); // Prints 'NumericArray([1, 2, 3, 4])'
    /// }
    /// ```
    #[inline]
    pub fn new(arr: GenericArray<T, N>) -> NumericArray<T, N> {
        NumericArray(arr)
    }

    /// Consumes self and returns the internal `GenericArray` instance
    #[inline]
    pub fn into_array(self) -> GenericArray<T, N> {
        self.0
    }

    /// Creates a new array filled with a single value.
    ///
    /// Example:
    ///
    /// ```ignore
    /// let a = NumericArray::new(arr![i32; 5, 5, 5, 5]);
    /// let b = NumericArray::from_element(5);
    ///
    /// assert_eq!(a, b);
    /// ```
    #[inline]
    pub fn from_element(t: T) -> NumericArray<T, N>
    where
        T: Clone,
    {
        Self::generate(|| t.clone())
    }

    /// Generates a new array using the given generator function to create each value.
    ///
    /// The generator function is called `N` times, once for each element.
    pub fn generate<G>(g: G) -> NumericArray<T, N>
    where
        G: Fn() -> T,
    {
        let mut res: NoDrop<GenericArray<T, N>> = NoDrop::new(unsafe { mem::uninitialized() });

        for dst in res.iter_mut() {
            unsafe {
                ptr::write(dst, g());
            }
        }

        NumericArray(res.into_inner())
    }

    /// Moves all but the last element into a `NumericArray` with one
    /// less element than the current one.
    ///
    /// The last element is dropped.
    ///
    /// Example:
    ///
    /// ```ignore
    /// let a = NumericArray::new(arr![i32; 1, 2, 3, 4]);
    /// let b = NumericArray::new(arr![i32; 1, 2, 3]);
    ///
    /// assert_eq!(a.shorten(), b);
    /// ```
    pub fn shorten(self) -> NumericArray<T, Sub1<N>>
    where
        N: Sub<B1>,
        Sub1<N>: ArrayLength<T>,
    {
        use std::{mem, ptr};

        let mut shorter: GenericArray<T, Sub1<N>> = unsafe { mem::uninitialized() };

        for (dst, src) in shorter.iter_mut().zip(self.iter()) {
            unsafe {
                ptr::write(dst, ptr::read(src));
            }
        }

        let _last = unsafe { ptr::read(&self.0[N::to_usize() - 1]) };

        mem::forget(self);

        NumericArray(shorter)
    }

    /// Moves all the current elements into a new array with one more element than the current one.
    ///
    /// The last element of the new array is set to `last`
    ///
    /// Example:
    ///
    /// ```ignore
    /// let a = NumericArray::new(arr![i32; 1, 2, 3, 4]);
    /// let b = NumericArray::new(arr![i32; 1, 2, 3]);
    ///
    /// assert_eq!(a, b.lengthen(4));
    /// ```
    pub fn lengthen(self, last: T) -> NumericArray<T, Add1<N>>
    where
        N: Add<B1>,
        Add1<N>: ArrayLength<T>,
    {
        use std::{mem, ptr};

        let mut longer: GenericArray<T, Add1<N>> = unsafe { mem::uninitialized() };

        for (dst, src) in longer.iter_mut().zip(self.iter()) {
            unsafe {
                ptr::write(dst, ptr::read(src));
            }
        }

        unsafe {
            ptr::write(&mut longer[N::to_usize()], last);
        }

        mem::forget(self);

        NumericArray(longer)
    }

    /// Maps the current array to a new one of the same size using the given function.
    pub fn map<U, F>(&self, f: F) -> NumericArray<U, N>
    where
        N: ArrayLength<U>,
        F: Fn(&T) -> U,
    {
        let mut res: NoDrop<GenericArray<U, N>> = NoDrop::new(unsafe { mem::uninitialized() });

        for (dst, src) in res.iter_mut().zip(self.iter()) {
            unsafe {
                ptr::write(dst, f(src));
            }
        }

        NumericArray(res.into_inner())
    }

    /// Same as `map`, but the values are moved rather than referenced.
    pub fn map_move<U, F>(self, f: F) -> NumericArray<U, N>
    where
        N: ArrayLength<U>,
        F: Fn(T) -> U,
    {
        let mut res: NoDrop<GenericArray<U, N>> = NoDrop::new(unsafe { mem::uninitialized() });

        for (dst, src) in res.iter_mut().zip(self.iter()) {
            unsafe {
                ptr::write(dst, f(ptr::read(src)));
            }
        }

        mem::forget(self);

        NumericArray(res.into_inner())
    }

    /// Combines two same-length arrays and maps both values to a new array using the given function.
    pub fn zip<V, U, F>(&self, rhs: &NumericArray<V, N>, f: F) -> NumericArray<U, N>
    where
        N: ArrayLength<V> + ArrayLength<U>,
        F: Fn(&T, &V) -> U,
    {
        let mut res: NoDrop<GenericArray<U, N>> = NoDrop::new(unsafe { mem::uninitialized() });

        for (dst, (lhs, rhs)) in res.iter_mut().zip(self.iter().zip(rhs.iter())) {
            unsafe {
                ptr::write(dst, f(lhs, rhs));
            }
        }

        NumericArray(res.into_inner())
    }

    /// Same as `zip`, but `self` values are moved. The `rhs` array is still accessed by reference.
    pub fn zip_move<V, U, F>(self, rhs: &NumericArray<V, N>, f: F) -> NumericArray<U, N>
    where
        N: ArrayLength<V> + ArrayLength<U>,
        F: Fn(T, &V) -> U,
    {
        let mut res: NoDrop<GenericArray<U, N>> = NoDrop::new(unsafe { mem::uninitialized() });

        for (dst, (lhs, rhs)) in res.iter_mut().zip(self.iter().zip(rhs.iter())) {
            unsafe {
                ptr::write(dst, f(ptr::read(lhs), rhs));
            }
        }

        mem::forget(self);

        NumericArray(res.into_inner())
    }

    /// Like `zip` and `zip_move`, but moves both `self` and the `rhs` array.
    pub fn zip_move_both<V, U, F>(self, rhs: NumericArray<V, N>, f: F) -> NumericArray<U, N>
    where
        N: ArrayLength<V> + ArrayLength<U>,
        F: Fn(T, V) -> U,
    {
        let mut res: NoDrop<GenericArray<U, N>> = NoDrop::new(unsafe { mem::uninitialized() });

        for (dst, (lhs, rhs)) in res.iter_mut().zip(self.iter().zip(rhs.iter())) {
            unsafe {
                ptr::write(dst, f(ptr::read(lhs), ptr::read(rhs)));
            }
        }

        mem::forget(self);
        mem::forget(rhs);

        NumericArray(res.into_inner())
    }

    /// Convert one `NumericArray` to another using `From` for each element.
    pub fn convert<U>(self) -> NumericArray<U, N>
    where
        N: ArrayLength<U>,
        U: From<T>,
    {
        self.map_move(From::from)
    }
}

impl<T, N: ArrayLength<T>> AsRef<[T]> for NumericArray<T, N> {
    fn as_ref(&self) -> &[T] {
        self
    }
}

impl<T, N: ArrayLength<T>> Borrow<[T]> for NumericArray<T, N> {
    fn borrow(&self) -> &[T] {
        self
    }
}

impl<T, N: ArrayLength<T>> AsMut<[T]> for NumericArray<T, N> {
    fn as_mut(&mut self) -> &mut [T] {
        self
    }
}

impl<T, N: ArrayLength<T>> BorrowMut<[T]> for NumericArray<T, N> {
    fn borrow_mut(&mut self) -> &mut [T] {
        self
    }
}

impl<T, N: ArrayLength<T>> Index<usize> for NumericArray<T, N> {
    type Output = T;

    #[inline]
    fn index(&self, index: usize) -> &T {
        &(**self)[index]
    }
}

impl<T, N: ArrayLength<T>> IndexMut<usize> for NumericArray<T, N> {
    #[inline]
    fn index_mut(&mut self, index: usize) -> &mut T {
        &mut (**self)[index]
    }
}

impl<T, N: ArrayLength<T>> Index<Range<usize>> for NumericArray<T, N> {
    type Output = [T];

    #[inline]
    fn index(&self, index: Range<usize>) -> &[T] {
        Index::index(&**self, index)
    }
}

impl<T, N: ArrayLength<T>> Index<RangeTo<usize>> for NumericArray<T, N> {
    type Output = [T];

    #[inline]
    fn index(&self, index: RangeTo<usize>) -> &[T] {
        Index::index(&**self, index)
    }
}

impl<T, N: ArrayLength<T>> Index<RangeFrom<usize>> for NumericArray<T, N> {
    type Output = [T];

    #[inline]
    fn index(&self, index: RangeFrom<usize>) -> &[T] {
        Index::index(&**self, index)
    }
}

impl<T, N: ArrayLength<T>> Index<RangeFull> for NumericArray<T, N> {
    type Output = [T];

    #[inline]
    fn index(&self, _index: RangeFull) -> &[T] {
        self
    }
}

impl<'a, T, N: ArrayLength<T>> IntoIterator for &'a NumericArray<T, N> {
    type Item = &'a T;
    type IntoIter = slice::Iter<'a, T>;

    #[inline]
    fn into_iter(self) -> Self::IntoIter {
        self.iter()
    }
}

impl<'a, T, N: ArrayLength<T>> IntoIterator for &'a mut NumericArray<T, N> {
    type Item = &'a mut T;
    type IntoIter = slice::IterMut<'a, T>;

    #[inline]
    fn into_iter(self) -> Self::IntoIter {
        self.iter_mut()
    }
}

impl<T, N: ArrayLength<T>> IntoIterator for NumericArray<T, N> {
    type Item = T;
    type IntoIter = GenericArrayIter<T, N>;

    fn into_iter(self) -> Self::IntoIter {
        self.0.into_iter()
    }
}

impl<T, N: ArrayLength<T>> Default for NumericArray<T, N>
where
    T: Default,
{
    fn default() -> Self {
        Self::generate(|| Default::default())
    }
}

impl<T, N: ArrayLength<T>> num_traits::Zero for NumericArray<T, N>
where
    T: num_traits::Zero,
{
    fn zero() -> Self {
        Self::generate(|| <T as num_traits::Zero>::zero())
    }

    fn is_zero(&self) -> bool {
        self.iter().all(|x| x.is_zero())
    }
}

impl<T, N: ArrayLength<T>> num_traits::One for NumericArray<T, N>
where
    T: num_traits::One,
{
    fn one() -> Self {
        Self::generate(|| <T as num_traits::One>::one())
    }
}

macro_rules! impl_unary_ops {
    ($($op_trait:ident::$op:ident),*) => {
        $(
            impl<T, N: ArrayLength<T>> ::std::ops::$op_trait for NumericArray<T, N>
            where
                T: ::std::ops::$op_trait,
                N: ArrayLength<<T as ::std::ops::$op_trait>::Output>
            {
                type Output = NumericArray<<T as ::std::ops::$op_trait>::Output, N>;

                fn $op(self) -> Self::Output {
                    self.map_move(::std::ops::$op_trait::$op)
                }
            }
        )*
    }
}

macro_rules! impl_binary_ops {
    ($($op_trait:ident::$op:ident),*) => {
        $(
            impl<T, U, N: ArrayLength<T> + ArrayLength<U>> ::std::ops::$op_trait<NumericArray<U, N>> for NumericArray<T, N>
            where
                T: ::std::ops::$op_trait<U>,
                N: ArrayLength<<T as ::std::ops::$op_trait<U>>::Output>
            {
                type Output = NumericArray<<T as ::std::ops::$op_trait<U>>::Output, N>;

                fn $op(self, rhs: NumericArray<U, N>) -> Self::Output {
                    self.zip_move_both(rhs, ::std::ops::$op_trait::$op)
                }
            }

            impl<T, U: Clone, N: ArrayLength<T>> ::std::ops::$op_trait<NumericConstant<U>> for NumericArray<T, N>
            where
                T: ::std::ops::$op_trait<U>,
                N: ArrayLength<<T as ::std::ops::$op_trait<U>>::Output>
            {
                type Output = NumericArray<<T as ::std::ops::$op_trait<U>>::Output, N>;

                fn $op(self, rhs: NumericConstant<U>) -> Self::Output {
                    self.map_move(|l| ::std::ops::$op_trait::$op(l, rhs.0.clone()))
                }
            }
        )*
    }
}

macro_rules! impl_assign_ops {
    ($($op_trait:ident::$op:ident),*) => {
        $(
            impl<T, U, N: ArrayLength<T> + ArrayLength<U>> ::std::ops::$op_trait<NumericArray<U, N>> for NumericArray<T, N>
            where
                T: ::std::ops::$op_trait<U>
            {
                fn $op(&mut self, rhs: NumericArray<U, N>) {
                    for (lhs, rhs) in self.iter_mut().zip(rhs.iter()) {
                        ::std::ops::$op_trait::$op(lhs, unsafe { ptr::read(rhs) });
                    }

                    mem::forget(rhs);
                }
            }

            impl<T, U: Clone, N: ArrayLength<T>> ::std::ops::$op_trait<NumericConstant<U>> for NumericArray<T, N>
            where
                T: ::std::ops::$op_trait<U>
            {
                fn $op(&mut self, rhs: NumericConstant<U>) {
                    for lhs in self.iter_mut() {
                        ::std::ops::$op_trait::$op(lhs, rhs.0.clone());
                    }
                }
            }
        )*
    }
}

macro_rules! impl_wrapping_ops {
    ($($op_trait:ident::$op:ident),*) => {
        $(
            impl<T, N: ArrayLength<T>> num_traits::$op_trait for NumericArray<T, N>
            where
                T: num_traits::$op_trait
            {
                fn $op(&self, rhs: &Self) -> Self {
                    self.zip(rhs, num_traits::$op_trait::$op)
                }
            }
        )*
    }
}

macro_rules! impl_checked_ops {
    ($($op_trait:ident::$op:ident),*) => {
        $(
            impl<T, N: ArrayLength<T>> num_traits::$op_trait for NumericArray<T, N>
            where
                T: num_traits::$op_trait
            {
                fn $op(&self, rhs: &Self) -> Option<Self> {
                    let mut res: NoDrop<GenericArray<T, N>> = NoDrop::new(unsafe { mem::uninitialized() });

                    for (dst, (lhs, rhs)) in res.iter_mut().zip(self.iter().zip(rhs.iter())) {
                        if let Some(value) = num_traits::$op_trait::$op(lhs, rhs) {
                            unsafe {
                                ptr::write(dst, value);
                            }
                        } else {
                            return None;
                        }
                    }

                    Some(NumericArray(res.into_inner()))
                }
            }
        )*
    }
}

impl_unary_ops!(Neg::neg, Not::not);

impl_binary_ops! {
    Add::add,
    Sub::sub,
    Mul::mul,
    Div::div,
    Rem::rem,
    BitAnd::bitand,
    BitOr::bitor,
    BitXor::bitxor,
    Shr::shr,
    Shl::shl
}

impl_assign_ops! {
    AddAssign::add_assign,
    SubAssign::sub_assign,
    MulAssign::mul_assign,
    DivAssign::div_assign,
    RemAssign::rem_assign,
    BitAndAssign::bitand_assign,
    BitOrAssign::bitor_assign,
    BitXorAssign::bitxor_assign,
    ShrAssign::shr_assign,
    ShlAssign::shl_assign
}

impl<T, N: ArrayLength<T>> num_traits::Saturating for NumericArray<T, N>
where
    T: num_traits::Saturating,
{
    fn saturating_add(self, rhs: Self) -> Self {
        self.zip_move_both(rhs, num_traits::Saturating::saturating_add)
    }

    fn saturating_sub(self, rhs: Self) -> Self {
        self.zip_move_both(rhs, num_traits::Saturating::saturating_sub)
    }
}

impl_wrapping_ops! {
    WrappingAdd::wrapping_add,
    WrappingSub::wrapping_sub,
    WrappingMul::wrapping_mul
}

impl_checked_ops! {
    CheckedAdd::checked_add,
    CheckedSub::checked_sub,
    CheckedMul::checked_mul,
    CheckedDiv::checked_div
}

impl<T: Clone, N: ArrayLength<T>> num_traits::Num for NumericArray<T, N>
where
    T: num_traits::Num,
{
    type FromStrRadixErr = <T as num_traits::Num>::FromStrRadixErr;

    fn from_str_radix(str: &str, radix: u32) -> Result<Self, Self::FromStrRadixErr> {
        <T as num_traits::Num>::from_str_radix(str, radix).map(Self::from_element)
    }
}

impl<T: Clone, N: ArrayLength<T>> num_traits::Signed for NumericArray<T, N>
where
    T: num_traits::Signed,
{
    fn abs(&self) -> Self {
        self.map(num_traits::Signed::abs)
    }

    fn abs_sub(&self, rhs: &Self) -> Self {
        self.zip(rhs, num_traits::Signed::abs_sub)
    }

    fn signum(&self) -> Self {
        self.map(num_traits::Signed::signum)
    }

    fn is_positive(&self) -> bool {
        self.iter().all(num_traits::Signed::is_positive)
    }

    fn is_negative(&self) -> bool {
        self.iter().any(num_traits::Signed::is_negative)
    }
}

impl<T: Clone, N: ArrayLength<T>> num_traits::Unsigned for NumericArray<T, N>
where
    T: num_traits::Unsigned,
{
}

macro_rules! impl_float_const {
    ($($f:ident),*) => {
        impl<T, N: ArrayLength<T>> num_traits::FloatConst for NumericArray<T, N>
        where
            T: num_traits::FloatConst
        {
            $(
                fn $f() -> Self {
                    Self::generate(|| <T as num_traits::FloatConst>::$f())
                }
            )*
        }
    }
}

impl_float_const!(
    E,
    FRAC_1_PI,
    FRAC_1_SQRT_2,
    FRAC_2_PI,
    FRAC_2_SQRT_PI,
    FRAC_PI_2,
    FRAC_PI_3,
    FRAC_PI_4,
    FRAC_PI_6,
    FRAC_PI_8,
    LN_10,
    LN_2,
    LOG10_E,
    LOG2_E,
    PI,
    SQRT_2
);

impl<T, N: ArrayLength<T>> num_traits::Bounded for NumericArray<T, N>
where
    T: num_traits::Bounded,
{
    fn min_value() -> Self {
        Self::generate(|| <T as num_traits::Bounded>::min_value())
    }

    fn max_value() -> Self {
        Self::generate(|| <T as num_traits::Bounded>::max_value())
    }
}

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

    #[inline(never)]
    pub fn black_box<T>(val: T) -> T {
        use std::{mem, ptr};

        let ret = unsafe { ptr::read_volatile(&val) };
        mem::forget(val);
        ret
    }

    #[test]
    fn shorten() {
        let a = NumericArray::new(arr![i32; 1, 2, 3, 4]);
        let b = NumericArray::new(arr![i32; 1, 2, 3]);

        assert_eq!(a.shorten(), b);
    }

    #[test]
    fn lengthen() {
        let a = NumericArray::new(arr![i32; 1, 2, 3, 4]);
        let b = NumericArray::new(arr![i32; 1, 2, 3]);

        assert_eq!(a, b.lengthen(4));
    }

    #[test]
    fn ops() {
        let a = black_box(NumericArray::new(arr![i32; 1, 3, 5, 7]));
        let b = black_box(NumericArray::new(arr![i32; 2, 4, 6, 8]));

        let c = a + b;
        let d = c * nconstant!(black_box(5));
        let e = d << nconstant!(1_usize);

        assert_eq!(e, NumericArray::new(arr![i32; 30, 70, 110, 150]))
    }

    #[test]
    fn readme_example() {
        let a = narr![i32; 1, 3, 5, 7];
        let b = narr![i32; 2, 4, 6, 8];

        let c = a + b * nconstant!(2);

        assert_eq!(c, narr![i32; 5, 11, 17, 23]);
    }
}