I have the following code:
pub trait Handler<T: Clone>: Clone {
fn invoke(&self) -> Result<(), std::io::Error>;
}
#[derive(Clone)]
pub struct App<T: Clone> {
handlers: Vec<Box<dyn Handler<T>>>,
}
fn main() -> Result<(), std::io::Error> {
#[derive(Clone)]
struct MyHandler<T: Clone> { tt: T }
impl<T: Clone> Handler<T> for MyHandler<T> {
fn invoke(&self) -> Result<(), std::io::Error> { () }
}
let app = App { handlers: vec![ MyHandler{ tt: 0 }, MyHandler{ tt: 5 } ] };
That generates a compile error:
error[E0038]: the trait `Handler` cannot be made into an object
--> src/main.rs:8:5
|
8 | handlers: Vec<Box<dyn Handler<T>>>,
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `Handler` cannot be made into an object
|
= note: the trait cannot require that `Self : Sized`
Even if I'm not trying to convert the trait into a concrete object. I don't understand: the box struct should allow me to store an unknown sized type.
The aim is to store a Vec
of Handler
implementors in order to have invoke
method for all object.