From fbf0bfff21533e4a4528d527d2e25d8d672b4303 Mon Sep 17 00:00:00 2001 From: annieversary Date: Fri, 5 Nov 2021 12:09:20 +0000 Subject: [PATCH] add arrays to _0 --- src/lenses/first.rs | 48 +++++++++++++++++++++++++++++++++++++++++++++ src/lib.rs | 12 ++++++++++-- 2 files changed, 58 insertions(+), 2 deletions(-) diff --git a/src/lenses/first.rs b/src/lenses/first.rs index a89fc74..3c717b3 100644 --- a/src/lenses/first.rs +++ b/src/lenses/first.rs @@ -48,3 +48,51 @@ make_tuples!(t, (t, _u, _v, _w, _x), (T, U, V, W, X)); make_tuples!(t, (t, _u, _v, _w, _x, _y), (T, U, V, W, X, Y)); make_tuples!(t, (t, _u, _v, _w, _x, _y, _z), (T, U, V, W, X, Y, Z)); // not doing more cause i'm lazy, open a pr if you need more :) + +macro_rules! make_arrays { + ($f:ident, $n:expr, [$( $v:ident ),*]) => { + impl LensView<[T; $n]> for _0 { + type Field = T; + + fn view([ $($v,)* ]: [T; $n]) -> Self::Field { + $f + } + } + impl LensOver<[T; $n]> for _0 { + fn over( + tup: [T; $n], + fun: F + ) -> [T; $n] + where + F: FnOnce(Self::Field) -> Self::Field + { + let [$($v,)*] = tup; + let $f = fun( $f ); + [$($v,)*] + } + } + + impl<'a, T> LensView<&'a [T; $n]> for _0 { + type Field = &'a T; + + fn view([ $($v,)* ]: &'a [T; $n]) -> Self::Field { + $f + } + } + impl<'a, T> LensView<&'a mut [T; $n]> for _0 { + type Field = &'a mut T; + + fn view([ $($v,)* ]: &'a mut [T; $n]) -> Self::Field { + $f + } + } + }; +} + +make_arrays!(t, 1, [t]); +make_arrays!(t, 2, [t, _a]); +make_arrays!(t, 3, [t, _a, _b]); +make_arrays!(t, 4, [t, _a, _b, _c]); +make_arrays!(t, 5, [t, _a, _b, _c, _d]); +make_arrays!(t, 6, [t, _a, _b, _c, _d, _e]); +make_arrays!(t, 7, [t, _a, _b, _c, _d, _e, _g]); diff --git a/src/lib.rs b/src/lib.rs index 5e8faa1..979a10c 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -84,11 +84,19 @@ mod tests { #[test] fn over_first_from_tuple() { let a = (1, 2); - let a = over(_0, a, &|v| v + 1); + let a = over(_0, a, |v| v + 1); assert_eq!(a, (2, 2)); let a = (1, 2); - let a = _0::over(a, &|v| v + 1); + let a = _0::over(a, |v| v + 1); assert_eq!(a, (2, 2)); } + + #[test] + fn over_first_from_array() { + let a = [1, 2, 3, 4]; + + let a = _0::over(a, |v| v + 1); + assert_eq!(a, [2, 2, 3, 4]); + } }