From 234dc4d4ddc19466ec3393210c2949d8ca0eba41 Mon Sep 17 00:00:00 2001 From: Jorge Aparicio Date: Sat, 3 Jan 2015 09:46:29 -0500 Subject: [PATCH] core: use assoc types in `Index[Mut]` --- src/libcore/ops.rs | 44 ++++++++++++++++++++++++++++++++++++++------ src/libcore/slice.rs | 26 ++++++++++++++++++++++++++ 2 files changed, 64 insertions(+), 6 deletions(-) diff --git a/src/libcore/ops.rs b/src/libcore/ops.rs index 642376f1d8e..bef91dbd760 100644 --- a/src/libcore/ops.rs +++ b/src/libcore/ops.rs @@ -717,6 +717,15 @@ fn shr(self, other: uint) -> $t { self >> other } shr_impl! { uint u8 u16 u32 u64 int i8 i16 i32 i64 } +// NOTE(stage0) remove trait after a snapshot +#[cfg(stage0)] +#[allow(missing_docs)] +#[lang="index"] +pub trait Index for Sized? { + /// The method for the indexing (`Foo[Bar]`) operation + fn index<'a>(&'a self, index: &Index) -> &'a Result; +} + /// The `Index` trait is used to specify the functionality of indexing operations /// like `arr[idx]` when used in an immutable context. /// @@ -726,12 +735,16 @@ fn shr(self, other: uint) -> $t { self >> other } /// calling `index`, and therefore, `main` prints `Indexing!`. /// /// ``` +/// #![feature(associated_types)] +/// /// use std::ops::Index; /// /// #[deriving(Copy)] /// struct Foo; /// -/// impl Index for Foo { +/// impl Index for Foo { +/// type Output = Foo; +/// /// fn index<'a>(&'a self, _index: &Foo) -> &'a Foo { /// println!("Indexing!"); /// self @@ -742,10 +755,22 @@ fn shr(self, other: uint) -> $t { self >> other } /// Foo[Foo]; /// } /// ``` +#[cfg(not(stage0))] // NOTE(stage0) remove cfg after a snapshot #[lang="index"] -pub trait Index for Sized? { +pub trait Index for Sized? { + type Sized? Output; + /// The method for the indexing (`Foo[Bar]`) operation - fn index<'a>(&'a self, index: &Index) -> &'a Result; + fn index<'a>(&'a self, index: &Index) -> &'a Self::Output; +} + +// NOTE(stage0) remove trait after a snapshot +#[cfg(stage0)] +#[allow(missing_docs)] +#[lang="index_mut"] +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; } /// The `IndexMut` trait is used to specify the functionality of indexing @@ -757,12 +782,16 @@ pub trait Index for Sized? { /// calling `index_mut`, and therefore, `main` prints `Indexing!`. /// /// ``` +/// #![feature(associated_types)] +/// /// use std::ops::IndexMut; /// /// #[deriving(Copy)] /// struct Foo; /// -/// impl IndexMut for Foo { +/// impl IndexMut for Foo { +/// type Output = Foo; +/// /// fn index_mut<'a>(&'a mut self, _index: &Foo) -> &'a mut Foo { /// println!("Indexing!"); /// self @@ -773,10 +802,13 @@ pub trait Index for Sized? { /// &mut Foo[Foo]; /// } /// ``` +#[cfg(not(stage0))] // NOTE(stage0) remove cfg after a snapshot #[lang="index_mut"] -pub trait IndexMut for Sized? { +pub trait IndexMut for Sized? { + type Sized? Output; + /// The method for the indexing (`Foo[Bar]`) operation - fn index_mut<'a>(&'a mut self, index: &Index) -> &'a mut Result; + fn index_mut<'a>(&'a mut self, index: &Index) -> &'a mut Self::Output; } /// The `Slice` trait is used to specify the functionality of slicing operations diff --git a/src/libcore/slice.rs b/src/libcore/slice.rs index bc4e8d32887..d5810a38296 100644 --- a/src/libcore/slice.rs +++ b/src/libcore/slice.rs @@ -531,6 +531,8 @@ fn clone_from_slice(&mut self, src: &[T]) -> uint where T: Clone { } } +// NOTE(stage0) remove impl after a snapshot +#[cfg(stage0)] impl ops::Index for [T] { fn index(&self, &index: &uint) -> &T { assert!(index < self.len()); @@ -539,6 +541,19 @@ fn index(&self, &index: &uint) -> &T { } } +#[cfg(not(stage0))] // NOTE(stage0) remove cfg after a snapshot +impl ops::Index for [T] { + type Output = T; + + fn index(&self, &index: &uint) -> &T { + assert!(index < self.len()); + + unsafe { mem::transmute(self.repr().data.offset(index as int)) } + } +} + +// NOTE(stage0) remove impl after a snapshot +#[cfg(stage0)] impl ops::IndexMut for [T] { fn index_mut(&mut self, &index: &uint) -> &mut T { assert!(index < self.len()); @@ -547,6 +562,17 @@ fn index_mut(&mut self, &index: &uint) -> &mut T { } } +#[cfg(not(stage0))] // NOTE(stage0) remove cfg after a snapshot +impl ops::IndexMut for [T] { + type Output = 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] { -- 2.44.0