I made a derive-less reflection library with the new type_info feature!
It uses reflection to recurse the ast at compile time allowing implementing Traits for any type, no derive needed! Slice, tuples, arrays, strings, floats, ints are supported for now!
Example usage:
pub trait ReflectionDebug {
fn dbg(&self);
}
impl<T: ReflectionRecursive> ReflectionDebug for T {
fn dbg(&self) {
dbg_impl(&self.get_ty_recursive());
println!("");
}
}
fn dbg_impl(ty: &TyKindMapped) {
match ty {
TyKindMapped::Tuple(tuple) => {
print!("(");
let mut first = true;
for val in tuple {
if !first {
print!(",");
}
first = false;
dbg_impl(val);
}
print!(")");
},
TyKindMapped::IntMapped(int) => {
print!("{int:?}");
}
_ => todo!(),
}
}
fn main() {
[1, 2, 3].dbg(); // prints [1, 2, 3]
}
// alternative, does not require importing any Trait on call size
pub fn dbg(ty: &impl ReflectionRecursive) {
dbg_impl(&ty.get_ty_recursive());
}