change over to not take a ref, rename id to identity

main
annieversary 2021-11-05 12:05:55 +00:00
parent 7bac8ee9ec
commit d76541fbc5
4 changed files with 17 additions and 9 deletions

View File

@ -12,10 +12,13 @@ macro_rules! make_tuples {
}
}
impl< $($t,)* > LensOver<( $($t,)* )> for _0 {
fn over(
fn over<F>(
mut tup: ($($t,)*),
f: &dyn Fn(Self::Field) -> Self::Field
) -> ( $($t,)* ) {
f: F
) -> ( $($t,)* )
where
F: FnOnce(Self::Field) -> Self::Field
{
tup.0 = f(tup.0);
tup
}

View File

@ -11,7 +11,10 @@ impl<T> LensView<T> for T {
}
}
impl<T> LensOver<T> for T {
fn over(thing: T, f: &dyn Fn(Self::Field) -> Self::Field) -> T {
fn over<F>(thing: T, f: F) -> T
where
F: FnOnce(Self::Field) -> Self::Field,
{
f(thing)
}
}

View File

@ -1,4 +1,4 @@
mod id;
pub use id::id;
mod identity;
pub use identity::id;
mod first;
pub use first::_0;

View File

@ -5,10 +5,12 @@ pub trait LensView<T> {
}
pub trait LensOver<T>: LensView<T> {
fn over(thing: T, f: &dyn Fn(Self::Field) -> Self::Field) -> T;
fn over<F>(thing: T, f: F) -> T
where
F: FnOnce(Self::Field) -> Self::Field;
fn set(thing: T, v: Self::Field) -> T {
Self::over(thing, &|_| v)
Self::over(thing, |_| v)
}
}
@ -18,7 +20,7 @@ pub fn view<T, L: LensView<T>>(_lens: L, thing: T) -> L::Field {
pub fn set<T, L: LensOver<T>>(_lens: L, thing: T, v: L::Field) -> T {
L::set(thing, v)
}
pub fn over<T, L: LensOver<T>>(_lens: L, thing: T, f: &dyn Fn(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)
}