opt_keypath

Macro opt_keypath 

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

Macro to create an OptionalKeyPath (readable, optional)

ยงExamples

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