add third, fourth, fifth lenses

This commit is contained in:
annieversary 2021-11-12 11:17:33 +00:00
parent fa60e84466
commit e6ac6d85fa
4 changed files with 323 additions and 0 deletions

108
src/lenses/fifth.rs Normal file
View file

@ -0,0 +1,108 @@
use crate::lenses::{Lens, LensOver, LensView};
#[derive(Clone, Copy)]
pub struct _4Inner;
pub const _4: Lens<_4Inner> = Lens(_4Inner);
macro_rules! make_tuples {
($f:ident, ( $( $v:ident ),* ), ( $( $t:ident ),* ) ) => {
impl< $($t,)* > LensView<( $($t,)* )> for _4Inner {
type Field = T;
fn view(&self, ( $($v,)* ): ($($t,)*)) -> Self::Field {
$f
}
}
impl< $($t,)* > LensOver<( $($t,)* )> for _4Inner {
fn over<F>(
&self,
mut tup: ($($t,)*),
f: F
) -> ( $($t,)* )
where
F: FnOnce(Self::Field) -> Self::Field
{
tup.4 = f(tup.4);
tup
}
}
impl<'a, $($t,)* > LensView<&'a ( $($t,)* )> for _4Inner {
type Field = &'a T;
fn view(&self, ( $($v,)* ): &'a ($($t,)*)) -> Self::Field {
$f
}
}
impl<'a, $($t,)* > LensView<&'a mut ( $($t,)* )> for _4Inner {
type Field = &'a mut T;
fn view(&self, ( $($v,)* ): &'a mut ($($t,)*)) -> Self::Field {
$f
}
}
};
}
make_tuples!(t, (_u, _v, _w, _x, t), (U, V, W, X, T));
make_tuples!(t, (_u, _v, _w, _x, t, _y), (U, V, W, X, T, Y));
make_tuples!(t, (_u, _v, _w, _x, t, _y, _z), (U, V, W, X, T, Y, Z));
make_tuples!(t, (_u, _v, _w, _x, t, _y, _z, _a), (U, V, W, X, T, Y, Z, A));
make_tuples!(
t,
(_u, _v, _w, _x, t, _y, _z, _a, _b),
(U, V, W, X, T, Y, Z, A, B)
);
make_tuples!(
t,
(_u, _v, _w, _x, t, _y, _z, _a, _b, _c),
(U, V, W, X, T, Y, Z, A, B, C)
);
// 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 _4Inner {
type Field = T;
fn view(&self, [ $($v,)* ]: [T; $n]) -> Self::Field {
$f
}
}
impl<T> LensOver<[T; $n]> for _4Inner {
fn over<F>(
&self,
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 _4Inner {
type Field = &'a T;
fn view(&self, [ $($v,)* ]: &'a [T; $n]) -> Self::Field {
$f
}
}
impl<'a, T> LensView<&'a mut [T; $n]> for _4Inner {
type Field = &'a mut T;
fn view(&self, [ $($v,)* ]: &'a mut [T; $n]) -> Self::Field {
$f
}
}
};
}
make_arrays!(t, 5, [_a, _b, _c, _d, t]);
make_arrays!(t, 6, [_a, _b, _c, _d, t, _e]);
make_arrays!(t, 7, [_a, _b, _c, _d, t, _e, _g]);
make_arrays!(t, 8, [_a, _b, _c, _d, t, _e, _g, _h]);
make_arrays!(t, 9, [_a, _b, _c, _d, t, _e, _g, _h, _i]);

110
src/lenses/fourth.rs Normal file
View file

