简体   繁体   中英

Pass boxed trait as a reference

So I have a trait that I want to pass around and do some dynamic method dispatching on. I have one method that needs the trait as a boxed pointer, but it needs to call another method that uses a reference to the trait. So something like:

trait Foo {
    fn do_something(&self);
}

struct Bar;

impl Foo for Bar {
    fn do_something(&self) {}
}

fn foo_as_box(foo : Box<Foo>) {
    foo_as_ref(&foo);
}

fn foo_as_ref(foo : &Foo) {
    foo.do_something();
}

fn main() {
    let boxed_foo = box Bar as Box<Foo>;
    foo_as_box(boxed_foo);
} 

But, I get an error on this code

error: failed to find an implementation of trait Foo for Box Foo<no-bounds>

The compiler will convert Box<Foo> into &Foo as needed automatically. You can just say

foo_as_ref(foo);

and it will work.

This doesn't necessarily work in more complex situations. Which is to say, the compiler can't always tell that you want that conversion.

Now that DST has been implemented, you can say &*foo to do the conversion.

In the past the only way to force it was a let -binding, as in

let ref_foo: &Foo = foo;

Of course, your use-case is simple enough that you don't need this.

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