change lenses to take a ref

main
annieversary 2021-11-11 10:39:47 +00:00
parent ea617b8789
commit 61484339f9
7 changed files with 47 additions and 64 deletions

View File

@ -1,6 +1,5 @@
use crate::{ use crate::{
lenses::{LensOver, LensView}, lenses::{LensOver, LensView},
prisms::PrismPreview,
Optics, OpticsTrait, Optics, OpticsTrait,
}; };
@ -27,8 +26,8 @@ where
{ {
type Field = B::Field; type Field = B::Field;
fn view(thing: T) -> Self::Field { fn view(&self, thing: T) -> Self::Field {
B::view(A::view(thing)) B::view(&self.1, A::view(&self.0, thing))
} }
} }
@ -37,22 +36,10 @@ where
A: LensOver<T>, A: LensOver<T>,
B: LensOver<A::Field>, B: LensOver<A::Field>,
{ {
fn over<F>(thing: T, f: F) -> T fn over<F>(&self, thing: T, f: F) -> T
where where
F: FnOnce(Self::Field) -> Self::Field, F: FnOnce(Self::Field) -> Self::Field,
{ {
A::over(thing, |b| B::over(b, f)) A::over(&self.0, thing, |b| B::over(&self.1, b, f))
}
}
impl<A, B, T> PrismPreview<T> for Combination<A, B>
where
A: LensView<T>,
B: PrismPreview<A::Field>,
{
type Field = B::Field;
fn preview(thing: T) -> Option<Self::Field> {
B::preview(A::view(thing))
} }
} }

View File

@ -10,7 +10,7 @@ where
type Output = L::Field; type Output = L::Field;
extern "rust-call" fn call_once(self, args: (A,)) -> Self::Output { extern "rust-call" fn call_once(self, args: (A,)) -> Self::Output {
L::view(args.0) L::view(&self.0, args.0)
} }
} }
impl<L, A> std::ops::FnMut<(A,)> for Optics<L> impl<L, A> std::ops::FnMut<(A,)> for Optics<L>
@ -18,7 +18,7 @@ where
L: LensView<A>, L: LensView<A>,
{ {
extern "rust-call" fn call_mut(&mut self, args: (A,)) -> Self::Output { extern "rust-call" fn call_mut(&mut self, args: (A,)) -> Self::Output {
L::view(args.0) L::view(&self.0, args.0)
} }
} }
impl<L, A> std::ops::Fn<(A,)> for Optics<L> impl<L, A> std::ops::Fn<(A,)> for Optics<L>
@ -26,7 +26,7 @@ where
L: LensView<A>, L: LensView<A>,
{ {
extern "rust-call" fn call(&self, args: (A,)) -> Self::Output { extern "rust-call" fn call(&self, args: (A,)) -> Self::Output {
L::view(args.0) L::view(&self.0, args.0)
} }
} }
@ -38,7 +38,7 @@ where
type Output = A; type Output = A;
extern "rust-call" fn call_once(self, args: (A, F)) -> Self::Output { extern "rust-call" fn call_once(self, args: (A, F)) -> Self::Output {
L::over(args.0, args.1) L::over(&self.0, args.0, args.1)
} }
} }
impl<L, A, F> std::ops::FnMut<(A, F)> for Optics<L> impl<L, A, F> std::ops::FnMut<(A, F)> for Optics<L>
@ -47,7 +47,7 @@ where
F: FnOnce(L::Field) -> L::Field, F: FnOnce(L::Field) -> L::Field,
{ {
extern "rust-call" fn call_mut(&mut self, args: (A, F)) -> Self::Output { extern "rust-call" fn call_mut(&mut self, args: (A, F)) -> Self::Output {
L::over(args.0, args.1) L::over(&self.0, args.0, args.1)
} }
} }
impl<L, A, F> std::ops::Fn<(A, F)> for Optics<L> impl<L, A, F> std::ops::Fn<(A, F)> for Optics<L>
@ -56,6 +56,6 @@ where
F: FnOnce(L::Field) -> L::Field, F: FnOnce(L::Field) -> L::Field,
{ {
extern "rust-call" fn call(&self, args: (A, F)) -> Self::Output { extern "rust-call" fn call(&self, args: (A, F)) -> Self::Output {
L::over(args.0, args.1) L::over(&self.0, args.0, args.1)
} }
} }

View File

