about summary refs log tree commit diff
diff options
context:
space:
mode:
authorJorge Aparicio <japaricious@gmail.com>2014-11-05 15:44:49 -0500
committerJorge Aparicio <japaricious@gmail.com>2014-11-06 21:11:19 -0500
commitf7c1771fd18672a148979d334bf732d15a2c4023 (patch)
treeadc3bb5f4752e9292064f7f17aed77ad7710283c
parent45cbdec4174778bf915f17561ef971c068a7fcbc (diff)
downloadrust-f7c1771fd18672a148979d334bf732d15a2c4023.tar.gz
rust-f7c1771fd18672a148979d334bf732d15a2c4023.zip
Implement Index/IndexMut for [T]
Closes #16529
-rw-r--r--src/libcore/ops.rs4
-rw-r--r--src/libcore/slice.rs17
2 files changed, 18 insertions, 3 deletions
diff --git a/src/libcore/ops.rs b/src/libcore/ops.rs
index ac735492be4..5475eef9d47 100644
--- a/src/libcore/ops.rs
+++ b/src/libcore/ops.rs
@@ -638,7 +638,7 @@ shr_impl!(uint u8 u16 u32 u64 int i8 i16 i32 i64)
  * ```
  */
 #[lang="index"]
-pub trait Index<Index, Sized? Result> {
+pub trait Index<Index, Sized? Result> for Sized? {
     /// The method for the indexing (`Foo[Bar]`) operation
     fn index<'a>(&'a self, index: &Index) -> &'a Result;
 }
@@ -669,7 +669,7 @@ pub trait Index<Index, Sized? Result> {
  * ```
  */
 #[lang="index_mut"]
-pub trait IndexMut<Index, Result> {
+pub trait IndexMut<Index, Result> for Sized? {
     /// The method for the indexing (`Foo[Bar]`) operation
     fn index_mut<'a>(&'a mut self, index: &Index) -> &'a mut Result;
 }
diff --git a/src/libcore/slice.rs b/src/libcore/slice.rs
index eaa52c99c4a..138422ceff1 100644
--- a/src/libcore/slice.rs
+++ b/src/libcore/slice.rs
@@ -256,7 +256,6 @@ pub trait SlicePrelude<T> for Sized? {
     #[inline]
     #[experimental = "not triaged yet"]
     fn is_empty(&self) -> bool { self.len() == 0 }
-
     /// Returns a mutable reference to the element at the given index,
     /// or `None` if the index is out of bounds
     #[unstable = "waiting on final error conventions"]
@@ -698,6 +697,22 @@ impl<T> SlicePrelude<T> for [T] {
     }
 }
 
+impl<T> ops::Index<uint, T> for [T] {
+    fn index(&self, &index: &uint) -> &T {
+        assert!(index < self.len());
+
+        unsafe { mem::transmute(self.repr().data.offset(index as int)) }
+    }
+}
+
+impl<T> ops::IndexMut<uint, T> for [T] {
+    fn index_mut(&mut self, &index: &uint) -> &mut T {
+        assert!(index < self.len());
+
+        unsafe { mem::transmute(self.repr().data.offset(index as int)) }
+    }
+}
+
 impl<T> ops::Slice<uint, [T]> for [T] {
     #[inline]
     fn as_slice_<'a>(&'a self) -> &'a [T] {