Function contrafact::lens [−][src]
pub fn lens<O, T, F, L, S>(
label: S,
lens: L,
inner_fact: F
) -> LensFact<O, T, F> where
O: Bounds,
T: Bounds,
S: ToString,
F: Fact<T>,
L: 'static + Fn(&mut O) -> &mut T,
Expand description
Lifts a Fact about a subset of some data into a Fact about the superset.
In other words, if type O
contains a T
, and you have a Fact<T>
,
LensFact
lets you lift that fact into a Fact<O>
.
The lens
closure provides a mutable view into the subset of data.
There must be a way to specify a mutable reference to the subset of data.
If this is not always possible, consider using prism()
instead.
This is a lazy way to provide a lens in the traditional optics sense. We may consider using a true lens library for this in the future.
use contrafact::*; use arbitrary::*; #[derive(Debug, Clone, PartialEq, Arbitrary)] struct S { x: u32, y: u32, } let mut fact = lens("S::x", |s: &mut S| &mut s.x, eq("must be 1", &1)); assert!(fact.check(&S {x: 1, y: 333}).is_ok()); assert!(fact.check(&S {x: 2, y: 333}).is_err()); let mut u = Unstructured::new(&[0; 9999]); let a = fact.build(&mut u); assert_eq!(a.x, 1);