Skip to content

Commit c9c0fdb

Browse files
committed
Merge pull request #111 from pcwalton/index-traits
RFC for index traits
2 parents fee471d + 4ca734f commit c9c0fdb

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed

active/0034-index-traits.md

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
- Start Date: 2014-06-09
2+
- RFC PR #: #111
3+
- Rust Issue #: #6515
4+
5+
# Summary
6+
7+
`Index` should be split into `Index` and `IndexMut`.
8+
9+
# Motivation
10+
11+
Currently, the `Index` trait is not suitable for most array indexing tasks. The slice functionality cannot be replicated using it, and as a result the new `Vec` has to use `.get()` and `.get_mut()` methods.
12+
13+
Additionally, this simply follows the `Deref`/`DerefMut` split that has been implemented for a while.
14+
15+
# Detailed design
16+
17+
We split `Index` into two traits (borrowed from @nikomatsakis):
18+
19+
// self[element] -- if used as rvalue, implicitly a deref of the result
20+
trait Index<E,R> {
21+
fn index<'a>(&'a self, element: &E) -> &'a R;
22+
}
23+
24+
// &mut self[element] -- when used as a mutable lvalue
25+
trait IndexMut<E,R> {
26+
fn index_mut<'a>(&'a mut self, element: &E) -> &'a mut R;
27+
}
28+
29+
# Drawbacks
30+
31+
* The number of lang. items increases.
32+
33+
* This design doesn't support moving out of a vector-like object. This can be added backwards compatibly.
34+
35+
* This design doesn't support hash tables because there is no assignment operator. This can be added backwards compatibly.
36+
37+
# Alternatives
38+
39+
The impact of not doing this is that the `[]` notation will not be available to `Vec`.
40+
41+
# Unresolved questions
42+
43+
None that I'm aware of.

0 commit comments

Comments
 (0)