Skip to content

Commit f7c1771

Browse files
author
Jorge Aparicio
committed
Implement Index/IndexMut for [T]
Closes #16529
1 parent 45cbdec commit f7c1771

File tree

2 files changed

+18
-3
lines changed

2 files changed

+18
-3
lines changed

src/libcore/ops.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -638,7 +638,7 @@ shr_impl!(uint u8 u16 u32 u64 int i8 i16 i32 i64)
638638
* ```
639639
*/
640640
#[lang="index"]
641-
pub trait Index<Index, Sized? Result> {
641+
pub trait Index<Index, Sized? Result> for Sized? {
642642
/// The method for the indexing (`Foo[Bar]`) operation
643643
fn index<'a>(&'a self, index: &Index) -> &'a Result;
644644
}
@@ -669,7 +669,7 @@ pub trait Index<Index, Sized? Result> {
669669
* ```
670670
*/
671671
#[lang="index_mut"]
672-
pub trait IndexMut<Index, Result> {
672+
pub trait IndexMut<Index, Result> for Sized? {
673673
/// The method for the indexing (`Foo[Bar]`) operation
674674
fn index_mut<'a>(&'a mut self, index: &Index) -> &'a mut Result;
675675
}

src/libcore/slice.rs

+16-1
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,6 @@ pub trait SlicePrelude<T> for Sized? {
256256
#[inline]
257257
#[experimental = "not triaged yet"]
258258
fn is_empty(&self) -> bool { self.len() == 0 }
259-
260259
/// Returns a mutable reference to the element at the given index,
261260
/// or `None` if the index is out of bounds
262261
#[unstable = "waiting on final error conventions"]
@@ -698,6 +697,22 @@ impl<T> SlicePrelude<T> for [T] {
698697
}
699698
}
700699

700+
impl<T> ops::Index<uint, T> for [T] {
701+
fn index(&self, &index: &uint) -> &T {
702+
assert!(index < self.len());
703+
704+
unsafe { mem::transmute(self.repr().data.offset(index as int)) }
705+
}
706+
}
707+
708+
impl<T> ops::IndexMut<uint, T> for [T] {
709+
fn index_mut(&mut self, &index: &uint) -> &mut T {
710+
assert!(index < self.len());
711+
712+
unsafe { mem::transmute(self.repr().data.offset(index as int)) }
713+
}
714+
}
715+
701716
impl<T> ops::Slice<uint, [T]> for [T] {
702717
#[inline]
703718
fn as_slice_<'a>(&'a self) -> &'a [T] {

0 commit comments

Comments
 (0)