diff --git a/src/combinations.rs b/src/combinations.rs index 7d815b9..fe6411b 100644 --- a/src/combinations.rs +++ b/src/combinations.rs @@ -1,6 +1,5 @@ use crate::{ lenses::{LensOver, LensView}, - prisms::PrismPreview, Optics, OpticsTrait, }; @@ -27,8 +26,8 @@ where { type Field = B::Field; - fn view(thing: T) -> Self::Field { - B::view(A::view(thing)) + fn view(&self, thing: T) -> Self::Field { + B::view(&self.1, A::view(&self.0, thing)) } } @@ -37,22 +36,10 @@ where A: LensOver, B: LensOver, { - fn over(thing: T, f: F) -> T + fn over(&self, thing: T, f: F) -> T where F: FnOnce(Self::Field) -> Self::Field, { - A::over(thing, |b| B::over(b, f)) - } -} - -impl PrismPreview for Combination -where - A: LensView, - B: PrismPreview, -{ - type Field = B::Field; - - fn preview(thing: T) -> Option { - B::preview(A::view(thing)) + A::over(&self.0, thing, |b| B::over(&self.1, b, f)) } } diff --git a/src/fns.rs b/src/fns.rs index 38055e3..a7474ee 100644 --- a/src/fns.rs +++ b/src/fns.rs @@ -10,7 +10,7 @@ where type Output = L::Field; extern "rust-call" fn call_once(self, args: (A,)) -> Self::Output { - L::view(args.0) + L::view(&self.0, args.0) } } impl std::ops::FnMut<(A,)> for Optics @@ -18,7 +18,7 @@ where L: LensView, { extern "rust-call" fn call_mut(&mut self, args: (A,)) -> Self::Output { - L::view(args.0) + L::view(&self.0, args.0) } } impl std::ops::Fn<(A,)> for Optics @@ -26,7 +26,7 @@ where L: LensView, { 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; 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 std::ops::FnMut<(A, F)> for Optics @@ -47,7 +47,7 @@ where F: FnOnce(L::Field) -> L::Field, { 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 std::ops::Fn<(A, F)> for Optics @@ -56,6 +56,6 @@ where F: FnOnce(L::Field) -> L::Field, { 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) } } diff --git a/src/lenses/first.rs b/src/lenses/first.rs index 3f5b02f..1358f71 100644 --- a/src/lenses/first.rs +++ b/src/lenses/first.rs @@ -13,12 +13,13 @@ macro_rules! make_tuples { impl< $($t,)* > LensView<( $($t,)* )> for _0Inner { type Field = T; - fn view(( $($v,)* ): ($($t,)*)) -> Self::Field { + fn view(&self, ( $($v,)* ): ($($t,)*)) -> Self::Field { $f } } - impl< $($t,)* > LensOver<( $($t,)* )> for _0Inner { + impl< $($t,)* > LensOver<( $($t,)* )> for _0Inner { fn over( + &self, mut tup: ($($t,)*), f: F ) -> ( $($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; - fn view(( $($v,)* ): &'a ($($t,)*)) -> Self::Field { + fn view(&self, ( $($v,)* ): &'a ($($t,)*)) -> Self::Field { $f } } - impl<'a, $($t,)* > LensView<&'a mut ( $($t,)* )> for _0Inner { + impl<'a, $($t,)* > LensView<&'a mut ( $($t,)* )> for _0Inner { type Field = &'a mut T; - fn view(( $($v,)* ): &'a mut ($($t,)*)) -> Self::Field { + fn view(&self, ( $($v,)* ): &'a mut ($($t,)*)) -> Self::Field { $f } } @@ -60,12 +61,13 @@ macro_rules! make_arrays { impl LensView<[T; $n]> for _0Inner { type Field = T; - fn view([ $($v,)* ]: [T; $n]) -> Self::Field { + fn view(&self, [ $($v,)* ]: [T; $n]) -> Self::Field { $f } } impl LensOver<[T; $n]> for _0Inner { fn over( + &self, tup: [T; $n], fun: F ) -> [T; $n] @@ -81,14 +83,14 @@ macro_rules! make_arrays { impl<'a, T> LensView<&'a [T; $n]> for _0Inner { type Field = &'a T; - fn view([ $($v,)* ]: &'a [T; $n]) -> Self::Field { + fn view(&self, [ $($v,)* ]: &'a [T; $n]) -> Self::Field { $f } } impl<'a, T> LensView<&'a mut [T; $n]> for _0Inner { 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 } } diff --git a/src/lenses/identity.rs b/src/lenses/identity.rs index 1d49fc0..4af9104 100644 --- a/src/lenses/identity.rs +++ b/src/lenses/identity.rs @@ -11,12 +11,12 @@ impl OpticsTrait for id {} impl LensView for id { type Field = T; - fn view(thing: T) -> Self::Field { + fn view(&self, thing: T) -> Self::Field { thing } } impl LensOver for id { - fn over(thing: T, f: F) -> T + fn over(&self, thing: T, f: F) -> T where F: FnOnce(Self::Field) -> Self::Field, { diff --git a/src/lenses/mod.rs b/src/lenses/mod.rs index bb2152d..9270b71 100644 --- a/src/lenses/mod.rs +++ b/src/lenses/mod.rs @@ -6,23 +6,25 @@ pub use first::_0; mod second; pub use second::_1; +mod to; + use crate::{Optics, OpticsTrait}; /// For lenses that allow viewing pub trait LensView: OpticsTrait { type Field; - fn view(thing: T) -> Self::Field; + fn view(&self, thing: T) -> Self::Field; } /// For lenses that allow setting pub trait LensOver: LensView { - fn over(thing: T, f: F) -> T + fn over(&self, thing: T, f: F) -> T where F: FnOnce(Self::Field) -> Self::Field; - fn set(thing: T, v: Self::Field) -> T { - Self::over(thing, |_| v) + fn set(&self, thing: T, v: Self::Field) -> T { + Self::over(self, thing, |_| v) } } @@ -32,30 +34,30 @@ where { type Field = L::Field; - fn view(thing: T) -> Self::Field { - L::view(thing) + fn view(&self, thing: T) -> Self::Field { + L::view(&self.0, thing) } } impl LensOver for Optics where L: LensOver, { - fn over(thing: T, f: F) -> T + fn over(&self, thing: T, f: F) -> T where F: FnOnce(Self::Field) -> Self::Field, { - L::over(thing, f) + L::over(&self.0, thing, f) } } -pub fn view>(_lens: L, thing: T) -> L::Field { - L::view(thing) +pub fn view>(lens: L, thing: T) -> L::Field { + L::view(&lens, thing) } -pub fn set>(_lens: L, thing: T, v: L::Field) -> T { - L::set(thing, v) +pub fn set>(lens: L, thing: T, v: L::Field) -> T { + L::set(&lens, thing, v) } -pub fn over>(_lens: L, thing: T, f: impl FnOnce(L::Field) -> L::Field) -> T { - L::over(thing, f) +pub fn over>(lens: L, thing: T, f: impl FnOnce(L::Field) -> L::Field) -> T { + L::over(&lens, thing, f) } #[cfg(test)] diff --git a/src/lenses/second.rs b/src/lenses/second.rs index d2c0d4f..b66a6d6 100644 --- a/src/lenses/second.rs +++ b/src/lenses/second.rs @@ -13,12 +13,13 @@ macro_rules! make_tuples { impl< $($t,)* > LensView<( $($t,)* )> for _1Inner { type Field = T; - fn view(( $($v,)* ): ($($t,)*)) -> Self::Field { + fn view(&self, ( $($v,)* ): ($($t,)*)) -> Self::Field { $f } } impl< $($t,)* > LensOver<( $($t,)* )> for _1Inner { fn over( + &self, mut tup: ($($t,)*), f: F ) -> ( $($t,)* ) @@ -33,14 +34,14 @@ macro_rules! make_tuples { impl<'a, $($t,)* > LensView<&'a ( $($t,)* )> for _1Inner { type Field = &'a T; - fn view(( $($v,)* ): &'a ($($t,)*)) -> Self::Field { + fn view(&self, ( $($v,)* ): &'a ($($t,)*)) -> Self::Field { $f } } impl<'a, $($t,)* > LensView<&'a mut ( $($t,)* )> for _1Inner { type Field = &'a mut T; - fn view(( $($v,)* ): &'a mut ($($t,)*)) -> Self::Field { + fn view(&self, ( $($v,)* ): &'a mut ($($t,)*)) -> Self::Field { $f } } @@ -60,12 +61,13 @@ macro_rules! make_arrays { impl LensView<[T; $n]> for _1Inner { type Field = T; - fn view([ $($v,)* ]: [T; $n]) -> Self::Field { + fn view(&self, [ $($v,)* ]: [T; $n]) -> Self::Field { $f } } impl LensOver<[T; $n]> for _1Inner { fn over( + &self, tup: [T; $n], fun: F ) -> [T; $n] @@ -81,14 +83,14 @@ macro_rules! make_arrays { impl<'a, T> LensView<&'a [T; $n]> for _1Inner { type Field = &'a T; - fn view([ $($v,)* ]: &'a [T; $n]) -> Self::Field { + fn view(&self, [ $($v,)* ]: &'a [T; $n]) -> Self::Field { $f } } impl<'a, T> LensView<&'a mut [T; $n]> for _1Inner { 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 } } diff --git a/src/lib.rs b/src/lib.rs index 037cad1..039b4d7 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,15 +1,5 @@ #![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 pub trait OpticsTrait {}