Edit: Question as the title modified by Shepmaster. Added more details to the situation.
I'm wanting to have access to an object following a trait in different threads, stored overall in a struct. As I'm porting my code from another language to try to learn rust, I'm still a bit of a newbie to the language.
Originally I thought to have a struct similar to the following, which compiled fine:
struct MyStruct<T : MyTrait> {
my_object : Arc<Mutex<T>>
}
However, ultimately I need to store a reference to to it as it will be stored in multiple threads. I understand I need to use lifetimes as well to get this to work, so I ended up with the following:
struct MyStruct<'a, T : MyTrait> {
my_object : &' Arc<Mutex<T>>
}
The error I get is:
error[E0309]: the parameter type
T
may not live long enough-- help: consider adding an explicit lifetime bound
T: 'a
......so that the reference type
&'a std::sync::Arc<std::sync::Mutex<T>>
does not outlive the data it points atmy_object : &' Arc< Mutex < T > >
The error implies that I have to specify the lifetime to my generic, however, I already have a trait for my type. So Ultimately, how can I specify that a generic type both follow a trait and have a lifetime?