Help understanding Iterator lifetime conflict

Can someone explain why there is a lifetime conflict here? I have a situation where I want to mutably iterate over Parts and their associated RigidBodys together. The Parts are stored in a generational_arena::Arena, and the bodies are stored in a RigidBodySet, which is essentially an Arena. I get the index for the body with part.body_handle(). I've written this Iterator implementation:

pub struct World {
    parts: Arena<Part>,
    bodies: RigidBodySet,
    pub planets: planets::Planets,
    reference_point_body: RigidBodyHandle,
}
...
struct PartsIteratorMut<'a> {
    parts_iter: generational_arena::IterMut<'a, Part>,
    bodies: &'a mut RigidBodySet,
}
impl<'a> Iterator for PartsIteratorMut<'a> {
    type Item = (PartHandle, &'a mut Part, &'a mut RigidBody);
    fn next(&mut self) -> Option<Self::Item> {
        if let Some((part_handle, part)) = self.parts_iter.next() {
            Some((part_handle, part, &mut self.bodies[part.body_handle()]))
        } else { None }
    }
}

That gives the following error. I had a similar error when I tried to self.parts.iter_mut().map(|(part_handle, part)| (part_handle, part, &mut self.bodies[part.body_handle()])).

error[E0495]: cannot infer an appropriate lifetime for lifetime parameter in function call due to conflicting requirements
   --> src/world/mod.rs:367:43   
    |                       
367 |             Some((part_handle, part, &mut self.bodies[part.body_handle()]))
    |                                           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    |                          
note: first, the lifetime cannot outlive the anonymous lifetime #1 defined on the method body at 365:5...
   --> src/world/mod.rs:365:5                                                                                 
    |
365 |     fn next(&mut self) -> Option<Self::Item> {                                                          
    |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^                                                            
note: ...so that reference does not outlive borrowed content
   --> src/world/mod.rs:367:43
    |                                               
367 |             Some((part_handle, part, &mut self.bodies[part.body_handle()]))
    |                                           ^^^^^^^^^^^                          
note: but, the lifetime must be valid for the lifetime `'a` as defined on the impl at 363:6...
   --> src/world/mod.rs:363:6
    |
363 | impl<'a> Iterator for PartsIteratorMut<'a> {
    |      ^^
note: ...so that the types are compatible
   --> src/world/mod.rs:365:46
    |
365 |       fn next(&mut self) -> Option<Self::Item> {
    |  ______________________________________________^
366 | |         if let Some((part_handle, part)) = self.parts_iter.next() {
367 | |             Some((part_handle, part, &mut self.bodies[part.body_handle()]))
368 | |         } else { None }
369 | |     }
    | |_____^
    = note: expected `Iterator`
               found `Iterator`

I'm confused as to why the lifetime '_ lifetime of self doesn't infer 'a since the struct has 'a built into it's signature. Shouldn't '_ and 'a be basically the same?

The problem is that &mut self.bodies[part.body_handle()] borrows from the &mut self parameter given to the next function, which is however shorter than 'a. If this was allowed then multiple calls to next would mutably borrow self.bodies at the same time, and that's not allowed. Think for example what would happen if &mut self.bodies[part.body_handle()] returned a mutable reference to the same data twice in a row.

1 Like