Skip to main content

Crate kanshou_derive

Crate kanshou_derive 

Source
Expand description

#[derive(Introspect)] — auto-implements kanshou::Introspect for a struct whose pub fields are each independently queryable.

§Default behavior

  • Every pub named field becomes a top-level path leaf. A query Query { path: ["field_name"] } returns serde_json::to_value(&self.field_name).
  • Tuple structs, unit structs, and enums are unsupported (compile error). Consumers with non-struct shapes hand-write Introspect.

§Field attributes

Per-field #[introspect(...)] modifies the leaf shape:

  • #[introspect(skip)] — exclude from the query surface. Useful for internal channels, abort handles, or anything the operator shouldn’t see.
  • #[introspect(load)] — call .load(Ordering::Relaxed) before serializing. For AtomicU64/AtomicUsize/etc. so the wire shape is a number, not the atomic struct’s debug print.
  • #[introspect(nested)] — the field itself implements Introspect; nested path elements walk into it. Without this attribute, query on ["field_name", "subfield"] returns QueryError::UnknownField. With it, the rest of the path recurses.
  • #[introspect(name = "wire_name")] — expose the field under a different name on the wire than the Rust identifier. Mirrors #[serde(rename = "...")]. Lets consumer apps stabilize a public API even when the internal field shape evolves.

§Example

use std::sync::atomic::AtomicU64;
use kanshou::Introspect;

#[derive(Introspect)]
pub struct AppState {
    pub sessions: Vec<String>,
    #[introspect(load)]
    pub frame_count: AtomicU64,
    #[introspect(nested)]
    pub config: Config,
    #[introspect(skip)]
    internal: tokio::sync::mpsc::Sender<()>,
}

#[derive(Introspect)]
pub struct Config {
    pub shell: String,
    pub width: u32,
}

Derive Macros§

Introspect
Auto-implement kanshou::Introspect for a struct with named pub fields. See module docs for attribute reference.