Implement direct usize indexing#132
Conversation
|
In my experiments in rustc, replacing |
|
One downside is that with my experiment of custom index types, I would ideally replace this |
|
I think we should just wait with this, in that case. If we have an indexing operator, IMO it must use the index type that we have parameterized the map with. |
|
I brought this up in the forum: https://users.rust-lang.org/t/any-tricks-for-generic-overlap-with-references/45186 I now think the answer is that we shouldn't try to create generic |
|
That makes sense but
Won't we have a trait bound on |
I think that's it. I shared a simplified playground, if you want to experiment with it: |
|
@bluss any more thoughts on this? My plan for custom index types is to just implement each manually, rather than trying to have a blanket impl that potentially conflicts with impl<K, V, S> Index<usize> for IndexMap<K, V, S, usize> {...}
impl<K, V, S> Index<u32> for IndexMap<K, V, S, u32> {...}
// perhaps all unsigned integers
// users can do their own `Index<NewTypeIndex>` |
```rust
where
IndexMap<K, V, S>: IndexMut<usize, Output = V>,
IndexSet<T, S>: Index<usize, Output = T>,
```
This allows `map[i]` and `set[i]` indexing to access values directly,
panicking if the index is out of bounds, similar to slices.
On maps, this somewhat overlaps with `Index<&Q> + IndexMut<&Q>` where
`Q: Equivalent<K>`. The reference makes this indexing unambiguous, but
it could be confusing to users if the key type is also an integer.
|
Let's go ahead with that plan then... |
where IndexMap<K, V, S>: IndexMut<usize, Output = V>, IndexSet<T, S>: Index<usize, Output = T>,This allows
map[i]andset[i]indexing to access values directly,panicking if the index is out of bounds, similar to slices.
On maps, this somewhat overlaps with
Index<&Q> + IndexMut<&Q>whereQ: Equivalent<K>. The reference makes this indexing unambiguous, butit could be confusing to users if the key type is also an integer.