Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement Index and IndexMut traits for ArrayVec #191

Open
wants to merge 5 commits into
base: master
Choose a base branch
from

Conversation

niluxv
Copy link

@niluxv niluxv commented Jul 15, 2021

I didn't have time to add tests to it yet, but this should implement Index<I> and IndexMut<I> where I: SliceIndex<[T]>.

Fixes #190

@niluxv
Copy link
Author

niluxv commented Jul 15, 2021

Instead of this implementation (self.get[_mut](index).unwrap()) we could also use

self.deref().index[_mut](index)

Maybe that gives better error messages in some situations? I don't know...

@bluss
Copy link
Owner

bluss commented Jul 19, 2021

Looks to be in the right direction, that's nice - I'll be a bit unreachable during the summer, but back in a bit

@niluxv niluxv force-pushed the index-indexmut-traits branch from df06b9d to 5b111f5 Compare July 24, 2021 08:00
@niluxv
Copy link
Author

niluxv commented Jul 24, 2021

No problem.

I changed the implementation as described above because it indeed gives much better error messages on out of bounds errors, and added some tests.

Copy link
Owner

@bluss bluss left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, yes not regressing the panic messages is certainly a requirement. One could also debate if I: SliceIndex or if [T]: Index<I> is the trait bound we should use (the latter being yet more general), but it seems fine as it is now.

We need the corresponding change for ArrayString too, but it can be a different PR.

src/arrayvec.rs Outdated Show resolved Hide resolved
src/arrayvec.rs Outdated Show resolved Hide resolved
@niluxv
Copy link
Author

niluxv commented Aug 29, 2021

So I changed the trait bounds as you suggested and just now added implementations to ArrayString analogous to those for ArrayVec.
I also removed the documentation I initially added to the Index/IndexMut implementation since the comment on panicing needn't be true for all instantiations of the generic parameter.

@bluss bluss added this to the 0.8 milestone Oct 28, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Feature request: Implement Index and IndexMut
2 participants