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 generics in Index #35

Open mikialex opened 4 years ago

mikialex commented 4 years ago

Index returned from Arena<TypeA> should not be used in Arena<TypeB>, but we dont use generics in index, so such type constraint not exist. To enforce compiletime check better we can add generics in index: Arena insert item and return Index.

some reference https://github.com/gfx-rs/naga/blob/master/src/arena.rs (without generation check)

When using generational-arena to store different type of resource, like scenegraph resource management, such typed Index handle is essential.

sinistersnare commented 4 years ago

Hi, I would also like that for a project of mine. Would this change be welcome?

Prin-to commented 4 years ago

Hello, making the Index generic will probably bind it to the std, which against the aim of this repo. You can easily clone the repo and change the Index class into

use std::marker::PhantomData struct Index<T> { index: u64, generation : usize, phantom: PhantomData<T> }

I hope it helped you :)

AaronKutch commented 3 years ago

PhatomData is included in core

LoganDark commented 1 year ago

In order to properly enforce this, it would require an invariant lifetime like drop_arena, which would make it incredibly painful to include an Arena as a field in any other structure (https://github.com/markcsaving/drop_arena/issues/1). However, an Index generic over the value type could still be useful for structs that contain Indexes to multiple arenas, because then it would at least be obvious which Index goes to which arena.