writable_opt_keypath

Macro writable_opt_keypath 

Source
macro_rules! writable_opt_keypath {
    ($closure:expr) => { ... };
}
Expand description

Macro to create a WritableOptionalKeyPath (writable, optional)

ยงExamples

use rust_keypaths::writable_opt_keypath;
 
struct User { metadata: Option<String>, address: Option<Address> }
struct Address { street: String }
 
// Using a closure with type annotation
let kp = writable_opt_keypath!(|u: &mut User| u.metadata.as_mut());
 
// Nested field access through Option
let kp = writable_opt_keypath!(|u: &mut User| u.address.as_mut().map(|a| &mut a.street));
 
// Or with automatic type inference
let kp = writable_opt_keypath!(|u| u.metadata.as_mut());