Tuple_combinations on JoinIter

(Omger) #1

Can I use tuple_combinations and some other functions from Iterator trait with JoinIter? I can see that they have blanket implementations, but when I’ve tried to use it on some components (which all had Clone trait derived) compiler had shown long errors from which I concluded that tuple_combinations also needs Clone trait implemented on storages. What i am trying to do is to iterate through all pair combinations of joined entities, is there an easy way to do that?

0 Likes

(Thomas Schaller) #2

Welcome to the forums @omger!

Can I use tuple_combinations and some other functions from Iterator trait with JoinIter?

Can you provide a link to “tuple_combinations”?

You can use all the methods of Iterator on a JoinIter, but you cannot join Iterators. If I understood you correct, you want the former.

Now, when you have an Iterator, there’s no way to combine elements arbitrarily since elements are only yielded once; in most cases, you therefore need to store the elements in a buffer:

// Store this buffer in your system
let mut buffer = vec![];

// Now, in run:
buffer.clear();
buffer.extend((&storage_a, &storage_b).join());
// `buffer` now has all elements and you can combine them in any way
1 Like

(Omger) #3

https://docs.rs/itertools/0.7/itertools/trait.Itertools.html#method.tuple_combinations
It’s in the blanket implementations for JoinIter.
Yes, I want the former.

1 Like

(Thomas Schaller) #4

Thanks for the link. Did you make sure the tuple is of size 4 or smaller and that is Clone (note that this is not true for &mut references)?

1 Like

(Omger) #5

No, it has 5 items and some of them are &mut. Thanks for the info, I will try it the other way then.

0 Likes

(Omger) #6

It looks like shred::res::data::Read<'s, specs::world::entity::EntitiesRes> doesn’t have Clone trait, so it’s not working with storages at all.
Even something like that:

type SystemData = (
        Entities<'s>,
        ReadStorage<'s, Transform>,
    );
fn run(
    &mut self,
    (
        entities,
        transforms
    ): Self::SystemData,
) { 
if let iter = (&entities, &transforms).join()
        {
            for (ent1,ent2) in iter.tuple_combinations() {

            }
        }
}

won’t work.

0 Likes