It is considered good practice to #[derive(Debug)]
for most structs you create to aid in debugging. However, this is not possible if your struct contains a type without Debug
, such as traits. But if the trait is under my control, is there something I can do to let users' implementations of said trait show up in the debug message?
I could require that people who implement my trait also implement Debug
, but I don't like having to add that arbitrary requirement:
trait MyTrait: Debug { ... }
I could just implement Debug
for my trait:
trait MyTrait { ... }
impl Debug for MyTrait {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
write!(f, "MyTrait {{ ... }}")
}
}
This doesn't allow implementations to override Debug
- it's almost as if the function is not virtual. How can I make this work?
use std::fmt;
use std::fmt::{ Formatter, Debug };
#[derive(Debug)]
struct A {
a: Box<Data>,
}
trait Data {}
impl Debug for Data {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
write!(f, "Data{{ ... }}")
}
}
#[derive(Debug)]
struct B(i32);
impl Data for B {}
fn main() {
let a = A{ a: Box::new(B(42)) };
println!("{:?}", a);
}
Outputs:
A { a: Data{ ... } }
What I want:
A { a: B(42) }
I only want the first output when B
does not implement Debug
.
Debug
for your trait. – Clayborne