Auto merge of #21949 - japaric:index, r=nikomatsakis
closes #21630 Overloaded indexing (`&[mut] foo[bar]`) only works when `<Self as Index>::Output` is the same as `<Self as IndexMut>::Output` (see issue above). To restrict implementations of `IndexMut` that doesn't work, this PR makes `IndexMut` a supertrait over `Index`, i.e. `trait IndexMut<I>: Index<I>`, just like in the `trait DerefMut: Deref` case. This breaks all downstream implementations of `IndexMut`, in most cases this simply means removing the `type Output = ..` bit, which is now redundant, from `IndexMut` implementations: ``` diff impl Index<Foo> for Bar { type Output = Baz; .. } impl IndexMut<Foo> for Bar { - type Output = Baz; .. } ``` [breaking-change] --- r? @nikomatsakis
This commit is contained in:
commit
0b6dbbc9cf
12 changed files with 14 additions and 46 deletions
|
|
@ -31,8 +31,6 @@ impl Index<String> for Foo {
|
|||
}
|
||||
|
||||
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
|
||||
|
|
|
|||
|
|
@ -28,8 +28,6 @@ impl Index<String> for Foo {
|
|||
}
|
||||
|
||||
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
|
||||
|
|
|
|||
|
|
@ -33,8 +33,6 @@ impl Index<int> for Foo {
|
|||
}
|
||||
|
||||
impl IndexMut<int> for Foo {
|
||||
type Output = int;
|
||||
|
||||
fn index_mut(&mut self, z: &int) -> &mut int {
|
||||
if *z == 0 {
|
||||
&mut self.x
|
||||
|
|
|
|||
|
|
@ -28,8 +28,6 @@ impl Index<int> for Foo {
|
|||
}
|
||||
|
||||
impl IndexMut<int> for Foo {
|
||||
type Output = int;
|
||||
|
||||
fn index_mut(&mut self, z: &int) -> &mut int {
|
||||
if *z == 0 {
|
||||
&mut self.x
|
||||
|
|
|
|||
|
|
@ -49,28 +49,24 @@ impl Index<RangeFull> for 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
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue