feat: added Arena::next_index - #54
Conversation
26bb0ef to
1f76856
Compare
|
also @mikwielgus going to ping you here since you wrote the Entry API |
|
@sanbox-irl thanks for the ping. Why do you need this? |
|
@mikwielgus For self-referential inserts. Basically you can now make: Obviously this is a silly example. This is also the equivalent of https://docs.rs/slab/latest/slab/struct.Slab.html#method.vacant_key |
| } | ||
| } else { | ||
| let slot: u32 = self.storage.len().try_into().unwrap_or_else(|_| { | ||
| unreachable!("Arena storage exceeded what can be represented by a u32") |
There was a problem hiding this comment.
Exceeding storage size is not really unreachable, so I'd use .expect() here instead.
There was a problem hiding this comment.
That's what Arena::insert uses, and I'd rather copy what it does there.
|
One more comment: An alternative interface would be something like |
|
Yes, I am also okay with that approach, though I think |
Yes, I agree that |
|
Given that let mut a: Arena<Index> = Arena::new();
let entry = a.vacant_entry();
entry.insert(entry.key());of course this means that a mutable borrow to the |
|
Also an unrelated note: we should really rename all the APIs that have the name |
Yeah that's pretty much the reason to have the alternative. Getting to skip that mutable borrow can make this kind of code incredibly simpler. For my particular use case though, I could also use something like Generally though, this seems weird, but this is as simple of an operation as doing: I think of |
Added two useful methods:
Arena::vacant_entrywhich makes a new Entry while computing the next keyArena::next_indexwhich immutably takes Arena and calculates what the next key would be.There's some testing for this but I didn't go sicko mode.
I also edited the Changelog but i'm not sure if you'd prefer to do that yourself!