Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can a Rust trait object return another trait object?

Tags:

rust

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();
}
like image 967
Ignatio Mobius Avatar asked Sep 03 '25 06:09

Ignatio Mobius


1 Answers

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{})
  }
}
like image 113
vkurchatkin Avatar answered Sep 04 '25 23:09

vkurchatkin