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
#![deny(unsafe_op_in_unsafe_fn)]
#![cfg_attr(not(any(doc, test, feature = "std")), no_std)]
use core::ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Sub, SubAssign};
#[cfg(test)]
mod tests;
mod iter;
use iter::{BinOpsIter, Iter};
pub struct Array<T, const N: usize>(pub [T; N]);
fn binop_impl<T, U, O, const N: usize>(
lhs: [T; N],
rhs: [U; N],
op: impl Fn(T, U) -> O + Copy,
) -> [O; N] {
let mut dc = BinOpsIter::new(lhs, rhs);
for _ in 0..32 {
unsafe { dc.step(op) }
}
unsafe { dc.output() }
}
fn binop_assign_impl<T, U, const N: usize>(
lhs: &mut [T; N],
rhs: [U; N],
op: impl Fn(&mut T, U) + Copy,
) {
let mut dc = Iter::new(rhs);
for _ in 0..32 {
unsafe { op(lhs.get_unchecked_mut(dc.index()), dc.next_unchecked()) }
}
}
macro_rules! binop {
($trait:ident, $method:ident) => {
impl<T, U, const N: usize> $trait<[U; N]> for Array<T, N>
where
T: $trait<U>,
{
type Output = [T::Output; N];
fn $method(self, rhs: [U; N]) -> Self::Output {
binop_impl(self.0, rhs, T::$method)
}
}
};
}
macro_rules! binop_assign {
($trait:ident, $method:ident) => {
impl<T, U, const N: usize> $trait<[U; N]> for Array<T, N>
where
T: $trait<U>,
{
fn $method(&mut self, rhs: [U; N]) {
binop_assign_impl(&mut self.0, rhs, T::$method)
}
}
};
}
binop!(Add, add);
binop!(Mul, mul);
binop!(Div, div);
binop!(Sub, sub);
binop_assign!(AddAssign, add_assign);
binop_assign!(MulAssign, mul_assign);
binop_assign!(DivAssign, div_assign);
binop_assign!(SubAssign, sub_assign);