inspect-rs 0.1.0

Universal introspection for Rust
Documentation
use inspect_rs::{Inspect, InspectCx, Kind, TypeInfo, ValueRef};

// Custom type that doesn't expose internals
struct OpaqueHandle {
    #[allow(dead_code)]
    raw: usize,
}

impl Inspect for OpaqueHandle {
    fn inspect(&self, _cx: &mut InspectCx<'_>) -> ValueRef<'_> {
        ValueRef::with_type(Kind::Opaque, TypeInfo::new("OpaqueHandle"))
    }
}

// Custom type with special representation
struct Percentage(f64);

impl Inspect for Percentage {
    fn inspect(&self, _cx: &mut InspectCx<'_>) -> ValueRef<'_> {
        ValueRef::with_type(Kind::F64(self.0), TypeInfo::new("Percentage"))
    }
}

#[derive(Inspect)]
struct System {
    handle: OpaqueHandle,
    cpu_usage: Percentage,
}

fn main() {
    let system = System { handle: OpaqueHandle { raw: 0xdeadbeef }, cpu_usage: Percentage(78.5) };

    let mut cx = InspectCx::new();
    let inspected = system.inspect(&mut cx);

    println!("{}", inspect_rs::format_tree(&inspected));
}