Skip to main content

Lens

Trait Lens 

Source
pub trait Lens<S, A> {
    // Required methods
    fn view(&self, whole: &S) -> A;
    fn set(&self, whole: &mut S, part: A);

    // Provided methods
    fn over(&self, whole: &mut S, f: impl FnOnce(A) -> A)
       where A: Clone { ... }
    fn then<B, L2: Lens<A, B>>(self, inner: L2) -> Composed<Self, L2, A>
       where Self: Sized,
             A: Clone { ... }
}
Expand description

A bidirectional lens focusing on part A of a whole S.

§Laws

A well-behaved lens satisfies:

  1. GetPut: lens.set(s, lens.view(s)) leaves s unchanged.
  2. PutGet: After lens.set(s, a), lens.view(s) returns a.

Required Methods§

Source

fn view(&self, whole: &S) -> A

View the focused part.

Source

fn set(&self, whole: &mut S, part: A)

Set the focused part, mutating the whole in place.

Provided Methods§

Source

fn over(&self, whole: &mut S, f: impl FnOnce(A) -> A)
where A: Clone,

Modify the focused part with a function.

Source

fn then<B, L2: Lens<A, B>>(self, inner: L2) -> Composed<Self, L2, A>
where Self: Sized, A: Clone,

Compose this lens with an inner lens to focus deeper.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§

Source§

impl<A: Clone, B: Clone> Lens<(A, B), A> for Fst

Source§

impl<A: Clone, B: Clone> Lens<(A, B), B> for Snd

Source§

impl<S, A, G, P> Lens<S, A> for FieldLens<G, P>
where G: Fn(&S) -> A, P: Fn(&mut S, A),

Source§

impl<S, B, A, L1, L2> Lens<S, A> for Composed<L1, L2, B>
where L1: Lens<S, B>, L2: Lens<B, A>, B: Clone,

Source§

impl<S: Clone> Lens<S, S> for Identity

Source§

impl<T: Clone> Lens<Vec<T>, T> for AtIndex