LPGhatguy / thunderdome

Arena type inspired by generational-arena
Apache License 2.0
197 stars 15 forks source link

Add Arena::get2_mut(Index, Index) or similar methods #21

Closed lynzrand closed 3 years ago

lynzrand commented 3 years ago

As one coming from generational-arena, I found thunderdome is lacking a method similar to Arena::get2_mut(&mut self, Index, Index) -> (Option<&mut Value>, Option<&mut Value>).

This method allows library users to access two different values at once (as long as their indices are different), allowing one to perform complex operations efficiently. An example usage would be connecting doubly-linked lists inside this arena, where having mutable references of both lists' ends simplifies the code.

Taking this idea one step further, adding another method like unsafe Arena::get_many_mut(&mut self, impl Iterator<Item=Index>) -> impl Iterator<Option<&mut Value>> (where the user ensures no two indices are the same) also seems reasonable. I'm just not sure if it's really needed.

LPGhatguy commented 3 years ago

I would accept a PR for Arena::get2_mut. I think the more generalized form gets into hairy situations pretty easily, and we ought to be able to implement get2_mut with only safe code using slice::split_first_mut.