Skip to content

Instantly share code, notes, and snippets.

@mitchmindtree
Last active August 29, 2015 14:02
Show Gist options
  • Save mitchmindtree/1e076c2de620fec2bc57 to your computer and use it in GitHub Desktop.
Save mitchmindtree/1e076c2de620fec2bc57 to your computer and use it in GitHub Desktop.
A question regarding structs containing multiple structs with the same lifetime.
//
// I'd like to be able to gain access to the 'foo', 'bar' and 'baz' members
// via the "get_my_struct" trait method.
//
// I'd like to be able to access both 'foo' and 'bar' as well as a mutable ref
// to 'baz' in order to write to 'baz' within the 'calc_baz' method.
//
struct A<'a> {
i: int
//, some other struct ref that requires 'a...
}
struct MyStruct<'a> {
foo: A<'a>,
bar: A<'a>,
baz: A<'a>
}
trait MyTrait<'a> {
fn get_my_struct<'b>(&'b self) -> &'b MyStruct<'a>;
fn get_foo<'b>(&'b self) -> &'b A<'a> { &self.get_my_struct().foo }
fn get_bar<'b>(&'b self) -> &'b A<'a> { &self.get_my_struct().bar }
fn get_baz_mut(&'a mut self) -> &'a mut A<'a> { &mut self.get_my_struct().baz }
fn calc_baz(&'a mut self) {
*self.get_baz_mut() = A { i: self.get_foo().i * self.get_bar().i } // << Errors point here.
}
}
impl<'a> MyTrait<'a> for MyStruct<'a> {
fn get_my_struct<'b>(&'b self) -> &'b MyStruct<'a> { self }
}
// fn main() {
// let ms = MyStruct { foo: A{i:5}, bar: A{i:10}, baz: A{i:0} };
// }
// NEW ERRORS AND NOTES:
/*
<anon>:22:46: 22:66 error: cannot infer an appropriate lifetime for lifetime parameter 'b in function call due to conflicting requirements
<anon>:22 fn get_foo<'b>(&'b self) -> &'b A<'a> { &self.get_my_struct().foo }
^~~~~~~~~~~~~~~~~~~~
<anon>:22:43: 22:72 note: first, the lifetime cannot outlive the lifetime 'a as defined on the block at 22:42...
<anon>:22 fn get_foo<'b>(&'b self) -> &'b A<'a> { &self.get_my_struct().foo }
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
<anon>:22:46: 22:66 note: ...so that the pointer does not outlive the data it points at
<anon>:22 fn get_foo<'b>(&'b self) -> &'b A<'a> { &self.get_my_struct().foo }
^~~~~~~~~~~~~~~~~~~~
<anon>:22:43: 22:72 note: but, the lifetime must be valid for the lifetime 'b as defined on the block at 22:42...
<anon>:22 fn get_foo<'b>(&'b self) -> &'b A<'a> { &self.get_my_struct().foo }
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
<anon>:22:45: 22:70 note: ...so that types are compatible (expected `&'b A<'a>` but found `&A<'a>`)
<anon>:22 fn get_foo<'b>(&'b self) -> &'b A<'a> { &self.get_my_struct().foo }
^~~~~~~~~~~~~~~~~~~~~~~~~
<anon>:23:46: 23:66 error: cannot infer an appropriate lifetime for lifetime parameter 'b in function call due to conflicting requirements
<anon>:23 fn get_bar<'b>(&'b self) -> &'b A<'a> { &self.get_my_struct().bar }
^~~~~~~~~~~~~~~~~~~~
<anon>:23:43: 23:72 note: first, the lifetime cannot outlive the lifetime 'a as defined on the block at 23:42...
<anon>:23 fn get_bar<'b>(&'b self) -> &'b A<'a> { &self.get_my_struct().bar }
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
<anon>:23:46: 23:66 note: ...so that the pointer does not outlive the data it points at
<anon>:23 fn get_bar<'b>(&'b self) -> &'b A<'a> { &self.get_my_struct().bar }
^~~~~~~~~~~~~~~~~~~~
<anon>:23:43: 23:72 note: but, the lifetime must be valid for the lifetime 'b as defined on the block at 23:42...
<anon>:23 fn get_bar<'b>(&'b self) -> &'b A<'a> { &self.get_my_struct().bar }
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
<anon>:23:45: 23:70 note: ...so that types are compatible (expected `&'b A<'a>` but found `&A<'a>`)
<anon>:23 fn get_bar<'b>(&'b self) -> &'b A<'a> { &self.get_my_struct().bar }
^~~~~~~~~~~~~~~~~~~~~~~~~
error: aborting due to 2 previous errors
playpen: application terminated with error code 101
*/
// THINGS I'VE TRIED:
//
// - Getting 'foo' and 'bar' as mutable instead, however when I do this,
// 'self' seems to take it's own reference eternally, meaning that it
// is still an issue even when I try to separate the method into individual
// lines.
//
// - Removing the lifetime from the "&'a mut self" parameter in calc_baz,
// however this raises another error where the 'get' functions can't assure
// the lifetime of the references that they return.
//
// - Various arrangements of the lifetime setup for the 'getter' functions,
// however I just can't seem to get something that plays nice!!
//
// Any help would be immensely appreciated!!!
//
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment