-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #111 from pcwalton/index-traits
RFC for index traits
- Loading branch information
Showing
1 changed file
with
43 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
- Start Date: 2014-06-09 | ||
- RFC PR #: #111 | ||
- Rust Issue #: #6515 | ||
|
||
# Summary | ||
|
||
`Index` should be split into `Index` and `IndexMut`. | ||
|
||
# Motivation | ||
|
||
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. | ||
|
||
Additionally, this simply follows the `Deref`/`DerefMut` split that has been implemented for a while. | ||
|
||
# Detailed design | ||
|
||
We split `Index` into two traits (borrowed from @nikomatsakis): | ||
|
||
// self[element] -- if used as rvalue, implicitly a deref of the result | ||
trait Index<E,R> { | ||
fn index<'a>(&'a self, element: &E) -> &'a R; | ||
} | ||
|
||
// &mut self[element] -- when used as a mutable lvalue | ||
trait IndexMut<E,R> { | ||
fn index_mut<'a>(&'a mut self, element: &E) -> &'a mut R; | ||
} | ||
|
||
# Drawbacks | ||
|
||
* The number of lang. items increases. | ||
|
||
* This design doesn't support moving out of a vector-like object. This can be added backwards compatibly. | ||
|
||
* This design doesn't support hash tables because there is no assignment operator. This can be added backwards compatibly. | ||
|
||
# Alternatives | ||
|
||
The impact of not doing this is that the `[]` notation will not be available to `Vec`. | ||
|
||
# Unresolved questions | ||
|
||
None that I'm aware of. |