-3

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 at

my_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?

Community
  • 1
  • 1
AdmiralJonB
  • 2,038
  • 3
  • 23
  • 27
  • 1
    The code you have posted is not syntactically valid Rust code. – Shepmaster Nov 06 '18 at 22:51
  • I believe your question is answered by the answers of [Why is the bound `T: 'a` required in order to store a reference `&'a T`?](https://stackoverflow.com/q/41794800/155423); [The compiler suggests I add a 'static lifetime because the parameter type may not live long enough, but I don't think that's what I want](https://stackoverflow.com/q/40053550/155423). If you disagree, please [edit] your question to explain the differences. Otherwise, we can mark this question as already answered. – Shepmaster Nov 06 '18 at 22:55
  • See also [Parameter type may not live long enough](https://stackoverflow.com/q/32625583/155423); [Parameter type may not live long enough?](https://stackoverflow.com/q/29740488/155423); [Why “explicit lifetime bound required” for Box in struct?](https://stackoverflow.com/q/25959075/155423); and basically any other question that mentions your error message. – Shepmaster Nov 06 '18 at 22:56
  • Yes, I know it's not syntactically valid, otherwise I wouldn't have posted the error message when trying to compile. Your edited question title is exactly what I was asking for; however, I've put in additional details. – AdmiralJonB Nov 07 '18 at 06:12
  • *'-- help: consider adding an explicit lifetime bound T: 'a...'* – hellow Nov 07 '18 at 07:07
  • @hellow yes, the question is how to do that and keep the trait as well. – AdmiralJonB Nov 07 '18 at 07:09
  • Ah, I see! https://stackoverflow.com/questions/29740488/parameter-type-may-not-live-long-enough Should give you the information – hellow Nov 07 '18 at 07:12

1 Answers1

3

Ultimately, how can I specify that a generic type both follow a trait and have a lifetime?

The same way you'd specify multiple traits: add them with +. I.e. T : MyTrait + 'a

Jan Hudec
  • 73,652
  • 13
  • 125
  • 172