diff --git a/src/libcore/ops.rs b/src/libcore/ops.rs index ac735492be4a..5475eef9d47d 100644 --- a/src/libcore/ops.rs +++ b/src/libcore/ops.rs @@ -638,7 +638,7 @@ fn shr(&self, other: &uint) -> $t { (*self) >> (*other) } * ``` */ #[lang="index"] -pub trait Index { +pub trait Index 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 { * ``` */ #[lang="index_mut"] -pub trait IndexMut { +pub trait IndexMut 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 eaa52c99c4a2..138422ceff1c 100644 --- a/src/libcore/slice.rs +++ b/src/libcore/slice.rs @@ -256,7 +256,6 @@ pub trait SlicePrelude 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 @@ fn as_mut_ptr(&mut self) -> *mut T { } } +impl ops::Index for [T] { + fn index(&self, &index: &uint) -> &T { + assert!(index < self.len()); + + unsafe { mem::transmute(self.repr().data.offset(index as int)) } + } +} + +impl ops::IndexMut 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 ops::Slice for [T] { #[inline] fn as_slice_<'a>(&'a self) -> &'a [T] {