Skip to main content

Akp

Derive Macro Akp 

Source
#[derive(Akp)]
Expand description

Derive macro that generates any_kps() -> Vec<AKp> returning all field/variant keypaths as any keypaths. Requires #[derive(Kp)] so the keypath accessor methods exist. AKp type-erases both Root and Value, enabling heterogeneous collections of keypaths.

For structs: returns keypaths for each field. For enums: returns keypaths for each variant (using the same methods Kp generates, e.g. some_variant()).

ยงExample

use key_paths_derive::{Kp, Akp};
use rust_key_paths::AKp;

#[derive(Kp, Akp)]
struct Person {
    name: String,
    age: i32,
}

let kps = Person::any_kps();
assert_eq!(kps.len(), 2);
let person = Person { name: "Alice".into(), age: 30 };
let name: Option<&String> = kps[0].get(&person as &dyn std::any::Any).and_then(|v| v.downcast_ref());
assert_eq!(name, Some(&"Alice".to_string()));