Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draining iterator for storages #264

Closed
malleusinferni opened this issue Sep 13, 2017 · 2 comments
Closed

Draining iterator for storages #264

malleusinferni opened this issue Sep 13, 2017 · 2 comments

Comments

@malleusinferni
Copy link
Contributor

On occasion, I find myself writing code like this:

for (_, eid) in (&foo, &entities).join() {
    if let Some(bar) = bar.remove(eid) {
        // Do something with bar
    }
}

Or this:

// To reduce allocation costs, working_set is owned by the system struct
for (_, eid) in (&bar, &entities).join() {
    self.working_set.push(eid);
}

for eid in self.working_set.drain(..) {
    if let Some(bar) = bar.remove(eid) {
        // ...
    }
}

Both of these rub me the wrong way, especially the second pattern with its redundant loop and owned list of entities. It seems to me that this logic should be available as some kind of modifier on the usual iteration patterns, since the storage already maintains a list of which entities have its associated components.

What I'd like to be able to write is something like the following:

// Remove "bar" from all entities that have it
for bar in bar.drain() {
    // ...
}

// Remove "bar" only from entities that also have "foo"
for (_, bar) in (&foo, bar.drain()).join() {
    // Do something with bar
}

I brought this up earlier on IRC earlier. @kvark suggested the name "drain" and @Aceeri suggested implementing it as a new Storage type. I'm not convinced the latter makes sense but I'm open to more suggestions.

@Aceeri
Copy link
Member

Aceeri commented Sep 13, 2017

Alternative using the check method:

for (entity, _, _) in (&*entities, &foo, &bar.check()).join() {
    bar.remove(entity);
}

@malleusinferni
Copy link
Contributor Author

malleusinferni commented Sep 13, 2017

Thanks for the example. My main codebase is still on 0.7 until I have enough spare time to update it.

I would still prefer to have a drain() option for flexibility's sake.

bors bot added a commit that referenced this issue Sep 29, 2017
273: Add `drain` method to `Storage` r=torkleyy a=torkleyy

Fixes #264
@bors bors bot closed this as completed in #273 Sep 29, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants