#[derive(Debug, Clone)]
struct Point {
x: i32,
y: i32
}
impl std::ops::Add<Point> for Point {
type Output = Point;
fn add( self, other: Point ) -> Point {
Point { x: self.x + other.x, y: self.y + other.y }
}
}
impl std::ops::Add<Box<Point>> for Point {
type Output = Point;
fn add( self, other: Box<Point> ) -> Point {
self + *other
}
}
impl std::ops::Add<Point> for Box<Point> {
type Output = Point;
fn add( self, other: Point ) -> Point {
*self + other
}
}
impl std::ops::Add<Box<Point>> for Box<Point> {
type Output = Point;
fn add( self, other: Box<Point> ) -> Point {
*self + *other
}
}
fn main() {
let first_point: Point = Point { x: 1, y: 2 };
let second_point: Box<Point> = Box::new( Point { x: 1, y: 1 } );
let third_point: Box<Point> = second_point.clone();
let fourth_point: Point = first_point + (second_point + third_point);
println!( "{:?}", fourth_point );
}
You can use macros to simplify things. This is the sort of things macros were made for.