2019-07-26 21:54:25 +00:00
|
|
|
//@ run-pass
|
|
|
|
|
2018-09-14 10:20:28 +00:00
|
|
|
#![allow(unused_variables)]
|
2013-05-25 02:35:29 +00:00
|
|
|
use std::cmp;
|
|
|
|
use std::ops;
|
|
|
|
|
2015-03-30 13:38:27 +00:00
|
|
|
#[derive(Copy, Clone, Debug)]
|
2012-07-28 02:32:42 +00:00
|
|
|
struct Point {
|
2015-03-26 00:06:52 +00:00
|
|
|
x: isize,
|
|
|
|
y: isize
|
2012-07-11 22:00:40 +00:00
|
|
|
}
|
|
|
|
|
2014-12-31 20:45:13 +00:00
|
|
|
impl ops::Add for Point {
|
|
|
|
type Output = Point;
|
|
|
|
|
2014-12-01 22:33:22 +00:00
|
|
|
fn add(self, other: Point) -> Point {
|
|
|
|
Point {x: self.x + other.x, y: self.y + other.y}
|
2012-01-26 11:26:14 +00:00
|
|
|
}
|
2012-07-28 02:32:42 +00:00
|
|
|
}
|
|
|
|
|
2014-12-31 20:45:13 +00:00
|
|
|
impl ops::Sub for Point {
|
|
|
|
type Output = Point;
|
|
|
|
|
2014-12-01 22:33:22 +00:00
|
|
|
fn sub(self, other: Point) -> Point {
|
|
|
|
Point {x: self.x - other.x, y: self.y - other.y}
|
2012-01-26 14:52:28 +00:00
|
|
|
}
|
2012-07-28 02:32:42 +00:00
|
|
|
}
|
|
|
|
|
2015-01-03 03:56:24 +00:00
|
|
|
impl ops::Neg for Point {
|
|
|
|
type Output = Point;
|
|
|
|
|
2014-12-15 23:24:07 +00:00
|
|
|
fn neg(self) -> Point {
|
2012-07-28 02:32:42 +00:00
|
|
|
Point {x: -self.x, y: -self.y}
|
2012-01-26 11:26:14 +00:00
|
|
|
}
|
2012-07-28 02:32:42 +00:00
|
|
|
}
|
|
|
|
|
2015-01-03 03:56:24 +00:00
|
|
|
impl ops::Not for Point {
|
|
|
|
type Output = Point;
|
|
|
|
|
2014-12-15 23:24:07 +00:00
|
|
|
fn not(self) -> Point {
|
2013-01-11 21:27:12 +00:00
|
|
|
Point {x: !self.x, y: !self.y }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-03 15:40:36 +00:00
|
|
|
impl ops::Index<bool> for Point {
|
2015-03-26 00:06:52 +00:00
|
|
|
type Output = isize;
|
2015-01-03 15:40:36 +00:00
|
|
|
|
2015-03-26 00:06:52 +00:00
|
|
|
fn index(&self, x: bool) -> &isize {
|
2015-03-22 01:15:47 +00:00
|
|
|
if x {
|
2014-07-03 21:32:41 +00:00
|
|
|
&self.x
|
|
|
|
} else {
|
|
|
|
&self.y
|
|
|
|
}
|
2012-01-26 14:23:04 +00:00
|
|
|
}
|
2012-01-26 11:26:14 +00:00
|
|
|
}
|
|
|
|
|
2014-05-30 00:45:07 +00:00
|
|
|
impl cmp::PartialEq for Point {
|
2013-03-22 18:23:21 +00:00
|
|
|
fn eq(&self, other: &Point) -> bool {
|
2012-11-15 02:59:30 +00:00
|
|
|
(*self).x == (*other).x && (*self).y == (*other).y
|
2012-08-27 23:26:35 +00:00
|
|
|
}
|
2013-03-22 18:23:21 +00:00
|
|
|
fn ne(&self, other: &Point) -> bool { !(*self).eq(other) }
|
2012-08-27 23:26:35 +00:00
|
|
|
}
|
|
|
|
|
2013-02-02 03:43:17 +00:00
|
|
|
pub fn main() {
|
2012-07-28 02:32:42 +00:00
|
|
|
let mut p = Point {x: 10, y: 20};
|
2013-06-12 02:13:42 +00:00
|
|
|
p = p + Point {x: 101, y: 102};
|
2012-11-21 20:25:35 +00:00
|
|
|
p = p - Point {x: 100, y: 100};
|
2013-05-19 02:02:45 +00:00
|
|
|
assert_eq!(p + Point {x: 5, y: 5}, Point {x: 16, y: 27});
|
|
|
|
assert_eq!(-p, Point {x: -11, y: -22});
|
|
|
|
assert_eq!(p[true], 11);
|
|
|
|
assert_eq!(p[false], 22);
|
2013-01-11 21:27:12 +00:00
|
|
|
|
|
|
|
let q = !p;
|
2013-05-19 02:02:45 +00:00
|
|
|
assert_eq!(q.x, !(p.x));
|
|
|
|
assert_eq!(q.y, !(p.y));
|
2013-01-11 21:27:12 +00:00
|
|
|
|
2012-02-02 09:30:07 +00:00
|
|
|
// Issue #1733
|
2013-03-01 23:55:31 +00:00
|
|
|
result(p[true]);
|
2012-01-26 11:26:14 +00:00
|
|
|
}
|
2014-11-26 13:12:18 +00:00
|
|
|
|
2015-03-26 00:06:52 +00:00
|
|
|
fn result(i: isize) { }
|