Home > front end >  Display all traits for a variable
Display all traits for a variable

Time:10-14

I am looking for tools to make debugging easier with Rust. One tool that would be very useful is one that showed all functions for all traits for a given type. Ideally, this would be a macro or function that I could insert in my code to assist with debugging. In other words, at run-time (assuming debug symbols are present), given a variable, I want to display everything about it: it's attributes, value, traits along with a list of all functions for each trait, etc...

I would like a macro or function that takes any Rust type and returns a string with a prettified tree of traits and all function names for each trait.

Using code from: https://doc.rust-lang.org/rust-by-example/trait.html

let mut dolly: Sheep = Animal::new("Dolly");

show_all_traits(dolly);

should display:

Animal
    fn new(name: &'static str) -> Self;
    fn name(&self) -> &'static str;
    fn noise(&self) -> &'static str;
    fn talk(&self);
Sheep
    fn is_naked(&self) -> bool;
    fn shear(&mut self);

CodePudding user response:

This isn't (and probably couldn't without compiler intrinsic) possible. However documentation generated by cargo includes sections Trait Implementations, Auto Trait Implementations and Blanket Implementations that will list all traits implemented for given type.

  • Related