我如何从另一个特征中导出一个特征?

How do I derive a trait for another trait?

我有一个包含特征对象成员的结构,如下所示:

trait Contract {}

#[derive(Debug)]
struct Foo {
    x: Box<Contract>,
}

我希望该结构派生 Debug,但编译器不喜欢它:

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`

我不太确定如何解决这个问题。我理解为什么编译器无法为特征实现 Debug 因为它无法判断哪些类型将实现它,但同样的原因使我无法为特征手动实现它(甚至不确定那是不是甚至可能)。

获得我想要的行为的好方法是什么?

特质不能使用#[derive()]属性;你需要手动实现它:

trait Contract {}

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

由于 trait 对象丢失了类型信息 (type erasure),您可以使用 Contract 实现的函数,但您将无法访问底层类型或它们对 Debug.

但是,如果您使 Contract 依赖于 Debug 特征,确保其所有实现者也必须实现 Debug:

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

您将能够 #[derive(Debug)] foo,而无需手动为 Contract 实施 Debug