Trait druid::Lens

source ·
pub trait Lens<T: ?Sized, U: ?Sized> {
    // Required methods
    fn with<V, F: FnOnce(&U) -> V>(&self, data: &T, f: F) -> V;
    fn with_mut<V, F: FnOnce(&mut U) -> V>(&self, data: &mut T, f: F) -> V;
}
Expand description

A lens is a datatype that gives access to a part of a larger data structure.

A simple example of a lens is a field of a struct; in this case, the lens itself is zero-sized. Another case is accessing an array element, in which case the lens contains the array index.

The most common way to create Lens implementations is to use #[derive(Lens)] to access a struct’s fields, but custom implementations are practical as well.

The name “lens” is inspired by the Haskell lens package, which has generally similar goals. It’s likely we’ll develop more sophistication, for example combinators to combine lenses.

Required Methods§

source

fn with<V, F: FnOnce(&U) -> V>(&self, data: &T, f: F) -> V

Get non-mut access to the field.

Runs the supplied closure with a reference to the data. It’s structured this way, as opposed to simply returning a reference, so that the data might be synthesized on-the-fly by the lens.

source

fn with_mut<V, F: FnOnce(&mut U) -> V>(&self, data: &mut T, f: F) -> V

Get mutable access to the field.

This method is defined in terms of a closure, rather than simply yielding a mutable reference, because it is intended to be used with value-type data (also known as immutable data structures). For example, a lens for an immutable list might be implemented by cloning the list, giving the closure mutable access to the clone, then updating the reference after the closure returns.

Implementations on Foreign Types§

source§

impl<A, L0, L1, L0B, L1B> Lens<A, (L0B, L1B)> for (L0, L1)where L0B: Clone, L1B: Clone, L0: Lens<A, L0B>, L1: Lens<A, L1B>,

source§

fn with<V, F: FnOnce(&(L0B, L1B)) -> V>(&self, data: &A, f: F) -> V

source§

fn with_mut<V, F: FnOnce(&mut (L0B, L1B)) -> V>(&self, data: &mut A, f: F) -> V

source§

impl<A, L0, L1, L2, L3, L4, L5, L6, L7, L0B, L1B, L2B, L3B, L4B, L5B, L6B, L7B> Lens<A, (L0B, L1B, L2B, L3B, L4B, L5B, L6B, L7B)> for (L0, L1, L2, L3, L4, L5, L6, L7)where L0B: Clone, L1B: Clone, L2B: Clone, L3B: Clone, L4B: Clone, L5B: Clone, L6B: Clone, L7B: Clone, L0: Lens<A, L0B>, L1: Lens<A, L1B>, L2: Lens<A, L2B>, L3: Lens<A, L3B>, L4: Lens<A, L4B>, L5: Lens<A, L5B>, L6: Lens<A, L6B>, L7: Lens<A, L7B>,

source§

fn with<V, F: FnOnce(&(L0B, L1B, L2B, L3B, L4B, L5B, L6B, L7B)) -> V>( &self, data: &A, f: F ) -> V

source§

fn with_mut<V, F: FnOnce(&mut (L0B, L1B, L2B, L3B, L4B, L5B, L6B, L7B)) -> V>( &self, data: &mut A, f: F ) -> V

source§

impl<A, L0, L1, L2, L3, L4, L5, L6, L0B, L1B, L2B, L3B, L4B, L5B, L6B> Lens<A, (L0B, L1B, L2B, L3B, L4B, L5B, L6B)> for (L0, L1, L2, L3, L4, L5, L6)where L0B: Clone, L1B: Clone, L2B: Clone, L3B: Clone, L4B: Clone, L5B: Clone, L6B: Clone, L0: Lens<A, L0B>, L1: Lens<A, L1B>, L2: Lens<A, L2B>, L3: Lens<A, L3B>, L4: Lens<A, L4B>, L5: Lens<A, L5B>, L6: Lens<A, L6B>,

source§

fn with<V, F: FnOnce(&(L0B, L1B, L2B, L3B, L4B, L5B, L6B)) -> V>( &self, data: &A, f: F ) -> V

source§

fn with_mut<V, F: FnOnce(&mut (L0B, L1B, L2B, L3B, L4B, L5B, L6B)) -> V>( &self, data: &mut A, f: F ) -> V

source§

