From 724bf7bce2b35ac5731cf7e217c0e87916517b69 Mon Sep 17 00:00:00 2001 From: Jorge Aparicio Date: Wed, 4 Feb 2015 18:00:12 -0500 Subject: [PATCH] make `IndexMut` a super trait over `Index` closes #21630 --- src/libcollections/btree/map.rs | 2 -- src/libcollections/ring_buf.rs | 2 -- src/libcollections/vec.rs | 6 ----- src/libcollections/vec_map.rs | 2 -- src/libcore/ops.rs | 24 +++++++++++-------- src/libcore/slice.rs | 10 -------- src/libstd/collections/hash/map.rs | 2 -- .../borrowck-overloaded-index-autoderef.rs | 2 -- .../compile-fail/borrowck-overloaded-index.rs | 2 -- .../run-pass/overloaded-index-autoderef.rs | 2 -- src/test/run-pass/overloaded-index.rs | 2 -- src/test/run-pass/slice.rs | 4 ---- 12 files changed, 14 insertions(+), 46 deletions(-) diff --git a/src/libcollections/btree/map.rs b/src/libcollections/btree/map.rs index 1b6057d4c72..aec50d53808 100644 --- a/src/libcollections/btree/map.rs +++ b/src/libcollections/btree/map.rs @@ -910,8 +910,6 @@ impl Index for BTreeMap impl IndexMut for BTreeMap where Q: BorrowFrom + Ord { - type Output = V; - fn index_mut(&mut self, key: &Q) -> &mut V { self.get_mut(key).expect("no entry found for key") } diff --git a/src/libcollections/ring_buf.rs b/src/libcollections/ring_buf.rs index f5df7018153..41749303840 100644 --- a/src/libcollections/ring_buf.rs +++ b/src/libcollections/ring_buf.rs @@ -1591,8 +1591,6 @@ impl Index for RingBuf { #[stable(feature = "rust1", since = "1.0.0")] impl IndexMut for RingBuf { - type Output = A; - #[inline] fn index_mut(&mut self, i: &usize) -> &mut A { self.get_mut(*i).expect("Out of bounds access") diff --git a/src/libcollections/vec.rs b/src/libcollections/vec.rs index 62640e1e250..4a082c3616c 100644 --- a/src/libcollections/vec.rs +++ b/src/libcollections/vec.rs @@ -1286,8 +1286,6 @@ impl Index for Vec { #[stable(feature = "rust1", since = "1.0.0")] impl IndexMut for Vec { - type Output = T; - #[inline] fn index_mut(&mut self, index: &usize) -> &mut T { // NB built-in indexing via `&mut [T]` @@ -1331,7 +1329,6 @@ impl ops::Index for Vec { #[stable(feature = "rust1", since = "1.0.0")] impl ops::IndexMut> for Vec { - type Output = [T]; #[inline] fn index_mut(&mut self, index: &ops::Range) -> &mut [T] { IndexMut::index_mut(&mut **self, index) @@ -1339,7 +1336,6 @@ impl ops::IndexMut> for Vec { } #[stable(feature = "rust1", since = "1.0.0")] impl ops::IndexMut> for Vec { - type Output = [T]; #[inline] fn index_mut(&mut self, index: &ops::RangeTo) -> &mut [T] { IndexMut::index_mut(&mut **self, index) @@ -1347,7 +1343,6 @@ impl ops::IndexMut> for Vec { } #[stable(feature = "rust1", since = "1.0.0")] impl ops::IndexMut> for Vec { - type Output = [T]; #[inline] fn index_mut(&mut self, index: &ops::RangeFrom) -> &mut [T] { IndexMut::index_mut(&mut **self, index) @@ -1355,7 +1350,6 @@ impl ops::IndexMut> for Vec { } #[stable(feature = "rust1", since = "1.0.0")] impl ops::IndexMut for Vec { - type Output = [T]; #[inline] fn index_mut(&mut self, _index: &ops::RangeFull) -> &mut [T] { self.as_mut_slice() diff --git a/src/libcollections/vec_map.rs b/src/libcollections/vec_map.rs index 65592d138c7..739b8d8ce19 100644 --- a/src/libcollections/vec_map.rs +++ b/src/libcollections/vec_map.rs @@ -712,8 +712,6 @@ impl Index for VecMap { #[stable(feature = "rust1", since = "1.0.0")] impl IndexMut for VecMap { - type Output = V; - #[inline] fn index_mut<'a>(&'a mut self, i: &usize) -> &'a mut V { self.get_mut(i).expect("key not present") diff --git a/src/libcore/ops.rs b/src/libcore/ops.rs index 7af94c73f32..782483a34fc 100644 --- a/src/libcore/ops.rs +++ b/src/libcore/ops.rs @@ -897,14 +897,14 @@ shr_impl_all! { u8 u16 u32 u64 usize i8 i16 i32 i64 isize } /// } /// ``` #[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 { +pub trait Index { 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 { /// 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 for Foo { +/// impl Index for Foo { /// type Output = Foo; /// +/// fn index<'a>(&'a self, _index: &Bar) -> &'a Foo { +/// self +/// } +/// } +/// +/// impl IndexMut for Foo { /// fn index_mut<'a>(&'a mut self, _index: &Bar) -> &'a mut Foo { /// println!("Indexing!"); /// self @@ -936,14 +942,12 @@ pub trait Index { /// } /// ``` #[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 { - type Output: ?Sized; - +pub trait IndexMut: Index { /// 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. diff --git a/src/libcore/slice.rs b/src/libcore/slice.rs index c622baa0cc2..fc51920ec6b 100644 --- a/src/libcore/slice.rs +++ b/src/libcore/slice.rs @@ -502,8 +502,6 @@ impl ops::Index for [T] { #[stable(feature = "rust1", since = "1.0.0")] impl ops::IndexMut for [T] { - type Output = T; - fn index_mut(&mut self, &index: &uint) -> &mut T { assert!(index < self.len()); @@ -553,7 +551,6 @@ impl ops::Index for [T] { #[stable(feature = "rust1", since = "1.0.0")] impl ops::IndexMut> for [T] { - type Output = [T]; #[inline] fn index_mut(&mut self, index: &ops::Range) -> &mut [T] { assert!(index.start <= index.end); @@ -568,7 +565,6 @@ impl ops::IndexMut> for [T] { } #[stable(feature = "rust1", since = "1.0.0")] impl ops::IndexMut> for [T] { - type Output = [T]; #[inline] fn index_mut(&mut self, index: &ops::RangeTo) -> &mut [T] { self.index_mut(&ops::Range{ start: 0, end: index.end }) @@ -576,7 +572,6 @@ impl ops::IndexMut> for [T] { } #[stable(feature = "rust1", since = "1.0.0")] impl ops::IndexMut> for [T] { - type Output = [T]; #[inline] fn index_mut(&mut self, index: &ops::RangeFrom) -> &mut [T] { let len = self.len(); @@ -585,7 +580,6 @@ impl ops::IndexMut> for [T] { } #[stable(feature = "rust1", since = "1.0.0")] impl ops::IndexMut for [T] { - type Output = [T]; #[inline] fn index_mut(&mut self, _index: &RangeFull) -> &mut [T] { self @@ -865,7 +859,6 @@ impl<'a, T> ops::Index for IterMut<'a, T> { #[unstable(feature = "core")] impl<'a, T> ops::IndexMut> for IterMut<'a, T> { - type Output = [T]; #[inline] fn index_mut(&mut self, index: &ops::Range) -> &mut [T] { self.index_mut(&RangeFull).index_mut(index) @@ -873,7 +866,6 @@ impl<'a, T> ops::IndexMut> for IterMut<'a, T> { } #[unstable(feature = "core")] impl<'a, T> ops::IndexMut> for IterMut<'a, T> { - type Output = [T]; #[inline] fn index_mut(&mut self, index: &ops::RangeTo) -> &mut [T] { self.index_mut(&RangeFull).index_mut(index) @@ -881,7 +873,6 @@ impl<'a, T> ops::IndexMut> for IterMut<'a, T> { } #[unstable(feature = "core")] impl<'a, T> ops::IndexMut> for IterMut<'a, T> { - type Output = [T]; #[inline] fn index_mut(&mut self, index: &ops::RangeFrom) -> &mut [T] { self.index_mut(&RangeFull).index_mut(index) @@ -889,7 +880,6 @@ impl<'a, T> ops::IndexMut> for IterMut<'a, T> { } #[unstable(feature = "core")] impl<'a, T> ops::IndexMut 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) diff --git a/src/libstd/collections/hash/map.rs b/src/libstd/collections/hash/map.rs index aec9446773f..710f021d912 100644 --- a/src/libstd/collections/hash/map.rs +++ b/src/libstd/collections/hash/map.rs @@ -1267,8 +1267,6 @@ impl IndexMut for HashMap S: HashState, H: hash::Hasher { - 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") diff --git a/src/test/compile-fail/borrowck-overloaded-index-autoderef.rs b/src/test/compile-fail/borrowck-overloaded-index-autoderef.rs index 977c67b1c7d..99f396ef814 100644 --- a/src/test/compile-fail/borrowck-overloaded-index-autoderef.rs +++ b/src/test/compile-fail/borrowck-overloaded-index-autoderef.rs @@ -31,8 +31,6 @@ impl Index for Foo { } impl IndexMut for Foo { - type Output = isize; - fn index_mut<'a>(&'a mut self, z: &String) -> &'a mut isize { if *z == "x" { &mut self.x diff --git a/src/test/compile-fail/borrowck-overloaded-index.rs b/src/test/compile-fail/borrowck-overloaded-index.rs index 9e79154eb0c..2d752abe7e3 100644 --- a/src/test/compile-fail/borrowck-overloaded-index.rs +++ b/src/test/compile-fail/borrowck-overloaded-index.rs @@ -28,8 +28,6 @@ impl Index for Foo { } impl IndexMut for Foo { - type Output = isize; - fn index_mut<'a>(&'a mut self, z: &String) -> &'a mut isize { if *z == "x" { &mut self.x diff --git a/src/test/run-pass/overloaded-index-autoderef.rs b/src/test/run-pass/overloaded-index-autoderef.rs index 637d2c94694..d5ccf8cd2be 100644 --- a/src/test/run-pass/overloaded-index-autoderef.rs +++ b/src/test/run-pass/overloaded-index-autoderef.rs @@ -33,8 +33,6 @@ impl Index for Foo { } impl IndexMut for Foo { - type Output = int; - fn index_mut(&mut self, z: &int) -> &mut int { if *z == 0 { &mut self.x diff --git a/src/test/run-pass/overloaded-index.rs b/src/test/run-pass/overloaded-index.rs index 0afdb24a81c..10ca3804eae 100644 --- a/src/test/run-pass/overloaded-index.rs +++ b/src/test/run-pass/overloaded-index.rs @@ -28,8 +28,6 @@ impl Index for Foo { } impl IndexMut for Foo { - type Output = int; - fn index_mut(&mut self, z: &int) -> &mut int { if *z == 0 { &mut self.x diff --git a/src/test/run-pass/slice.rs b/src/test/run-pass/slice.rs index 59fb24ffc02..30b53dbb0ad 100644 --- a/src/test/run-pass/slice.rs +++ b/src/test/run-pass/slice.rs @@ -49,28 +49,24 @@ impl Index for Foo { } impl IndexMut> for Foo { - type Output = Foo; fn index_mut(&mut self, index: &Range) -> &mut Foo { unsafe { COUNT += 1; } self } } impl IndexMut> for Foo { - type Output = Foo; fn index_mut(&mut self, index: &RangeTo) -> &mut Foo { unsafe { COUNT += 1; } self } } impl IndexMut> for Foo { - type Output = Foo; fn index_mut(&mut self, index: &RangeFrom) -> &mut Foo { unsafe { COUNT += 1; } self } } impl IndexMut for Foo { - type Output = Foo; fn index_mut(&mut self, _index: &RangeFull) -> &mut Foo { unsafe { COUNT += 1; } self