As the following code shows:
trait T2impl {}
struct S4T2impl;
impl T2impl for S4T2impl{}
trait TimplMethod {
fn f() -> impl T2impl;
}
struct S4TimplMethod;
impl TimplMethod for S4TimplMethod {
fn f() -> impl T2impl {
S4T2impl
}
}
fn f1() -> impl TimplMethod {
S4TimplMethod
}
// so far so good
// but I want to return one of more TimplMethod implementations, so I need a dynamic approach:
// fn f2() -> Box<dyn TimplMethod> {
// Box::new( S4TimplMethod)
// }
Here I get the error message:
error[E0038]: the trait `TimplMethod` cannot be made into an object
--> src/main.rs:158:18
|
158 | fn f2() -> Box<dyn TimplMethod> {
| ^^^^^^^^^^^^^^^ `TimplMethod` cannot be made into an object
|
note: for a trait to be "object safe" it needs to allow building a vtable to allow the call to be resolvable dynamically; for more information visit <https://doc.rust-lang.org/reference/items/traits.html#object-safety>
--> src/main.rs:140:7
How can I solve this problem?
[update]
As I can see in some answers some suggest to change the TimplMethod trait. This is not what I am looking for. Please consider all code from line 1 to function f1 as read-only.
Box<dyn T2impl>instead ofimpl T2implfrom the method.TimplMethodexcept itsf()will returnBox<dyn T2impl>. The question is, will it be enough for you?