How to implement ECS style lookups with multiple exclusive borrows

I'm trying to understand how various ECS (e.g. Hecs, Bevy ECS, etc) can have what appears to be multiple exclusive borrows to data and how they are able to implement what appear to be dynamic queries for the resources they contain using tuples of types, i.e.: something like

let (a, b, c) = world.get::<(&A, &B, &C)>();
let (d, e) = world.get::<(&D, &E)>();
let (a, b, c) = world.get_mut::<(&mut A, &mut B, &C)>();

I'm assuming there is some element of borrow splitting going on for the multiple exclusive borrows. I don't even know where to begin with the generic parameter that is used to determine which resources to look up.

Having looked through the sources, I'm struggling to identify exactly what aspects of the ECS are needed to make this work. I'm trying to find something that can help distil the concepts at work here; the few articles I have found tend to focus on the storage and retrieval of entities and components rather than how these can be looked up.

Is anyone able to help explain how this works or point me to some articles?

Have a look at this

1 Like

Bevy has a WorldQuery trait for these arguments, which gives it a ComponentId for every type used, and it can check at run time if ComponentIds repeat or not.

1 Like

This is very helpful, thank you.