Skip to content

Commit 57af2e9

Browse files
committed
RFC for index traits
1 parent fee471d commit 57af2e9

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed

active/0000-index-traits.md

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
- Start Date: 2014-06-09
2+
- RFC PR #: (leave this empty)
3+
- Rust Issue #: #6515
4+
5+
# Summary
6+
7+
`Index` should be split into `Index`, `IndexMut`, and `IndexAssign`
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 three 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+
// self[element] = value
30+
trait IndexSet<E,V> {
31+
fn index_set(&mut self, element: E, value: V);
32+
}
33+
34+
# Drawbacks
35+
36+
* The number of lang. items increases.
37+
38+
* This design doesn't support moving out of a vector-like object.
39+
40+
# Alternatives
41+
42+
The impact of not doing this is that the `[]` notation will not be available to `Vec`.
43+
44+
# Unresolved questions
45+
46+
None that I'm aware of.

0 commit comments

Comments
 (0)