fitzgen / generational-arena

A safe arena allocator that allows deletion without suffering from the ABA problem by using generational indices.
https://docs.rs/generational-arena
Mozilla Public License 2.0
672 stars 53 forks source link

Add a function to create an Index #21

Closed AndreaCatania closed 5 years ago

AndreaCatania commented 5 years ago

I need to create an opaque key.

Would be nice to have the possibility to use a function new to create an Index, when you want to deal with opaque indices.

It's unsafe crate IDs out of this crate, so an idea is to create an unsafe function. Something like: unsafe create(id: u32, version: u32);

AndreaCatania commented 5 years ago

Hello @fitzgen do you like to add such a change or it's not something that you like to support?

fitzgen commented 5 years ago

It isn't clear to me what the use case / motivation is here or why the proposed solution is the best way to solve the problem. Could you provide more details?

AndreaCatania commented 5 years ago

I'm implementing the APIs of an interface that use opaque ids to refer the elements. This opaque ids is called ResourceID.

fn create_element() -> ResoourceID;
fn drop_element(id: ResourceID);
fn set_color(id: ResourceID, color: Vec<f32>);

Internally I'm storing the elements inside the generational arena, and I need to convert the Index from/to the ResourceID.

I've added this possibility with the PR https://github.com/fitzgen/generational-arena/pull/22 and would be nice if this could land upstream.

Make the Index opaque is something really useful that even other similar libraries already support.

fitzgen commented 5 years ago

So you don't want this to actually allocate space for the index or anything like that?

It seems like the APIs you are describing are possible with a new type right now:

struct ResourceId(Index);

What am I missing here?

AndreaCatania commented 5 years ago

ResourceId is a type defined by the abstraction that I cannot change. Check it here: https://github.com/AndreaCatania/amethyst_phythyst/blob/master/src/objects.rs#L12-L74

AndreaCatania commented 5 years ago

@fitzgen also there is a fork of your crate (that for sure you know https://crates.io/crates/typed-generational-arena) that allow me do what I need. But I would like to use directly your crate since I don't really need a new type.

By the way, other crates like slotmap allows to obtains an opaque ID: https://github.com/orlp/slotmap/blob/master/src/lib.rs#L298 This solution is a bit restrictive but would be also enough for me.

AndreaCatania commented 5 years ago

My proposal to add this feature is this one: https://github.com/fitzgen/generational-arena/pull/22/files