summary refs log tree commit diff
path: root/src/test/run-pass/deriving-zero.rs
blob: 9ae72038aa9d9fc13fac13c00e2a9555b56464fa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
// Copyright 2012-2013 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

#[feature(managed_boxes)];

use std::num::Zero;

#[deriving(Zero)]
struct Vector2<T>(T, T);

impl<T: Add<T, T>> Add<Vector2<T>, Vector2<T>> for Vector2<T> {
    fn add(&self, other: &Vector2<T>) -> Vector2<T> {
        match (self, other) {
            (&Vector2(ref x0, ref y0), &Vector2(ref x1, ref y1)) => {
                Vector2(*x0 + *x1, *y0 + *y1)
            }
        }
    }
}

#[deriving(Zero)]
struct Vector3<T> {
    x: T, y: T, z: T,
}

impl<T: Add<T, T>> Add<Vector3<T>, Vector3<T>> for Vector3<T> {
    fn add(&self, other: &Vector3<T>) -> Vector3<T> {
        Vector3 {
            x: self.x + other.x,
            y: self.y + other.y,
            z: self.z + other.z,
        }
    }
}

#[deriving(Zero)]
struct Matrix3x2<T> {
    x: Vector2<T>,
    y: Vector2<T>,
    z: Vector2<T>,
}

impl<T: Add<T, T>> Add<Matrix3x2<T>, Matrix3x2<T>> for Matrix3x2<T> {
    fn add(&self, other: &Matrix3x2<T>) -> Matrix3x2<T> {
        Matrix3x2 {
            x: self.x + other.x,
            y: self.y + other.y,
            z: self.z + other.z,
        }
    }
}

pub fn main() {
    let _: Vector2<int> = Zero::zero();
    let _: Vector3<f64> = Zero::zero();
    let _: Matrix3x2<u8> = Zero::zero();
}