Lifetime and type inference issues with traits

#1

Context

I am trying to implement a model for my entities in my games. I am aware of already existing ECS libraries but i am experimenting with another approach as a learning experience.

The goal is to store entities of the same layout (= with the same components) in a struct of array fashion (that I will call tables). Iterating on all entities that contains a particular set of components only then require to find tables that match these requirements.

Problem

I have trouble satisfying the borrow checker and the type inference to implement a safe interface for the for_each method. I managed to implement the spawn method without generic associated types but without any success for this more hairy problem.

The main problem I think comes from creating a typed view of a table line 561, I fail to express that the view only exists for the current iteration and that the parameters given to the closure are only valid for the scope of the closure call. e.g:

let mut bad = None; 

for_each(&entities, &query, |eid: &EId| {
    bad = Some(eid);
})

The code

The code is here. I tried to reduce the case but it is unfortunatly still quite big…

The most interesting parts are the View trait line 524, for_each line 549. There is also the Entity trait defined line 141 that is quite similar to the View trait.

The rest is mostly boilerplate around storing type erased struct of arrays.