generational-arena icon indicating copy to clipboard operation
generational-arena copied to clipboard

Add generics in Index

Open mikialex opened this issue 4 years ago • 4 comments

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<TypeA> insert item and return Index<TypeA>.

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.

mikialex avatar Jun 11 '20 08:06 mikialex

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

sinistersnare avatar Aug 08 '20 05:08 sinistersnare

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 :)

Prin-to avatar Oct 13 '20 06:10 Prin-to

PhatomData is included in core

AaronKutch avatar Dec 17 '20 05:12 AaronKutch

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.

LoganDark avatar Nov 05 '23 22:11 LoganDark