]> git.lizzy.rs Git - rust.git/commitdiff
make `IndexMut` a super trait over `Index`
authorJorge Aparicio <japaricious@gmail.com>
Wed, 4 Feb 2015 23:00:12 +0000 (18:00 -0500)
committerJorge Aparicio <japaricious@gmail.com>
Sat, 7 Feb 2015 02:11:59 +0000 (21:11 -0500)
closes #21630

12 files changed:
src/libcollections/btree/map.rs
src/libcollections/ring_buf.rs
src/libcollections/vec.rs
src/libcollections/vec_map.rs
src/libcore/ops.rs
src/libcore/slice.rs
src/libstd/collections/hash/map.rs
src/test/compile-fail/borrowck-overloaded-index-autoderef.rs
src/test/compile-fail/borrowck-overloaded-index.rs
src/test/run-pass/overloaded-index-autoderef.rs
src/test/run-pass/overloaded-index.rs
src/test/run-pass/slice.rs

index 1b6057d4c728660d1e046776ea5718c08064e854..aec50d5380880e24891e760d5247a3fbaad862f1 100644 (file)
@@ -910,8 +910,6 @@ fn index(&self, key: &Q) -> &V {
 impl<K: Ord, Q: ?Sized, V> IndexMut<Q> for BTreeMap<K, V>
     where Q: BorrowFrom<K> + Ord
 {
-    type Output = V;
-
     fn index_mut(&mut self, key: &Q) -> &mut V {
         self.get_mut(key).expect("no entry found for key")
     }
index f5df7018153c4a039955460a061e122ae2405183..417493038404a11ecb4d59526c1fe308a2cdc12b 100644 (file)
@@ -1591,8 +1591,6 @@ fn index(&self, i: &usize) -> &A {
 
 #[stable(feature = "rust1", since = "1.0.0")]
 impl<A> IndexMut<usize> for RingBuf<A> {
-    type Output = A;
-
     #[inline]
     fn index_mut(&mut self, i: &usize) -> &mut A {
         self.get_mut(*i).expect("Out of bounds access")
index 62640e1e25032274d99c0fc965c290ac24ebdec2..4a082c3616cb91bbfe2d5d1e2332f201f5f4ecbb 100644 (file)
@@ -1286,8 +1286,6 @@ fn index(&self, index: &usize) -> &T {
 
 #[stable(feature = "rust1", since = "1.0.0")]
 impl<T> IndexMut<usize> for Vec<T> {
-    type Output = T;
-
     #[inline]
     fn index_mut(&mut self, index: &usize) -> &mut T {
         // NB built-in indexing via `&mut [T]`
@@ -1331,7 +1329,6 @@ fn index(&self, _index: &ops::RangeFull) -> &[T] {
 
 #[stable(feature = "rust1", since = "1.0.0")]
 impl<T> ops::IndexMut<ops::Range<usize>> for Vec<T> {
-    type Output = [T];
     #[inline]
     fn index_mut(&mut self, index: &ops::Range<usize>) -> &mut [T] {
         IndexMut::index_mut(&mut **self, index)
@@ -1339,7 +1336,6 @@ fn index_mut(&mut self, index: &ops::Range<usize>) -> &mut [T] {
 }
 #[stable(feature = "rust1", since = "1.0.0")]
 impl<T> ops::IndexMut<ops::RangeTo<usize>> for Vec<T> {
-    type Output = [T];
     #[inline]
     fn index_mut(&mut self, index: &ops::RangeTo<usize>) -> &mut [T] {
         IndexMut::index_mut(&mut **self, index)
@@ -1347,7 +1343,6 @@ fn index_mut(&mut self, index: &ops::RangeTo<usize>) -> &mut [T] {
 }
 #[stable(feature = "rust1", since = "1.0.0")]
 impl<T> ops::IndexMut<ops::RangeFrom<usize>> for Vec<T> {
-    type Output = [T];
     #[inline]
     fn index_mut(&mut self, index: &ops::RangeFrom<usize>) -> &mut [T] {
         IndexMut::index_mut(&mut **self, index)
@@ -1355,7 +1350,6 @@ fn index_mut(&mut self, index: &ops::RangeFrom<usize>) -> &mut [T] {
 }
 #[stable(feature = "rust1", since = "1.0.0")]
 impl<T> ops::IndexMut<ops::RangeFull> for Vec<T> {
-    type Output = [T];
     #[inline]
     fn index_mut(&mut self, _index: &ops::RangeFull) -> &mut [T] {
         self.as_mut_slice()
index 65592d138c72c63758fd6028db83c3e249529908..739b8d8ce19c24899abcd9d06f2a25d62d848623 100644 (file)
@@ -712,8 +712,6 @@ fn index<'a>(&'a self, i: &usize) -> &'a V {
 
 #[stable(feature = "rust1", since = "1.0.0")]
 impl<V> IndexMut<usize> for VecMap<V> {
-    type Output = V;
-
     #[inline]
     fn index_mut<'a>(&'a mut self, i: &usize) -> &'a mut V {
         self.get_mut(i).expect("key not present")
index 7af94c73f324d80324fdacec9a44edd95df61698..782483a34fc89744465e4f09bd47c08a4c229b49 100644 (file)
@@ -897,14 +897,14 @@ macro_rules! shr_impl_all {
 /// }
 /// ```
 #[lang="index"]
-#[rustc_on_unimplemented = "the type `{Self}` cannot be indexed by `{Index}`"]
+#[rustc_on_unimplemented = "the type `{Self}` cannot be indexed by `{Idx}`"]
 #[stable(feature = "rust1", since = "1.0.0")]
-pub trait Index<Index: ?Sized> {
+pub trait Index<Idx: ?Sized> {
     type Output: ?Sized;
 
     /// The method for the indexing (`Foo[Bar]`) operation
     #[stable(feature = "rust1", since = "1.0.0")]
-    fn index<'a>(&'a self, index: &Index) -> &'a Self::Output;
+    fn index<'a>(&'a self, index: &Idx) -> &'a Self::Output;
 }
 
 /// The `IndexMut` trait is used to specify the functionality of indexing
@@ -916,15 +916,21 @@ pub trait Index<Index: ?Sized> {
 /// calling `index_mut`, and therefore, `main` prints `Indexing!`.
 ///
 /// ```
-/// use std::ops::IndexMut;
+/// use std::ops::{Index, IndexMut};
 ///
 /// #[derive(Copy)]
 /// struct Foo;
 /// struct Bar;
 ///
-/// impl IndexMut<Bar> for Foo {
+/// impl Index<Bar> for Foo {
 ///     type Output = Foo;
 ///
+///     fn index<'a>(&'a self, _index: &Bar) -> &'a Foo {
+///         self
+///     }
+/// }
+///
+/// impl IndexMut<Bar> for Foo {
 ///     fn index_mut<'a>(&'a mut self, _index: &Bar) -> &'a mut Foo {
 ///         println!("Indexing!");
 ///         self
@@ -936,14 +942,12 @@ pub trait Index<Index: ?Sized> {
 /// }
 /// ```
 #[lang="index_mut"]
-#[rustc_on_unimplemented = "the type `{Self}` cannot be mutably indexed by `{Index}`"]
+#[rustc_on_unimplemented = "the type `{Self}` cannot be mutably indexed by `{Idx}`"]
 #[stable(feature = "rust1", since = "1.0.0")]
-pub trait IndexMut<Index: ?Sized> {
-    type Output: ?Sized;
-
+pub trait IndexMut<Idx: ?Sized>: Index<Idx> {
     /// The method for the indexing (`Foo[Bar]`) operation
     #[stable(feature = "rust1", since = "1.0.0")]
-    fn index_mut<'a>(&'a mut self, index: &Index) -> &'a mut Self::Output;
+    fn index_mut<'a>(&'a mut self, index: &Idx) -> &'a mut Self::Output;
 }
 
 /// An unbounded range.
index c622baa0cc22dbedef32747ddf377002cf74e63e..fc51920ec6b82de1941863fbb4a48849f69d709e 100644 (file)
@@ -502,8 +502,6 @@ fn index(&self, &index: &uint) -> &T {
 
 #[stable(feature = "rust1", since = "1.0.0")]
 impl<T> ops::IndexMut<uint> for [T] {
-    type Output = T;
-
     fn index_mut(&mut self, &index: &uint) -> &mut T {
         assert!(index < self.len());
 
@@ -553,7 +551,6 @@ fn index(&self, _index: &RangeFull) -> &[T] {
 
 #[stable(feature = "rust1", since = "1.0.0")]
 impl<T> ops::IndexMut<ops::Range<uint>> for [T] {
-    type Output = [T];
     #[inline]
     fn index_mut(&mut self, index: &ops::Range<uint>) -> &mut [T] {
         assert!(index.start <= index.end);
@@ -568,7 +565,6 @@ fn index_mut(&mut self, index: &ops::Range<uint>) -> &mut [T] {
 }
 #[stable(feature = "rust1", since = "1.0.0")]
 impl<T> ops::IndexMut<ops::RangeTo<uint>> for [T] {
-    type Output = [T];
     #[inline]
     fn index_mut(&mut self, index: &ops::RangeTo<uint>) -> &mut [T] {
         self.index_mut(&ops::Range{ start: 0, end: index.end })
@@ -576,7 +572,6 @@ fn index_mut(&mut self, index: &ops::RangeTo<uint>) -> &mut [T] {
 }
 #[stable(feature = "rust1", since = "1.0.0")]
 impl<T> ops::IndexMut<ops::RangeFrom<uint>> for [T] {
-    type Output = [T];
     #[inline]
     fn index_mut(&mut self, index: &ops::RangeFrom<uint>) -> &mut [T] {
         let len = self.len();
@@ -585,7 +580,6 @@ fn index_mut(&mut self, index: &ops::RangeFrom<uint>) -> &mut [T] {
 }
 #[stable(feature = "rust1", since = "1.0.0")]
 impl<T> ops::IndexMut<RangeFull> for [T] {
-    type Output = [T];
     #[inline]
     fn index_mut(&mut self, _index: &RangeFull) -> &mut [T] {
         self
@@ -865,7 +859,6 @@ fn index(&self, _index: &RangeFull) -> &[T] {
 
 #[unstable(feature = "core")]
 impl<'a, T> ops::IndexMut<ops::Range<uint>> for IterMut<'a, T> {
-    type Output = [T];
     #[inline]
     fn index_mut(&mut self, index: &ops::Range<uint>) -> &mut [T] {
         self.index_mut(&RangeFull).index_mut(index)
@@ -873,7 +866,6 @@ fn index_mut(&mut self, index: &ops::Range<uint>) -> &mut [T] {
 }
 #[unstable(feature = "core")]
 impl<'a, T> ops::IndexMut<ops::RangeTo<uint>> for IterMut<'a, T> {
-    type Output = [T];
     #[inline]
     fn index_mut(&mut self, index: &ops::RangeTo<uint>) -> &mut [T] {
         self.index_mut(&RangeFull).index_mut(index)
@@ -881,7 +873,6 @@ fn index_mut(&mut self, index: &ops::RangeTo<uint>) -> &mut [T] {
 }
 #[unstable(feature = "core")]
 impl<'a, T> ops::IndexMut<ops::RangeFrom<uint>> for IterMut<'a, T> {
-    type Output = [T];
     #[inline]
     fn index_mut(&mut self, index: &ops::RangeFrom<uint>) -> &mut [T] {
         self.index_mut(&RangeFull).index_mut(index)
@@ -889,7 +880,6 @@ fn index_mut(&mut self, index: &ops::RangeFrom<uint>) -> &mut [T] {
 }
 #[unstable(feature = "core")]
 impl<'a, T> ops::IndexMut<RangeFull> for IterMut<'a, T> {
-    type Output = [T];
     #[inline]
     fn index_mut(&mut self, _index: &RangeFull) -> &mut [T] {
         make_slice!(T => &mut [T]: self.ptr, self.end)
index aec9446773f2ac5e5e46f2d9371cce50b0089beb..710f021d9125e6205e1149d2fe8ccc12da1ff697 100644 (file)
@@ -1267,8 +1267,6 @@ impl<K, V, S, H, Q: ?Sized> IndexMut<Q> for HashMap<K, V, S>
           S: HashState<Hasher=H>,
           H: hash::Hasher<Output=u64>
 {
-    type Output = V;
-
     #[inline]
     fn index_mut<'a>(&'a mut self, index: &Q) -> &'a mut V {
         self.get_mut(index).expect("no entry found for key")
index 977c67b1c7dbd073b03c2f53b238010433c982b9..99f396ef8143213af415dbca2a649d0b4e1cd167 100644 (file)
@@ -31,8 +31,6 @@ fn index<'a>(&'a self, z: &String) -> &'a isize {
 }
 
 impl IndexMut<String> for Foo {
-    type Output = isize;
-
     fn index_mut<'a>(&'a mut self, z: &String) -> &'a mut isize {
         if *z == "x" {
             &mut self.x
index 9e79154eb0c5cde893bf917e93551baef80b11c4..2d752abe7e3c293a1ccc443bf378581786c635dd 100644 (file)
@@ -28,8 +28,6 @@ fn index<'a>(&'a self, z: &String) -> &'a isize {
 }
 
 impl IndexMut<String> for Foo {
-    type Output = isize;
-
     fn index_mut<'a>(&'a mut self, z: &String) -> &'a mut isize {
         if *z == "x" {
             &mut self.x
index 637d2c94694455d84aa64eccca52791000e13061..d5ccf8cd2befb0f4f18e29fc0021ba49f4d814bf 100644 (file)
@@ -33,8 +33,6 @@ fn index(&self, z: &int) -> &int {
 }
 
 impl IndexMut<int> for Foo {
-    type Output = int;
-
     fn index_mut(&mut self, z: &int) -> &mut int {
         if *z == 0 {
             &mut self.x
index 0afdb24a81cc0dbf64dd86b437faeb4af54fa4bd..10ca3804eaedbc187ab128e619f45d20cd7c41dd 100644 (file)
@@ -28,8 +28,6 @@ fn index(&self, z: &int) -> &int {
 }
 
 impl IndexMut<int> for Foo {
-    type Output = int;
-
     fn index_mut(&mut self, z: &int) -> &mut int {
         if *z == 0 {
             &mut self.x
index 59fb24ffc02cd405ded5acb56abf80f63c1348ae..30b53dbb0ad5bea0bdf96b7ddb419345e2749ad6 100644 (file)
@@ -49,28 +49,24 @@ fn index(&self, _index: &RangeFull) -> &Foo {
 }
 
 impl IndexMut<Range<Foo>> for Foo {
-    type Output = Foo;
     fn index_mut(&mut self, index: &Range<Foo>) -> &mut Foo {
         unsafe { COUNT += 1; }
         self
     }
 }
 impl IndexMut<RangeTo<Foo>> for Foo {
-    type Output = Foo;
     fn index_mut(&mut self, index: &RangeTo<Foo>) -> &mut Foo {
         unsafe { COUNT += 1; }
         self
     }
 }
 impl IndexMut<RangeFrom<Foo>> for Foo {
-    type Output = Foo;
     fn index_mut(&mut self, index: &RangeFrom<Foo>) -> &mut Foo {
         unsafe { COUNT += 1; }
         self
     }
 }
 impl IndexMut<RangeFull> for Foo {
-    type Output = Foo;
     fn index_mut(&mut self, _index: &RangeFull) -> &mut Foo {
         unsafe { COUNT += 1; }
         self