Lens

Trait Lens 

Source
pub trait Lens<S, A>: HasGetter<S, A, GetterError = Infallible> + HasSetter<S, A> { }
Expand description

An optic for focusing on a value that is guaranteed to exist within a larger structure.

A Lens is appropriate for product types (e.g., structs) where the focus is always present. Unlike a [Prism], a Lens cannot fail to retrieve its focus — hence its associated [Optic::Error] type is fixed to Infallible.

It can also act as a [Prism] for compatibility in compositions.

§See Also

  • [Optic] — base trait implemented by all optics
  • [Prism] — optional focus optic for sum types
  • [Iso] — reversible transformations
  • [FallibleIso] — reversible transformations with fallible forward mapping

A Lens is an optic that focuses on a value that is guaranteed to exist within a larger structure.

A Lens is appropriate for product types (e.g., structs) where the focus is always present. Unlike a [Prism], a Lens cannot fail to retrieve its focus — hence its associated [Getter::GetterError] type is fixed to Infallible.

It provides:

  • get to extract a focused value from a larger type
  • set to set the focused value of a larger type

Type Arguments

  • S: The data type the optic operates on
  • A: The data type the optic focuses on

§Note

This is a marker trait that is blanket implemented for all structs that satisfy the requirements.

§See Also

  • [Getter] — an optic that focuses on value that is guaranteed to exist in a larger type
  • [Setter] — an optic that can change its focused value
  • [Iso] — an isomorphism optic representing a reversible bijective conversion between two types

Implementors§

Source§

impl<S, A, L: HasGetter<S, A, GetterError = Infallible> + HasSetter<S, A>> Lens<S, A> for L