Search code examples
rusttraits

How do I derive a trait for another trait?


I have a struct that holds a trait object member like this:

trait Contract {}

#[derive(Debug)]
struct Foo {
    x: Box<dyn 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?


Solution

  • Traits can't use the #[derive()] attribute; you need to implement it manually:

    trait Contract {}
    
    impl std::fmt::Debug for dyn Contract {
        fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
            write!(f, "{}", "derp")
        }
    }
    

    Since trait objects lose information on the type (type erasure) you could utilize the functions implemented by Contract, but you won't have access to the underlying types or their specific implementations of Debug.

    If, however, you make Contract dependent on the Debug trait, ensuring that all its implementors must also implement Debug:

    trait Contract: std::fmt::Debug {}
    

    You will be able to #[derive(Debug)] for foo without having to implement Debug for Contract manually.