]> git.lizzy.rs Git - rust.git/commitdiff
core: use assoc types in `Index[Mut]`
authorJorge Aparicio <japaricious@gmail.com>
Sat, 3 Jan 2015 14:46:29 +0000 (09:46 -0500)
committerJorge Aparicio <japaricious@gmail.com>
Sat, 3 Jan 2015 21:30:48 +0000 (16:30 -0500)
src/libcore/ops.rs
src/libcore/slice.rs

index 642376f1d8e94ca1b3cb034dd7276b304e413598..bef91dbd7604733c17a1841d248cd3131fa473a5 100644 (file)
@@ -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<Sized? Index, Sized? Result> 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<Foo, Foo> for Foo {
+/// impl Index<Foo> 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<Sized? Index, Sized? Result> for Sized? {
+pub trait Index<Sized? 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<Sized? Index, Sized? Result> 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<Sized? Index, Sized? Result> for Sized? {
 /// calling `index_mut`, and therefore, `main` prints `Indexing!`.
 ///
 /// ```
+/// #![feature(associated_types)]
+///
 /// use std::ops::IndexMut;
 ///
 /// #[deriving(Copy)]
 /// struct Foo;
 ///
-/// impl IndexMut<Foo, Foo> for Foo {
+/// impl IndexMut<Foo> 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<Sized? Index, Sized? Result> for Sized? {
 ///     &mut Foo[Foo];
 /// }
 /// ```
+#[cfg(not(stage0))]  // NOTE(stage0) remove cfg after a snapshot
 #[lang="index_mut"]
-pub trait IndexMut<Sized? Index, Sized? Result> for Sized? {
+pub trait IndexMut<Sized? Index> 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
index bc4e8d32887e2b458548c1bb4c36623396a3622a..d5810a382968b29a0c56bbe280a31d3db37b78b5 100644 (file)
@@ -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<T> ops::Index<uint, T> 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<T> ops::Index<uint> 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<T> ops::IndexMut<uint, T> 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<T> ops::IndexMut<uint> 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<T> ops::Slice<uint, [T]> for [T] {
     #[inline]
     fn as_slice_<'a>(&'a self) -> &'a [T] {