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::{
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<T>,
B: LensOver<A::Field>,
{
fn over<F>(thing: T, f: F) -> T
fn over<F>(&self, thing: T, f: F) -> T
where
F: FnOnce(Self::Field) -> Self::Field,
{
A::over(thing, |b| B::over(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))
A::over(&self.0, thing, |b| B::over(&self.1, b, f))
}
}

View File

@ -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<L, A> std::ops::FnMut<(A,)> for Optics<L>
@ -18,7 +18,7 @@ where
L: LensView<A>,
{
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>
@ -26,7 +26,7 @@ where
L: LensView<A>,
{
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<L, A, F> std::ops::FnMut<(A, F)> for Optics<L>
@ -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<L, A, F> std::ops::Fn<(A, F)> for Optics<L>
@ -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)
}
}

View File

@ -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<F>(
&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<T> 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<T> LensOver<[T; $n]> for _0Inner {
fn over<F>(
&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
}
}

View File

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

View File

@ -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<T>: OpticsTrait {
type Field;
fn view(thing: T) -> Self::Field;
fn view(&self, thing: T) -> Self::Field;
}
/// For lenses that allow setting
pub trait LensOver<T>: LensView<T> {
fn over<F>(thing: T, f: F) -> T
fn over<F>(&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<L, T> LensOver<T> for Optics<L>
where
L: LensOver<T>,
{
fn over<F>(thing: T, f: F) -> T
fn over<F>(&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<T, L: LensView<T>>(_lens: L, thing: T) -> L::Field {
L::view(thing)
pub fn view<T, L: LensView<T>>(lens: L, thing: T) -> L::Field {
L::view(&lens, thing)
}
pub fn set<T, L: LensOver<T>>(_lens: L, thing: T, v: L::Field) -> T {
L::set(thing, v)
pub fn set<T, L: LensOver<T>>(lens: L, thing: T, v: L::Field) -> T {
L::set(&lens, thing, v)
}
pub fn over<T, L: LensOver<T>>(_lens: L, thing: T, f: impl FnOnce(L::Field) -> L::Field) -> T {
L::over(thing, f)
pub fn over<T, L: LensOver<T>>(lens: L, thing: T, f: impl FnOnce(L::Field) -> L::Field) -> T {
L::over(&lens, thing, f)
}
#[cfg(test)]

View File

@ -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<F>(
&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<T> 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<T> LensOver<[T; $n]> for _1Inner {
fn over<F>(
&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
}
}

View File

@ -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 {}