roeeshoshani · The Rust Programming Language Forum

1638

I would like to self nominate index_type, a crate for providing strongly typed indices for collections. Prevents you from using an index to one array as an index to another array, at compile time, and provides a full ecosystem for working with such typed indices.

For example, it provides a way to iterate over ranges with custom index types (e.g MyIndex::new(5)..=MyIndex::new(8)). By default, in stable rust, you cannot iterate over this type. With index_type, you can just write MyIndex::new(5)..=MyIndex::new(8).iter() which gives you something you can iterate on.

Additionally, it allows you to use NonZero integer types as indices. This is useful since it makes Option<MyIndex> have the same size as MyIndex, which is useful if you are storing a lot of Option<MyIndex> objects in memory, and may also improve the performance of code that uses Option<MyIndex>.

Basically this crate provides you with everything you need when working with typed indices, and it is very nice to use. I personally used it in some of my own personal projects where it was very useful. I specifically found it very useful when implementing custom graph data structures and other compiler related data structures, where you have many vectors containing many objects of different types, and you have a lot of indices to those vectors in different locations in your code.

Read the original on users.rust-lang.org ↗