2018-08-30 07:18:55 -05:00
|
|
|
// run-pass
|
2014-10-31 04:44:10 -05:00
|
|
|
// Test using overloaded indexing when the "map" is stored in a
|
|
|
|
// field. This caused problems at some point.
|
|
|
|
|
2014-12-22 11:04:23 -06:00
|
|
|
use std::ops::Index;
|
|
|
|
|
2014-10-31 04:44:10 -05:00
|
|
|
struct Foo {
|
2015-03-25 19:06:52 -05:00
|
|
|
x: isize,
|
|
|
|
y: isize,
|
2014-10-31 04:44:10 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
struct Bar {
|
|
|
|
foo: Foo
|
|
|
|
}
|
|
|
|
|
2015-03-25 19:06:52 -05:00
|
|
|
impl Index<isize> for Foo {
|
|
|
|
type Output = isize;
|
2015-01-03 09:40:36 -06:00
|
|
|
|
2015-03-25 19:06:52 -05:00
|
|
|
fn index(&self, z: isize) -> &isize {
|
2015-03-21 20:15:47 -05:00
|
|
|
if z == 0 {
|
2014-10-31 04:44:10 -05:00
|
|
|
&self.x
|
|
|
|
} else {
|
|
|
|
&self.y
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
trait Int {
|
2015-03-25 19:06:52 -05:00
|
|
|
fn get(self) -> isize;
|
|
|
|
fn get_from_ref(&self) -> isize;
|
2014-10-31 04:44:10 -05:00
|
|
|
fn inc(&mut self);
|
|
|
|
}
|
|
|
|
|
2015-03-25 19:06:52 -05:00
|
|
|
impl Int for isize {
|
|
|
|
fn get(self) -> isize { self }
|
|
|
|
fn get_from_ref(&self) -> isize { *self }
|
2014-10-31 04:44:10 -05:00
|
|
|
fn inc(&mut self) { *self += 1; }
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
let f = Bar { foo: Foo {
|
|
|
|
x: 1,
|
|
|
|
y: 2,
|
|
|
|
} };
|
|
|
|
assert_eq!(f.foo[1].get(), 2);
|
|
|
|
}
|