mod identity; pub use identity::id; mod first; pub use first::_0; mod second; pub use second::_1; use crate::{Optics, OpticsTrait}; /// For lenses that allow viewing pub trait LensView: OpticsTrait { type Field; fn view(thing: T) -> Self::Field; } /// For lenses that allow setting pub trait LensOver: LensView { fn over(thing: T, f: F) -> T where F: FnOnce(Self::Field) -> Self::Field; fn set(thing: T, v: Self::Field) -> T { Self::over(thing, |_| v) } } impl LensView for Optics where L: LensView, { type Field = L::Field; fn view(thing: T) -> Self::Field { L::view(thing) } } impl LensOver for Optics where L: LensOver, { fn over(thing: T, f: F) -> T where F: FnOnce(Self::Field) -> Self::Field, { L::over(thing, f) } } pub fn view>(_lens: L, thing: T) -> L::Field { L::view(thing) } pub fn set>(_lens: L, thing: T, v: L::Field) -> T { L::set(thing, v) } pub fn over>(_lens: L, thing: T, f: impl FnOnce(L::Field) -> L::Field) -> T { L::over(thing, f) }