Hello,
I have a trait which call a function expecting this trait as parameter. But i don't know how type that. Example:
struct MyStruct1 {
}
impl MyStruct1 {
fn my_struct_func(&self, obj: Box<dyn MyTrait>) {
}
}
trait MyTrait {
fn my_func(&self) {
let my_struct = MyStruct1 {};
my_struct.my_struct_func(self);
}
}
fn main() {}
Produce
error[E0308]: mismatched types
--> src/main.rs:15:34
|
15 | my_struct.my_struct_func(self);
| ^^^^
| |
| expected struct `Box`, found `&Self`
| help: store this in the heap by calling `Box::new`: `Box::new(self)`
|
= note: expected struct `Box<(dyn MyTrait + 'static)>`
found reference `&Self`
= note: for more on the distinction between the stack and the heap, read https://doc.rust-lang.org/book/ch15-01-box.html, https://doc.rust-lang.org/rust-by-example/std/box.html, and https://doc.rust-lang.org/std/boxed/index.html
I tried some things without success (like my_struct.my_struct_func(&self as Box<dyn MyTrait>);
or my_struct.my_struct_func(Box::new(self));
).
How to do that ? Thanks in advance :