How can a Rust trait object return another trait object?

≯℡__Kan透↙ 提交于 2020-12-14 16:38:04

问题


How can I attempt something like the following in Rust?

The builder class is a trait object which returns another trait object (type erasure) where the implementation that is selected is defined by the specific object of the builder trait that we are using.

trait Builder {
    // I want this trait to return a trait object
    fn commits(&self) -> dyn Commit;

    fn finish(&self);
}

trait Commit {
}

struct FooBuilder {
}

struct FooCommit {
}

impl Builder for FooBuilder {
    fn commits(&self) -> impl Commit {
        FooCommit{ }
    }

    fn finish(&self) {
    }
}

fn get_commits(b: &Builder) {
    // trait object returns a trait
    let c = b.commits();
}

fn main() {
    let b = FooBuilder{};
    get_commits(&b);
    b.finish();
}

回答1:


There is no problem returning trait objects from trait methods in Rust:

trait Foo {
  fn bar(&self) -> Box<dyn Bar>;
}

One thing to notice is that you need to return Box<dyn Bar>, not dyn Bar, since size of dyn Bar is not known at compiled time, which renders it useless.

When you implement this trait, the signature has to match, so it should return Box<dyn Bar>, not impl Bar:

impl Foo for MyFoo {
  fn bar(&self) -> Box<dyn Bar> {
    Box::new(MyBar{})
  }
}


来源:https://stackoverflow.com/questions/60143046/how-can-a-rust-trait-object-return-another-trait-object

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!