Skip to content

Commit 234dc4d

Browse files
author
Jorge Aparicio
committed
core: use assoc types in Index[Mut]
1 parent fc34330 commit 234dc4d

File tree

2 files changed

+64
-6
lines changed

2 files changed

+64
-6
lines changed

src/libcore/ops.rs

Lines changed: 38 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -717,6 +717,15 @@ macro_rules! shr_impl {
717717

718718
shr_impl! { uint u8 u16 u32 u64 int i8 i16 i32 i64 }
719719

720+
// NOTE(stage0) remove trait after a snapshot
721+
#[cfg(stage0)]
722+
#[allow(missing_docs)]
723+
#[lang="index"]
724+
pub trait Index<Sized? Index, Sized? Result> for Sized? {
725+
/// The method for the indexing (`Foo[Bar]`) operation
726+
fn index<'a>(&'a self, index: &Index) -> &'a Result;
727+
}
728+
720729
/// The `Index` trait is used to specify the functionality of indexing operations
721730
/// like `arr[idx]` when used in an immutable context.
722731
///
@@ -726,12 +735,16 @@ shr_impl! { uint u8 u16 u32 u64 int i8 i16 i32 i64 }
726735
/// calling `index`, and therefore, `main` prints `Indexing!`.
727736
///
728737
/// ```
738+
/// #![feature(associated_types)]
739+
///
729740
/// use std::ops::Index;
730741
///
731742
/// #[deriving(Copy)]
732743
/// struct Foo;
733744
///
734-
/// impl Index<Foo, Foo> for Foo {
745+
/// impl Index<Foo> for Foo {
746+
/// type Output = Foo;
747+
///
735748
/// fn index<'a>(&'a self, _index: &Foo) -> &'a Foo {
736749
/// println!("Indexing!");
737750
/// self
@@ -742,10 +755,22 @@ shr_impl! { uint u8 u16 u32 u64 int i8 i16 i32 i64 }
742755
/// Foo[Foo];
743756
/// }
744757
/// ```
758+
#[cfg(not(stage0))] // NOTE(stage0) remove cfg after a snapshot
745759
#[lang="index"]
746-
pub trait Index<Sized? Index, Sized? Result> for Sized? {
760+
pub trait Index<Sized? Index> for Sized? {
761+
type Sized? Output;
762+
747763
/// The method for the indexing (`Foo[Bar]`) operation
748-
fn index<'a>(&'a self, index: &Index) -> &'a Result;
764+
fn index<'a>(&'a self, index: &Index) -> &'a Self::Output;
765+
}
766+
767+
// NOTE(stage0) remove trait after a snapshot
768+
#[cfg(stage0)]
769+
#[allow(missing_docs)]
770+
#[lang="index_mut"]
771+
pub trait IndexMut<Sized? Index, Sized? Result> for Sized? {
772+
/// The method for the indexing (`Foo[Bar]`) operation
773+
fn index_mut<'a>(&'a mut self, index: &Index) -> &'a mut Result;
749774
}
750775

751776
/// The `IndexMut` trait is used to specify the functionality of indexing
@@ -757,12 +782,16 @@ pub trait Index<Sized? Index, Sized? Result> for Sized? {
757782
/// calling `index_mut`, and therefore, `main` prints `Indexing!`.
758783
///
759784
/// ```
785+
/// #![feature(associated_types)]
786+
///
760787
/// use std::ops::IndexMut;
761788
///
762789
/// #[deriving(Copy)]
763790
/// struct Foo;
764791
///
765-
/// impl IndexMut<Foo, Foo> for Foo {
792+
/// impl IndexMut<Foo> for Foo {
793+
/// type Output = Foo;
794+
///
766795
/// fn index_mut<'a>(&'a mut self, _index: &Foo) -> &'a mut Foo {
767796
/// println!("Indexing!");
768797
/// self
@@ -773,10 +802,13 @@ pub trait Index<Sized? Index, Sized? Result> for Sized? {
773802
/// &mut Foo[Foo];
774803
/// }
775804
/// ```
805+
#[cfg(not(stage0))] // NOTE(stage0) remove cfg after a snapshot
776806
#[lang="index_mut"]
777-
pub trait IndexMut<Sized? Index, Sized? Result> for Sized? {
807+
pub trait IndexMut<Sized? Index> for Sized? {
808+
type Sized? Output;
809+
778810
/// The method for the indexing (`Foo[Bar]`) operation
779-
fn index_mut<'a>(&'a mut self, index: &Index) -> &'a mut Result;
811+
fn index_mut<'a>(&'a mut self, index: &Index) -> &'a mut Self::Output;
780812
}
781813

782814
/// The `Slice` trait is used to specify the functionality of slicing operations

src/libcore/slice.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -531,6 +531,8 @@ impl<T> SliceExt for [T] {
531531
}
532532
}
533533

534+
// NOTE(stage0) remove impl after a snapshot
535+
#[cfg(stage0)]
534536
impl<T> ops::Index<uint, T> for [T] {
535537
fn index(&self, &index: &uint) -> &T {
536538
assert!(index < self.len());
@@ -539,6 +541,19 @@ impl<T> ops::Index<uint, T> for [T] {
539541
}
540542
}
541543

544+
#[cfg(not(stage0))] // NOTE(stage0) remove cfg after a snapshot
545+
impl<T> ops::Index<uint> for [T] {
546+
type Output = T;
547+
548+
fn index(&self, &index: &uint) -> &T {
549+
assert!(index < self.len());
550+
551+
unsafe { mem::transmute(self.repr().data.offset(index as int)) }
552+
}
553+
}
554+
555+
// NOTE(stage0) remove impl after a snapshot
556+
#[cfg(stage0)]
542557
impl<T> ops::IndexMut<uint, T> for [T] {
543558
fn index_mut(&mut self, &index: &uint) -> &mut T {
544559
assert!(index < self.len());
@@ -547,6 +562,17 @@ impl<T> ops::IndexMut<uint, T> for [T] {
547562
}
548563
}
549564

565+
#[cfg(not(stage0))] // NOTE(stage0) remove cfg after a snapshot
566+
impl<T> ops::IndexMut<uint> for [T] {
567+
type Output = T;
568+
569+
fn index_mut(&mut self, &index: &uint) -> &mut T {
570+
assert!(index < self.len());
571+
572+
unsafe { mem::transmute(self.repr().data.offset(index as int)) }
573+
}
574+
}
575+
550576
impl<T> ops::Slice<uint, [T]> for [T] {
551577
#[inline]
552578
fn as_slice_<'a>(&'a self) -> &'a [T] {

0 commit comments

Comments
 (0)