2018-08-30 12:18:55 +00:00
|
|
|
//@ run-pass
|
2014-10-31 09:44:10 +00:00
|
|
|
// Test using overloaded indexing when the "map" is stored in a
|
|
|
|
// field. This caused problems at some point.
|
|
|
|
|
2014-12-22 17:04:23 +00:00
|
|
|
use std::ops::Index;
|
|
|
|
|
2014-10-31 09:44:10 +00:00
|
|
|
struct Foo {
|
2015-03-26 00:06:52 +00:00
|
|
|
x: isize,
|
|
|
|
y: isize,
|
2014-10-31 09:44:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
struct Bar {
|
|
|
|
foo: Foo
|
|
|
|
}
|
|
|
|
|
2015-03-26 00:06:52 +00:00
|
|
|
impl Index<isize> for Foo {
|
|
|
|
type Output = isize;
|
2015-01-03 15:40:36 +00:00
|
|
|
|
2015-03-26 00:06:52 +00:00
|
|
|
fn index(&self, z: isize) -> &isize {
|
2015-03-22 01:15:47 +00:00
|
|
|
if z == 0 {
|
2014-10-31 09:44:10 +00:00
|
|
|
&self.x
|
|
|
|
} else {
|
|
|
|
&self.y
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
trait Int {
|
2015-03-26 00:06:52 +00:00
|
|
|
fn get(self) -> isize;
|
2024-02-07 02:42:01 +00:00
|
|
|
fn get_from_ref(&self) -> isize; //~ WARN methods `get_from_ref` and `inc` are never used
|
2014-10-31 09:44:10 +00:00
|
|
|
fn inc(&mut self);
|
|
|
|
}
|
|
|
|
|
2015-03-26 00:06:52 +00:00
|
|
|
impl Int for isize {
|
|
|
|
fn get(self) -> isize { self }
|
|
|
|
fn get_from_ref(&self) -> isize { *self }
|
2014-10-31 09:44:10 +00: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);
|
|
|
|
}
|