refraction/
lens.rs

1use super::{Compose, Identity, Invert, Iso, PartialLens};
2
3/// The supertype of all lens families.
4pub trait Lens: PartialLens
5    where Self::AtInitial: Lens,
6          Self::AtFinal: Lens
7{
8    fn get(&self, v: Self::InitialSource) -> Self::InitialTarget;
9}
10
11impl<S, T> Lens for Identity<S, T> {
12    #[inline]
13    fn get(&self, v: S) -> S {
14        v
15    }
16}
17
18impl<LF: Lens, LS: ?Sized> Lens for Compose<LF, LS>
19    where LS: Lens<InitialTarget = LF::InitialSource, FinalTarget = LF::FinalSource>,
20          LF::AtInitial: Lens,
21          LF::AtFinal: Lens,
22          LS::AtInitial: Lens,
23          LS::AtFinal: Lens
24{
25    fn get(&self, v: Self::InitialSource) -> Self::InitialTarget {
26        self.first.get(self.second.get(v))
27    }
28}
29
30impl<L: Iso> Lens for Invert<L>
31    where L::AtInitial: Iso,
32          L::AtFinal: Iso
33{
34    #[inline]
35    fn get(&self, v: Self::InitialSource) -> Self::InitialTarget {
36        self.deinvert.inject(v)
37    }
38}