add arrays to _0

main
annieversary 2021-11-05 12:09:20 +00:00
parent d76541fbc5
commit fbf0bfff21
2 changed files with 58 additions and 2 deletions

View File

@ -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), (T, U, V, W, X, Y));
make_tuples!(t, (t, _u, _v, _w, _x, _y, _z), (T, U, V, W, X, Y, Z)); 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 :) // 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<T> LensView<[T; $n]> for _0 {
type Field = T;
fn view([ $($v,)* ]: [T; $n]) -> Self::Field {
$f
}
}
impl<T> LensOver<[T; $n]> for _0 {
fn over<F>(
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]);

View File

@ -84,11 +84,19 @@ mod tests {
#[test] #[test]
fn over_first_from_tuple() { fn over_first_from_tuple() {
let a = (1, 2); 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)); assert_eq!(a, (2, 2));
let a = (1, 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)); 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]);
}
} }