@ -13,12 +13,13 @@ macro_rules! make_tuples {
impl< $($t,)* > LensView<( $($t,)* )> for _0Inner { impl< $($t,)* > LensView<( $($t,)* )> for _0Inner {
type Field = T; type Field = T;
fn view(( $($v,)* ): ($($t,)*)) -> Self::Field { fn view(&self, ( $($v,)* ): ($($t,)*)) -> Self::Field {
$f $f
} }
} }
impl< $($t,)* > LensOver<( $($t,)* )> for _0Inner { impl< $($t,)* > LensOver<( $($t,)* )> for _0Inner {
fn over<F>( fn over<F>(
&self,
mut tup: ($($t,)*), mut tup: ($($t,)*),
f: F f: F
) -> ( $($t,)* ) ) -> ( $($t,)* )
@ -30,17 +31,17 @@ macro_rules! make_tuples {
} }
} }
impl<'a, $($t,)* > LensView<&'a ( $($t,)* )> for _0Inner { impl<'a, $($t,)* > LensView<&'a ( $($t,)* )> for _0Inner {
type Field = &'a T; type Field = &'a T;
fn view(( $($v,)* ): &'a ($($t,)*)) -> Self::Field { fn view(&self, ( $($v,)* ): &'a ($($t,)*)) -> Self::Field {
$f $f
} }
} }
impl<'a, $($t,)* > LensView<&'a mut ( $($t,)* )> for _0Inner { impl<'a, $($t,)* > LensView<&'a mut ( $($t,)* )> for _0Inner {
type Field = &'a mut T; type Field = &'a mut T;
fn view(( $($v,)* ): &'a mut ($($t,)*)) -> Self::Field { fn view(&self, ( $($v,)* ): &'a mut ($($t,)*)) -> Self::Field {
$f $f
} }
} }
@ -60,12 +61,13 @@ macro_rules! make_arrays {
impl<T> LensView<[T; $n]> for _0Inner { impl<T> LensView<[T; $n]> for _0Inner {
type Field = T; type Field = T;
fn view([ $($v,)* ]: [T; $n]) -> Self::Field { fn view(&self, [ $($v,)* ]: [T; $n]) -> Self::Field {
$f $f
} }
} }
impl<T> LensOver<[T; $n]> for _0Inner { impl<T> LensOver<[T; $n]> for _0Inner {
fn over<F>( fn over<F>(
&self,
tup: [T; $n], tup: [T; $n],
fun: F fun: F
) -> [T; $n] ) -> [T; $n]
@ -81,14 +83,14 @@ macro_rules! make_arrays {
impl<'a, T> LensView<&'a [T; $n]> for _0Inner { impl<'a, T> LensView<&'a [T; $n]> for _0Inner {
type Field = &'a T; type Field = &'a T;
fn view([ $($v,)* ]: &'a [T; $n]) -> Self::Field { fn view(&self, [ $($v,)* ]: &'a [T; $n]) -> Self::Field {
$f $f
} }
} }
impl<'a, T> LensView<&'a mut [T; $n]> for _0Inner { impl<'a, T> LensView<&'a mut [T; $n]> for _0Inner {
type Field = &'a mut T; type Field = &'a mut T;
fn view([ $($v,)* ]: &'a mut [T; $n]) -> Self::Field { fn view(&self, [ $($v,)* ]: &'a mut [T; $n]) -> Self::Field {
$f $f
} }
} }

View File

@ -11,12 +11,12 @@ impl OpticsTrait for id {}
impl<T> LensView<T> for id { impl<T> LensView<T> for id {
type Field = T; type Field = T;
fn view(thing: T) -> Self::Field { fn view(&self, thing: T) -> Self::Field {
thing thing
} }
} }
impl<T> LensOver<T> for id { impl<T> LensOver<T> for id {
fn over<F>(thing: T, f: F) -> T fn over<F>(&self, thing: T, f: F) -> T
where where
F: FnOnce(Self::Field) -> Self::Field, F: FnOnce(Self::Field) -> Self::Field,
{ {

View File

@ -6,23 +6,25 @@ pub use first::_0;
mod second; mod second;
pub use second::_1; pub use second::_1;
mod to;
use crate::{Optics, OpticsTrait}; use crate::{Optics, OpticsTrait};
/// For lenses that allow viewing /// For lenses that allow viewing
pub trait LensView<T>: OpticsTrait { pub trait LensView<T>: OpticsTrait {
type Field; type Field;
fn view(thing: T) -> Self::Field; fn view(&self, thing: T) -> Self::Field;
} }
/// For lenses that allow setting /// For lenses that allow setting
pub trait LensOver<T>: LensView<T> { pub trait LensOver<T>: LensView<T> {
fn over<F>(thing: T, f: F) -> T fn over<F>(&self, thing: T, f: F) -> T
where where
F: FnOnce(Self::Field) -> Self::Field; F: FnOnce(Self::Field) -> Self::Field;
fn set(thing: T, v: Self::Field) -> T { fn set(&self, thing: T, v: Self::Field) -> T {
Self::over(thing, |_| v) Self::over(self, thing, |_| v)
} }
} }
@ -32,30 +34,30 @@ where
{ {
type Field = L::Field; type Field = L::Field;
fn view(thing: T) -> Self::Field { fn view(&self, thing: T) -> Self::Field {
L::view(thing) L::view(&self.0, thing)
} }
} }
impl<L, T> LensOver<T> for Optics<L> impl<L, T> LensOver<T> for Optics<L>
where where
L: LensOver<T>, L: LensOver<T>,
{ {
fn over<F>(thing: T, f: F) -> T fn over<F>(&self, thing: T, f: F) -> T
where where
F: FnOnce(Self::Field) -> Self::Field, F: FnOnce(Self::Field) -> Self::Field,
{ {
L::over(thing, f) L::over(&self.0, thing, f)
} }
} }
pub fn view<T, L: LensView<T>>(_lens: L, thing: T) -> L::Field { pub fn view<T, L: LensView<T>>(lens: L, thing: T) -> L::Field {
L::view(thing) L::view(&lens, thing)
} }
pub fn set<T, L: LensOver<T>>(_lens: L, thing: T, v: L::Field) -> T { pub fn set<T, L: LensOver<T>>(lens: L, thing: T, v: L::Field) -> T {
L::set(thing, v) L::set(&lens, thing, v)
} }
pub fn over<T, L: LensOver<T>>(_lens: L, thing: T, f: impl FnOnce(L::Field) -> L::Field) -> T { pub fn over<T, L: LensOver<T>>(lens: L, thing: T, f: impl FnOnce(L::Field) -> L::Field) -> T {
L::over(thing, f) L::over(&lens, thing, f)
} }
#[cfg(test)] #[cfg(test)]

View File

@ -13,12 +13,13 @@ macro_rules! make_tuples {
impl< $($t,)* > LensView<( $($t,)* )> for _1Inner { impl< $($t,)* > LensView<( $($t,)* )> for _1Inner {
type Field = T; type Field = T;
fn view(( $($v,)* ): ($($t,)*)) -> Self::Field { fn view(&self, ( $($v,)* ): ($($t,)*)) -> Self::Field {
$f $f
} }
} }
impl< $($t,)* > LensOver<( $($t,)* )> for _1Inner { impl< $($t,)* > LensOver<( $($t,)* )> for _1Inner {
fn over<F>( fn over<F>(
&self,
mut tup: ($($t,)*), mut tup: ($($t,)*),
f: F f: F
) -> ( $($t,)* ) ) -> ( $($t,)* )
@ -33,14 +34,14 @@ macro_rules! make_tuples {
impl<'a, $($t,)* > LensView<&'a ( $($t,)* )> for _1Inner { impl<'a, $($t,)* > LensView<&'a ( $($t,)* )> for _1Inner {
type Field = &'a T; type Field = &'a T;
fn view(( $($v,)* ): &'a ($($t,)*)) -> Self::Field { fn view(&self, ( $($v,)* ): &'a ($($t,)*)) -> Self::Field {
$f $f
} }
} }
impl<'a, $($t,)* > LensView<&'a mut ( $($t,)* )> for _1Inner { impl<'a, $($t,)* > LensView<&'a mut ( $($t,)* )> for _1Inner {
type Field = &'a mut T; type Field = &'a mut T;
fn view(( $($v,)* ): &'a mut ($($t,)*)) -> Self::Field { fn view(&self, ( $($v,)* ): &'a mut ($($t,)*)) -> Self::Field {
$f $f
} }
} }
@ -60,12 +61,13 @@ macro_rules! make_arrays {
impl<T> LensView<[T; $n]> for _1Inner { impl<T> LensView<[T; $n]> for _1Inner {
type Field = T; type Field = T;
fn view([ $($v,)* ]: [T; $n]) -> Self::Field { fn view(&self, [ $($v,)* ]: [T; $n]) -> Self::Field {
$f $f
} }
} }
impl<T> LensOver<[T; $n]> for _1Inner { impl<T> LensOver<[T; $n]> for _1Inner {
fn over<F>( fn over<F>(
&self,
tup: [T; $n], tup: [T; $n],
fun: F fun: F
) -> [T; $n] ) -> [T; $n]
@ -81,14 +83,14 @@ macro_rules! make_arrays {
impl<'a, T> LensView<&'a [T; $n]> for _1Inner { impl<'a, T> LensView<&'a [T; $n]> for _1Inner {
type Field = &'a T; type Field = &'a T;
fn view([ $($v,)* ]: &'a [T; $n]) -> Self::Field { fn view(&self, [ $($v,)* ]: &'a [T; $n]) -> Self::Field {
$f $f
} }
} }
impl<'a, T> LensView<&'a mut [T; $n]> for _1Inner { impl<'a, T> LensView<&'a mut [T; $n]> for _1Inner {
type Field = &'a mut T; type Field = &'a mut T;
fn view([ $($v,)* ]: &'a mut [T; $n]) -> Self::Field { fn view(&self, [ $($v,)* ]: &'a mut [T; $n]) -> Self::Field {
$f $f
} }
} }

View File

@ -1,15 +1,5 @@
#![feature(unboxed_closures, fn_traits)] #![feature(unboxed_closures, fn_traits)]
// TODO add third_from_tuple, etc
// TODO array traversals
// TODO something for structs
// TODO something for Ok, Some, etc
// TODO make over work with changing types
/// Base trait /// Base trait
pub trait OpticsTrait {} pub trait OpticsTrait {}