I'm trying to make a simple simple linked list type class where I have a pointer to the next node, but am running into some issues. What would be the proper way to do this?
What I currently have is this:
trait Base {
fn connect<'a, 'b>(&'a self, next: &'b Base);
}
struct MyStruct<'b> {
next: Option<&'b Base>, // This should be swapped out with a reference to Base for the next node
}
impl<a', b'> Base for MyStruct<'b> {
pub fn new() -> MyStruct<'b'> {
MyStruct { next: None, }
}
pub fn connect<'a, 'b>(&'a self, layer: &'b Base) {
self.next = Some(layer);
}
}
The way I picture this the lifetime for the struct/node that is connected should be the should be the same as the initial node (i.e. when I deallocate a list it should do so entirely) so it should have one lifetime. However, I believe this causes issues when there is a self pointer as in the connect function.