impl<A, L0, L1, L2, L3, L4, L0B, L1B, L2B, L3B, L4B> Lens<A, (L0B, L1B, L2B, L3B, L4B)> for (L0, L1, L2, L3, L4)where L0B: Clone, L1B: Clone, L2B: Clone, L3B: Clone, L4B: Clone, L0: Lens<A, L0B>, L1: Lens<A, L1B>, L2: Lens<A, L2B>, L3: Lens<A, L3B>, L4: Lens<A, L4B>,

source§

fn with<V, F: FnOnce(&(L0B, L1B, L2B, L3B, L4B)) -> V>(&self, data: &A, f: F) -> V

source§

fn with_mut<V, F: FnOnce(&mut (L0B, L1B, L2B, L3B, L4B)) -> V>( &self, data: &mut A, f: F ) -> V

source§

impl<A, L0, L1, L2, L0B, L1B, L2B> Lens<A, (L0B, L1B, L2B)> for (L0, L1, L2)where L0B: Clone, L1B: Clone, L2B: Clone, L0: Lens<A, L0B>, L1: Lens<A, L1B>, L2: Lens<A, L2B>,

source§

fn with<V, F: FnOnce(&(L0B, L1B, L2B)) -> V>(&self, data: &A, f: F) -> V

source§

fn with_mut<V, F: FnOnce(&mut (L0B, L1B, L2B)) -> V>( &self, data: &mut A, f: F ) -> V

source§

impl<A, L0, L1, L2, L3, L4, L5, L0B, L1B, L2B, L3B, L4B, L5B> Lens<A, (L0B, L1B, L2B, L3B, L4B, L5B)> for (L0, L1, L2, L3, L4, L5)where L0B: Clone, L1B: Clone, L2B: Clone, L3B: Clone, L4B: Clone, L5B: Clone, L0: Lens<A, L0B>, L1: Lens<A, L1B>, L2: Lens<A, L2B>, L3: Lens<A, L3B>, L4: Lens<A, L4B>, L5: Lens<A, L5B>,

source§

fn with<V, F: FnOnce(&(L0B, L1B, L2B, L3B, L4B, L5B)) -> V>( &self, data: &A, f: F ) -> V

source§

fn with_mut<V, F: FnOnce(&mut (L0B, L1B, L2B, L3B, L4B, L5B)) -> V>( &self, data: &mut A, f: F ) -> V

source§

impl<A, L0, L1, L2, L3, L0B, L1B, L2B, L3B> Lens<A, (L0B, L1B, L2B, L3B)> for (L0, L1, L2, L3)where L0B: Clone, L1B: Clone, L2B: Clone, L3B: Clone, L0: Lens<A, L0B>, L1: Lens<A, L1B>, L2: Lens<A, L2B>, L3: Lens<A, L3B>,

source§

fn with<V, F: FnOnce(&(L0B, L1B, L2B, L3B)) -> V>(&self, data: &A, f: F) -> V

source§

fn with_mut<V, F: FnOnce(&mut (L0B, L1B, L2B, L3B)) -> V>( &self, data: &mut A, f: F ) -> V

Implementors§

source§

impl<A, B, L> Lens<Arc<A>, B> for InArc<L>where A: Clone, B: Data, L: Lens<A, B>,

source§

impl<A, B: Clone> Lens<A, B> for Constant<B>

source§

impl<A: ?Sized> Lens<A, A> for Identity

source§

impl<A: ?Sized, B, Get, Put> Lens<A, B> for Map<Get, Put>where Get: Fn(&A) -> B, Put: Fn(&mut A, B),

source§

impl<T> Lens<T, <T as Deref>::Target> for Derefwhere T: Deref + DerefMut + ?Sized,

source§

impl<T> Lens<T, ()> for Unit

source§

impl<T, I> Lens<T, <T as Index<I>>::Output> for Index<I>where T: ?Sized + Index<I> + IndexMut<I>, I: Clone,

source§

impl<T, U, A, B, C> Lens<A, C> for Then<T, U, B>where A: ?Sized, B: ?Sized, C: ?Sized, T: Lens<A, B>, U: Lens<B, C>,

source§

impl<T, U, Get, GetMut> Lens<T, U> for Field<Get, GetMut>where T: ?Sized, U: ?Sized, Get: Fn(&T) -> &U, GetMut: Fn(&mut T) -> &mut U,

source§

impl<T, U: ?Sized> Lens<T, U> for Refwhere T: AsRef<U> + AsMut<U> + ?Sized,