I have a struct that holds a trait object member like this:
trait Contract {}
#[derive(Debug)]
struct Foo {
x: Box<Contract>,
}
I want that struct to derive Debug
, but the compiler doesn't like it:
error[E0277]: `Contract + 'static` doesn't implement `std::fmt::Debug`
--> src/main.rs:5:5
|
5 | x: Box<Contract>,
| ^^^^^^^^^^^^^^^^ `Contract + 'static` cannot be formatted using `:?`; add `#[derive(Debug)]` or manually implement `std::fmt::Debug`
|
= help: the trait `std::fmt::Debug` is not implemented for `Contract + 'static`
= note: required because of the requirements on the impl of `std::fmt::Debug` for `std::boxed::Box<Contract + 'static>`
= note: required because of the requirements on the impl of `std::fmt::Debug` for `&std::boxed::Box<Contract + 'static>`
= note: required for the cast to the object type `std::fmt::Debug`
I'm not really sure how to approach fixing this. I understand why the compiler can't implement Debug
for the trait since it can't tell what types will implement it, but that same reason is what keeps me from implementing it manually for the trait (not even sure if that's even possible).
What would be a good approach to obtain the behaviour I want?