[][src]Trait druid::Lens

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

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.

Many Lens implementations will be derived by macro, 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

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

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.

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

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.

Loading content...

Implementors

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

impl<A, B: Clone> Lens<A, B> for Constant<B>[src]

impl<A: ?Sized> Lens<A, A> for Identity[src]

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

impl<T> Lens<T, ()> for Unit[src]

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>, 
[src]

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, 
[src]

impl<T: ?Sized> Lens<T, <T as Deref>::Target> for Deref where
    T: Deref + DerefMut
[src]

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

impl<T: ?Sized, U: ?Sized> Lens<T, U> for Ref where
    T: AsRef<U> + AsMut<U>, 
[src]

Loading content...