bad-optics/src/combinations.rs

38 lines
820 B
Rust
Raw Normal View History

use crate::lenses::{Lens, LensOver, LensView};
2021-11-05 13:27:34 +00:00
2021-11-05 14:22:59 +00:00
#[derive(Clone, Copy)]
2021-11-05 13:27:34 +00:00
pub struct Combination<A, B>(A, B);
impl<A, B> std::ops::Add<Lens<B>> for Lens<A> {
type Output = Lens<Combination<Lens<A>, Lens<B>>>;
2021-11-05 13:27:34 +00:00
fn add(self, rhs: Lens<B>) -> Self::Output {
Lens(Combination(self, rhs))
2021-11-05 13:27:34 +00:00
}
}
impl<A, B, T> LensView<T> for Combination<A, B>
where
A: LensView<T>,
B: LensView<A::Field>,
{
type Field = B::Field;
2021-11-11 10:39:47 +00:00
fn view(&self, thing: T) -> Self::Field {
B::view(&self.1, A::view(&self.0, thing))
2021-11-05 13:27:34 +00:00
}
}
impl<A, B, T> LensOver<T> for Combination<A, B>
where
A: LensOver<T>,
B: LensOver<A::Field>,
{
2021-11-11 10:39:47 +00:00
fn over<F>(&self, thing: T, f: F) -> T
2021-11-05 13:27:34 +00:00
where
F: FnOnce(Self::Field) -> Self::Field,
{
2021-11-11 10:39:47 +00:00
A::over(&self.0, thing, |b| B::over(&self.1, b, f))
2021-11-05 19:35:26 +00:00
}
}