So my goal here is to have a structure, that can create instance of a template T where T is implementing a specific trait.
Like this:
trait MyTrait {
fn do_something(&self);
}
Here is my struct that generate the instances, I've put them in a Box because the size can vary
struct MyStruct {
instances: Vec<Box<dyn MyTrait>>,
}
this struct as a gen function which take a template T that implement MyTrait, it also ask for the Default trait so that he's able to generate a default instance without breaking the Object-safety
impl MyStruct {
// ...
pub fn gen<T>(...) -> bool
where T: MyTrait + Default
{
self.instances.push(Box::new(T::default()));
}
// ...
}
But this kind of code is throwing an error when doing the push "the parameter type MyTrait may not live long enough"
and I'm very confuse by this error because I'm literally creating the MyTrait instance and putting it in a Box so I would assume that the lifetime was going to be bound to the vector or something.
In my attempt I've tried to add the 'a lifetime to my struct so that the Box lifetime in my vector was bound to the lifetime of the struct, but it ain't working :/
If you wonder why I do this, is because I started to use lambda and that make thing super complicated to interact with other stuff