0

I have a type which wraps a mutable reference:

pub struct PipelineBinding<'a> {
    pub renderpass: &'a mut RenderPass<'a>
}

impl RenderPipelineBinding<'a> {
    fn renderpass(&mut self) -> &mut RenderPass<'a> {
        self.renderpass
    }
    fn bind_vertex_buffer(
        &mut self,
        slot: u32,
        buffer_slice: VertexBufferSlice<'a, VertexType>,
    ) {
        ...
    }
    fn bind_index_buffer<T: IndexType>(&mut self, buffer: IndexBufferSlice<'a, T>) {
        ...
    }
}

When I try to use it like so:

render_pass.set_pipeline(&self.render_pipeline);

{
   let mut binding = PipelineBinding {
       renderpass: &mut render_pass,
   };

   binding.bind_vertex_buffer(0, self.verticies.slice(..));
   binding.bind_index_buffer(self.indicies.slice(..));
} // Mark: Last use of binding

render_pass.draw_indexed(0..(INDICES.len() as u32), 0, 0..1);

I get the following error:

error[E0597]: `render_pass` does not live long enough
   --> use-case/src/main.rs:283:33
    |
283 |                     renderpass: &mut render_pass,
    |                                 ^^^^^^^^^^^^^^^^ borrowed value does not live long enough
...
293 |         }
    |         -
    |         |
    |         `render_pass` dropped here while still borrowed
    |         borrow might be used here, when `render_pass` is dropped and runs the `Drop` code for type `RenderPass`

As well as this one:

error[E0499]: cannot borrow `render_pass` as mutable more than once at a time
   --> use-case/src/main.rs:290:13
    |
283 |                     renderpass: &mut render_pass,
    |                                 ---------------- first mutable borrow occurs here
...
290 |             render_pass.draw_indexed(0..(INDICES.len() as u32), 0, 0..1);
    |             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    |             |
    |             second mutable borrow occurs here
    |             first borrow later used here

Why is render_pass still borrowed here? Since binding is not used past } // Mark: Last use of binding, shouldn't it be dropped, thus releasing the borrowed reference to render_pass?

sak
  • 2,612
  • 24
  • 55

0 Answers0