[−][src]Trait druid::LensExt
Helpers for manipulating Lens
es
Provided methods
fn get(&self, data: &A) -> B where
B: Clone,
B: Clone,
Copy the targeted value out of data
fn put(&self, data: &mut A, value: B) where
B: Sized,
B: Sized,
Set the targeted value in data
to value
fn then<Other, C>(self, other: Other) -> Then<Self, Other, B> where
Other: Lens<B, C> + Sized,
C: ?Sized,
Self: Sized,
Other: Lens<B, C> + Sized,
C: ?Sized,
Self: Sized,
Compose a Lens<A, B>
with a Lens<B, C>
to produce a Lens<A, C>
struct Foo { x: (u32, bool) } let lens = lens!(Foo, x).then(lens!((u32, bool), 1)); assert_eq!(lens.get(&Foo { x: (0, true) }), true);
fn map<Get, Put, C>(self, get: Get, put: Put) -> Then<Self, Map<Get, Put>, B> where
Get: Fn(&B) -> C,
Put: Fn(&mut B, C),
Self: Sized,
Get: Fn(&B) -> C,
Put: Fn(&mut B, C),
Self: Sized,
Combine a Lens<A, B>
with a function that can transform a B
and its inverse.
Useful for cases where the desired value doesn't physically exist in A
, but can be
computed. For example, a lens like the following might be used to adapt a value with the
range 0-2 for use with a Widget<f64>
like Slider
that has a range of 0-1:
let lens = lens!((bool, f64), 1); assert_eq!(lens.map(|x| x / 2.0, |x, y| *x = y * 2.0).get(&(true, 2.0)), 1.0);
The computed C
may represent a whole or only part of the original B
.
fn deref(self) -> Then<Self, Deref, B> where
B: Deref + DerefMut,
Self: Sized,
B: Deref + DerefMut,
Self: Sized,
Invoke a type's Deref
impl
assert_eq!(lens::Id.deref().get(&Box::new(42)), 42);
fn index<I>(self, index: I) -> Then<Self, Index<I>, B> where
I: Clone,
B: Index<I> + IndexMut<I>,
Self: Sized,
I: Clone,
B: Index<I> + IndexMut<I>,
Self: Sized,
Access an index in a container
assert_eq!(lens::Id.index(2).get(&vec![0u32, 1, 2, 3]), 2);
fn in_arc(self) -> InArc<Self> where
A: Clone,
B: Data,
Self: Sized,
A: Clone,
B: Data,
Self: Sized,
Adapt to operate on the contents of an Arc
with efficient copy-on-write semantics
let lens = lens::Id.index(2).in_arc(); let mut x = Arc::new(vec![0, 1, 2, 3]); let original = x.clone(); assert_eq!(lens.get(&x), 2); lens.put(&mut x, 2); assert!(Arc::ptr_eq(&original, &x), "no-op writes don't cause a deep copy"); lens.put(&mut x, 42); assert_eq!(&*x, &[0, 1, 42, 3]);