[][src]Trait druid::Lens

pub trait Lens<T, U> {
    fn get<'a>(&self, data: &'a T) -> &'a U;
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

fn get<'a>(&self, data: &'a T) -> &'a U

Get non-mut access to the field.

Discussion question: using a closure as in the with_mut method may improve flexibility (for example, being able to synthesize derived data on the fly). Is this flexibility worth the increased complexity?

If so, the signature of the Lens trait may change.

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.

Loading content...

Implementors

Loading content...