是否可以将 trait 对象转换为另一个 trait 对象?

dos*_*pro 8 casting dynamic rust

我尝试了以下代码:

trait TraitA {
    fn say_hello(&self) {
        self.say_hello_from_a();
    }
    fn say_hello_from_a(&self);
}

trait TraitB {
    fn say_hello(&self) {
        self.say_hello_from_b();
    }
    fn say_hello_from_b(&self);
}

struct MyType {}

impl TraitA for MyType {
    fn say_hello_from_a(&self) {
        println!("Hello from A");
    }
}

impl TraitB for MyType {
    fn say_hello_from_b(&self) {
        println!("Hello from B");
    }
}

fn main() {
    let a: Box<dyn TraitA> = Box::new(MyType {});
    let b: Box<dyn TraitB>;

    a.say_hello();
    b = a;
    b.say_hello();
}
Run Code Online (Sandbox Code Playgroud)

我收到以下编译错误:

error[E0308]: mismatched types
  --> src/main.rs:34:9
   |
34 |     b = a;
   |         ^ expected trait `TraitB`, found trait `TraitA`
   |
   = note: expected struct `std::boxed::Box<dyn TraitB>`
              found struct `std::boxed::Box<dyn TraitA>`
Run Code Online (Sandbox Code Playgroud)

我声明了两个特征和一个类型,MyType并为MyType. 我创建了一个新特性的对象TraitA类型的MyType,我叫a。由于a也实现了TraitB,我认为它应该能够被转换为TraitB.

我还没有弄清楚它是否可能。如果是,我如何将 trait objecta转换为TraitB

在 C++ 中,std::dynamic_pointer_cast<TraitB>(a);为了同样的目的,我会使用类似的东西。

这是我可以使用横向转换的示例:我有一个结构体,其中包含一些代表现实生活中的实体的数据:

struct MyType {
    a: i32,
    b: i32,
}
Run Code Online (Sandbox Code Playgroud)

这种类型的实例至少可以用于代码库的两个不同部分。在这两个部分,我都需要一个名为get_final_value.

有趣的部分是get_final_value应该根据谁调用它而做出不同的反应。

  • 为什么我不将类型拆分为两个不同的类型?:从技术上讲,按照设计,a并且b属于一起,并不是说get_final_value()使用两个值来计算结果。

  • 为什么不使用泛型/静态调度?因为MyType只是一个例子。在实际情况下,我有不同的结构,它们都以不同的方式实现这两个特征。

  • 为什么不使用Anytrait?老实说,直到最近我才知道它的存在。我不记得The Rust Programming Language提到过它。无论如何,似乎您需要知道具体类型才能从Any该具体类型转换到该具体类型,然后再转换到 trait 对象。

Jer*_*ere 5

另一种选择是创建一个同时使用TraitATraitB作为超特征并为每种类型提供强制转换的特征:

trait TraitC: TraitA + TraitB {
    fn as_trait_a(&self) -> &dyn TraitA;
    fn as_trait_b(&self) -> &dyn TraitB;
}
Run Code Online (Sandbox Code Playgroud)

然后MyType实现它:

impl TraitC for MyType {
    fn as_trait_a(&self) -> &dyn TraitA {
        self
    }
    fn as_trait_b(&self) -> &dyn TraitB {
        self
    }
}
Run Code Online (Sandbox Code Playgroud)

一旦你这样做了,你就可TraitC以为你Box和你的程序逻辑使用两者TraitATraitB一起使用。

示例 main 以显示各种使用方法:

fn test_a(a: &TraitA) {
    a.say_hello();
}
fn test_b(b: &TraitB) {
    b.say_hello();
}

fn main() {
    let c: Box<dyn TraitC> = Box::new(MyType {});

    TraitA::say_hello(&*c);
    TraitB::say_hello(&*c);

    c.as_trait_a().say_hello();
    c.as_trait_b().say_hello();

    test_a(c.as_trait_a());
    test_b(c.as_trait_b());

    let a: &dyn TraitA = c.as_trait_a();
    a.say_hello();
    let b: &dyn TraitB = c.as_trait_b();
    b.say_hello();
}
Run Code Online (Sandbox Code Playgroud)

铁锈游乐场

如果AB确实属于一起,这更好地代表了这一点,并且如果您愿意,您仍然可以自由地单独使用它们。