In this mesh class, what's wrong with my use of lifetimes?

I can almost guarantee the problem is &'b mut RenderPass<'b>.

&'b Foo<'b> (a reference to a thing with the same lifetime inside it) is suspicious enough to pay close attention to. But &'b mut Foo<'b> (same thing but with a mutable reference) is virtually always a mistake. The reason they're different has to do with variance and, fair warning, when the answer to your question contains the word "variance" you're probably going to have a bad time. I'll link to this SO question if you have an hour to kill. The upshot is that taking a &'b mut RenderPass<'b> means that the RenderPass is borrowed for its entire lifetime, and it can't be used anymore. You need to let the compiler choose different lifetimes.

Let's back up. Starting with no lifetimes at all:

pub fn render(&self, render_pass: &mut RenderPass<'_>)

What compiler error does this give you?

5 Likes