@ -0,0 +1,110 @@
use crate::lenses::{Lens, LensOver, LensView};
#[derive(Clone, Copy)]
pub struct _3Inner;
pub const _3: Lens<_3Inner> = Lens(_3Inner);
macro_rules! make_tuples {
($f:ident, ( $( $v:ident ),* ), ( $( $t:ident ),* ) ) => {
impl< $($t,)* > LensView<( $($t,)* )> for _3Inner {
type Field = T;
fn view(&self, ( $($v,)* ): ($($t,)*)) -> Self::Field {
$f
}
}
impl< $($t,)* > LensOver<( $($t,)* )> for _3Inner {
fn over<F>(
&self,
mut tup: ($($t,)*),
f: F
) -> ( $($t,)* )
where
F: FnOnce(Self::Field) -> Self::Field
{
tup.3 = f(tup.3);
tup
}
}
impl<'a, $($t,)* > LensView<&'a ( $($t,)* )> for _3Inner {
type Field = &'a T;
fn view(&self, ( $($v,)* ): &'a ($($t,)*)) -> Self::Field {
$f
}
}
impl<'a, $($t,)* > LensView<&'a mut ( $($t,)* )> for _3Inner {
type Field = &'a mut T;
fn view(&self, ( $($v,)* ): &'a mut ($($t,)*)) -> Self::Field {
$f
}
}
};
}
make_tuples!(t, (_u, _v, _w, t), (U, V, W, T));
make_tuples!(t, (_u, _v, _w, t, _x), (U, V, W, T, X));
make_tuples!(t, (_u, _v, _w, t, _x, _y), (U, V, W, T, X, Y));
make_tuples!(t, (_u, _v, _w, t, _x, _y, _z), (U, V, W, T, X, Y, Z));
make_tuples!(t, (_u, _v, _w, t, _x, _y, _z, _a), (U, V, W, T, X, Y, Z, A));
make_tuples!(
t,
(_u, _v, _w, t, _x, _y, _z, _a, _b),
(U, V, W, T, X, Y, Z, A, B)
);
make_tuples!(
t,
(_u, _v, _w, t, _x, _y, _z, _a, _b, _c),
(U, V, W, T, X, Y, Z, A, B, C)
);
// 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 _3Inner {
type Field = T;
fn view(&self, [ $($v,)* ]: [T; $n]) -> Self::Field {
$f
}
}
impl<T> LensOver<[T; $n]> for _3Inner {
fn over<F>(
&self,
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 _3Inner {
type Field = &'a T;
fn view(&self, [ $($v,)* ]: &'a [T; $n]) -> Self::Field {
$f
}
}
impl<'a, T> LensView<&'a mut [T; $n]> for _3Inner {
type Field = &'a mut T;
fn view(&self, [ $($v,)* ]: &'a mut [T; $n]) -> Self::Field {
$f
}
}
};
}
make_arrays!(t, 4, [_a, _b, _c, t]);
make_arrays!(t, 5, [_a, _b, _c, t, _d]);
make_arrays!(t, 6, [_a, _b, _c, t, _d, _e]);
make_arrays!(t, 7, [_a, _b, _c, t, _d, _e, _g]);
make_arrays!(t, 8, [_a, _b, _c, t, _d, _e, _g, _h]);
make_arrays!(t, 9, [_a, _b, _c, t, _d, _e, _g, _h, _i]);

View file

@ -7,6 +7,12 @@ mod first;
pub use first::_0;
mod second;
pub use second::_1;
mod third;
pub use third::_2;
mod fourth;
pub use fourth::_3;
mod fifth;
pub use fifth::_4;
mod to;
pub use to::{to, to_from_boxed};

99
src/lenses/third.rs Normal file
View file

@ -0,0 +1,99 @@
use crate::lenses::{Lens, LensOver, LensView};
#[derive(Clone, Copy)]
pub struct _2Inner;
pub const _2: Lens<_2Inner> = Lens(_2Inner);
macro_rules! make_tuples {
($f:ident, ( $( $v:ident ),* ), ( $( $t:ident ),* ) ) => {
impl< $($t,)* > LensView<( $($t,)* )> for _2Inner {
type Field = T;
fn view(&self, ( $($v,)* ): ($($t,)*)) -> Self::Field {
$f
}
}
impl< $($t,)* > LensOver<( $($t,)* )> for _2Inner {
fn over<F>(
&self,
mut tup: ($($t,)*),
f: F
) -> ( $($t,)* )
where
F: FnOnce(Self::Field) -> Self::Field
{
tup.2 = f(tup.2);
tup
}
}
impl<'a, $($t,)* > LensView<&'a ( $($t,)* )> for _2Inner {
type Field = &'a T;
fn view(&self, ( $($v,)* ): &'a ($($t,)*)) -> Self::Field {
$f
}
}
impl<'a, $($t,)* > LensView<&'a mut ( $($t,)* )> for _2Inner {
type Field = &'a mut T;
fn view(&self, ( $($v,)* ): &'a mut ($($t,)*)) -> Self::Field {
$f
}
}
};
}
make_tuples!(t, (_u, _v, t), (U, V, T));
make_tuples!(t, (_u, _v, t, _w), (U, V, T, W));
make_tuples!(t, (_u, _v, t, _w, _x), (U, V, T, W, X));
make_tuples!(t, (_u, _v, t, _w, _x, _y), (U, V, T, W, X, Y));
make_tuples!(t, (_u, _v, t, _w, _x, _y, _z), (U, V, T, 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<T> LensView<[T; $n]> for _2Inner {
type Field = T;
fn view(&self, [ $($v,)* ]: [T; $n]) -> Self::Field {
$f
}
}
impl<T> LensOver<[T; $n]> for _2Inner {
fn over<F>(
&self,
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 _2Inner {
type Field = &'a T;
fn view(&self, [ $($v,)* ]: &'a [T; $n]) -> Self::Field {
$f
}
}
impl<'a, T> LensView<&'a mut [T; $n]> for _2Inner {
type Field = &'a mut T;
fn view(&self, [ $($v,)* ]: &'a mut [T; $n]) -> Self::Field {
$f
}
}
};
}
make_arrays!(t, 3, [_a, _b, t]);
make_arrays!(t, 4, [_a, _b, t, _c]);
make_arrays!(t, 5, [_a, _b, t, _c, _d]);
make_arrays!(t, 6, [_a, _b, t, _c, _d, _e]);
make_arrays!(t, 7, [_a, _b, t, _c, _d, _e, _g]);