const generic expr based fixed length array manipulation crate
array + array
| -rw-r--r-- | Cargo.toml | 2 | ||||
| -rw-r--r-- | src/pervasive.rs | 32 |
2 files changed, 32 insertions, 2 deletions
@@ -1,6 +1,6 @@ [package] name = "atools" -version = "0.1.0" +version = "0.1.1" edition = "2021" description = "const generic expr based fixed length array manipulation" authors = ["bend-n <[email protected]>"] diff --git a/src/pervasive.rs b/src/pervasive.rs index 3672780..afe5846 100644 --- a/src/pervasive.rs +++ b/src/pervasive.rs @@ -57,6 +57,36 @@ pub mod array_and_scalar { op!(Sub, AASSub, sub); } +mod array_and_array { + //! traits for array $op scalar + macro_rules! op { + ($op:ident, $n:ident, $f:ident, $name:ident) => { + #[doc = concat!("see [`", stringify!($f), "`](core::ops::", stringify!($op), "::", stringify!($f), ")")] + pub trait $n<T, const N: usize> { + #[doc = concat!("apply the [`", stringify!($f), "`](core::ops::", stringify!($op), "::", stringify!($f), ") function to the elements of both of these arrays.")] + fn $name(self, rhs: [T; N]) -> Self; + } + + impl<T: core::ops::$op<Output = T> + Copy, const N: usize> $n<T, N> for [T; N] { + fn $name(self, rhs: [T; N]) -> Self { + use crate::ArrayTools; + self.zip(rhs).map(|(a, b)| core::ops::$op::$f(a, b)) + } + } + }; + } + op!(Add, AAAdd, add, aadd); + op!(BitAnd, AAAnd, bitand, aand); + op!(BitOr, AAOr, bitor, aor); + op!(BitXor, AAXor, bitxor, axor); + op!(Div, AADiv, div, adiv); + op!(Mul, AAMul, mul, amul); + op!(Rem, AARem, rem, arem); + op!(Shl, AAShl, shl, ashl); + op!(Shr, AAShr, shr, ashr); + op!(Sub, AASub, sub, asub); +} + /// see [`not`](core::ops::Not::not) pub trait ANot<T, const N: usize> { /// apply the [`not`](core::ops::Not::not) function to each element of this array. @@ -82,7 +112,7 @@ impl<T: core::ops::Neg<Output = T>, const N: usize> ANeg<T, N> for [T; N] { /// Prelude for pervasive operations. pub mod prelude { #[doc(inline)] - pub use super::{array_and_scalar::*, scalar_and_array::*, ANeg, ANot}; + pub use super::{array_and_array::*, array_and_scalar::*, scalar_and_array::*, ANeg, ANot}; } #[test] fn x() { |