I have been pulling my hair out in the last week because of this incredibly annoying problem with a lifetime.
The problem occurs when I try to put a reference to Buffer inside a DataSource , which then refers to DrawCommand . I get an error: vertex_data_source does not live long enough .
src/main.rs:65:23: 65:41 error: src/main.rs:65 data_source: &vertex_data_source ^~~~~~~~~~~~~~~~~~ src/main.rs:60:51: 67:2 note: reference must be valid for the block suffix following statement 3 at 60:50... src/main.rs:60 let vertices = VertexAttributes::new(&buffer); src/main.rs:61 src/main.rs:62 let vertex_data_source = factory.create_data_source(vertices); src/main.rs:63 src/main.rs:64 let command: DrawCommand<ResourcesImpl> = DrawCommand { src/main.rs:65 data_source: &vertex_data_source ... src/main.rs:62:67: 67:2 note: ...but borrowed value is only valid for the block suffix following statement 4 at 62:66 src/main.rs:62 let vertex_data_source = factory.create_data_source(vertices); src/main.rs:63 src/main.rs:64 let command: DrawCommand<ResourcesImpl> = DrawCommand { src/main.rs:65 data_source: &vertex_data_source src/main.rs:66 }; src/main.rs:67 }
He says that vertex_data_source must be valid for the suffix of the block following statement 3 on line 60. My interpretation of this error is that vertex_data_source must be defined before line 60 . But to create a vertex_data_source , first of all I need access to those VertexAttributes on line 60, so I canβt just change the order around.
It seems to me that all the lives sprinkled with my code need to be divided by 2 or maybe just deleted, however I tried every combination that seemed reasonable, and I'm not from the idea.
Below is a very simplified example of my code that demonstrates the problem. I would really appreciate a sanity check and hopefully a fresh mind could identify the problem. (every time before a few days of feedling released a fix, but this time I'm at a standstill).
use std::cell::RefCell; use std::marker::PhantomData; pub struct DrawCommand<'a, R: Resources<'a>> { pub data_source: &'a R::DataSource } pub trait Resources<'a> { type DataSource: 'a; type Buffer: 'a; } pub struct DataSource<'a> { id: u32, attributes: Vec<VertexAttributes<'a, ResourcesImpl<'a>>>, current_element_array_buffer_binding: RefCell<Option<Buffer<'a>>> } pub struct Buffer<'a> { context: &'a GraphicsContextImpl } pub struct GraphicsContextImpl; pub struct ResourcesImpl<'a> { phantom: PhantomData<&'a u32>
Thank you very well in advance.
EDIT:
I updated the code to better reflect my actual implementation.
By the way, replacing this:
let vertex_data_source = factory.create_data_source(vec!(vertices));
Wherein:
let vertex_data_source = DataSource { id: 0, attributes: vec!(vertices), current_element_array_buffer_binding: RefCell::new(None) };
Does not solve the problem.