The problem here is that you are using a trait object in the struct definition
rather than a generic; at the moment, struct generics cannot have trait bounds,
though, so the code for the struct would be simply:
```
struct MyStruct<‘a, T> {
my_field: &’a T
}
```
Then the `impl` code should be exactly as you have now.
--
John Grosen
On Tuesday, February 4, 2014 at 1:16 PM, Marc Bowes wrote:
> Hello,
>
> I'm trying to implement a struct where one of the fields is a reference and
> therefore has bounded lifetime. The reason I would like it to be a reference
> is to encourage sharing of the value in question as setup of said value might
> be expensive. In my specific example, the value is a session manager and
> opening said session is expensive.
>
> I have come up with the following
>
> ```
> trait MyTrait {
> fn be_traity(&self);
> }
>
> struct MyImpl {
> my_field: u32
> }
>
> impl MyTrait for MyImpl {
> fn be_traity(&self) {}
> }
>
> struct MyStruct<'a> {
> my_field: &'a MyTrait
> }
>
> impl<'a> MyStruct<'a> {
> fn new<T: MyTrait>(my_field: &'a T) -> MyStruct {
> MyStruct {
> my_field: my_field
> }
> }
> }
>
> fn main() {
> let my_field = MyImpl { my_field: 0 };
> let my_struct = MyStruct::new(&my_field);
> }
>
> ```
>
> This fails to compile:
>
> rust-lifetimes-with-references.rs:20:23: 20:31 error: value may contain
> references; add `'static` bound
> rust-lifetimes-with-references.rs:20
> (http://rust-lifetimes-with-references.rs:20) my_field: my_field
> ^~~~~~~~
>
>
> This confuses me because "may contain references" is exactly what I want? I
> want to assign it as a ref.. If I slap on a & in the assignment (for no good
> reason other than being confused):
>
> MyStruct {
> my_field: &my_field
> }
>
>
> Then I get:
>
> rust-lifetimes-with-references.rs:20:23: 20:32 error: failed to find an
> implementation of trait MyTrait for &'a T
> rust-lifetimes-with-references.rs:20
> (http://rust-lifetimes-with-references.rs:20) my_field: &my_field
>
> ---
>
> I'm clearly doing something stupid but cannot find a reference example..
> _______________________________________________
> Rust-dev mailing list
> [email protected] (mailto:[email protected])
> https://mail.mozilla.org/listinfo/rust-dev
>
>
_______________________________________________
Rust-dev mailing list
[email protected]
https://mail.mozilla.org/listinfo/rust-dev