Macro im::lens
[−]
[src]
macro_rules! lens { ( $from:ident : $headpath:ident : $headto:ident : $($tail:tt):* ) => { ... }; ( $from:ident : $path:ident : $to:ident ) => { ... }; }
Construct a lens into a struct, or a series of structs.
You'll need to specify the type of the source struct, the
name of the field you want a lens into, and the type of that
field, separated by colons, eg.
lens!(MyStruct: string_field: String)
.
You can keep repeating name/type pairs for fields inside structs
inside structs.
Please note: this only works on fields which are wrapped in an
Arc
(so the type of the string_field
field in the example in
the previous paragraph must be Arc<String
>), and the source
struct must implement Clone
.
Examples
#[derive(Clone)] struct Name { first: Arc<String>, last: Arc<String> } #[derive(Clone)] struct Person { name: Arc<Name> } let person_last_name = lens!(Person: name: Name: last: String); let the_admiral = Person { name: Arc::new(Name { first: Arc::new("Grace".to_string()), last: Arc::new("Hopper".to_string()) }) }; assert_eq!( Arc::new("Hopper".to_string()), person_last_name.get(&the_admiral) );