Niko's DST changes may involve allowing bounds in type definitions, which includes structs. You can read more here<http://smallcultfollowing.com/babysteps/blog/2014/01/05/dst-take-5/> .
On Tue, Feb 4, 2014 at 1:31 PM, Marc Bowes <[email protected]> wrote: > I see, thanks. Just to be clear, is this correct then? > > ``` > trait MyTrait { > fn be_traity(&self); > } > > struct MyImpl { > my_field: u32 > } > > impl MyTrait for MyImpl { > fn be_traity(&self) {} > } > > struct MyStruct<'a, T> { > my_field: &'a T > } > > impl<'a, T: MyTrait> MyStruct<'a, T> { > fn new(my_field: &'a T) -> MyStruct<'a, T> { > MyStruct { > my_field: my_field > } > } > } > > fn main() { > let my_field = MyImpl { my_field: 0 }; > let my_struct = MyStruct::new(&my_field); > } > ``` > > The main differences being: > > 1) The struct definition for MyStruct no longer gives any clue as to what > my_field might be (this seems weird to me) > 2) The impl now includes 'T: MyTrait' and returns the templated MyStruct > > How (if at all) will Niko's DST changes impact this? More broadly, what > else might impact this in the (short/medium-term) future? #1 bothers me a > bit (right now :-)). > > Thanks! > > > On Tue, Feb 4, 2014 at 11:21 PM, John Grosen <[email protected]> wrote: > >> 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 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 my_field: &my_field >> >> --- >> >> I'm clearly doing something stupid but cannot find a reference example.. >> _______________________________________________ >> Rust-dev mailing list >> [email protected] >> https://mail.mozilla.org/listinfo/rust-dev >> >> >> > > _______________________________________________ > Rust-dev mailing list > [email protected] > https://mail.mozilla.org/listinfo/rust-dev > >
_______________________________________________ Rust-dev mailing list [email protected] https://mail.mozilla.org/listinfo/rust-dev
