arrayvec icon indicating copy to clipboard operation
arrayvec copied to clipboard

Feature request: Implement `Index` and `IndexMut`

Open niluxv opened this issue 4 years ago • 2 comments

For code which is generic over the Index or IndexMut traits, it would be helpful if ArrayVec would implement the Index and IndexMut traits (in core::ops). I understand and like that you prefer ArrayVec methods not to panic but rather return a Result or Option, and I see I can use the get and get_mut functions and then unwrap myself, but for code generic over these traits this does not help (much).

Precedence for implementing Index<I> and IndexMut<I> where I: SliceIndex<[T]> (even though the methods may panic):

  • (primitive) arrays
  • std::vec::Vec
  • smallvec::SmallVec (from the popular smallvec crate)
  • tinyvec::ArrayVec (from the tinyvec crate)

niluxv avatar Jul 14 '21 18:07 niluxv

Now panic or not has nothing to do with it, you can already index arrayvec because it derefs to slice. If possible in a good way, we should impl Index too. The complexity of slice's index impls could make them hard to match(?)

bluss avatar Jul 14 '21 21:07 bluss

you can already index arrayvec because it derefs to slice

Ah, didn't think of that. I created a PR which implements Index and IndexMut (also through).

niluxv avatar Jul 15 '21 08:07 niluxv