简体   繁体   中英

How can a Rust trait object return another trait object?

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();
}

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{})
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM