]> git.lizzy.rs Git - rust.git/blob - src/test/run-pass/deriving-zero.rs
doc: remove incomplete sentence
[rust.git] / src / test / run-pass / deriving-zero.rs
1 // Copyright 2012-2013 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 #![feature(associated_types)]
12
13 use std::num::Zero;
14 use std::ops::Add;
15
16 #[derive(Zero)]
17 struct Vector2<T>(T, T);
18
19 impl<T: Add<Output=T>> Add for Vector2<T> {
20     type Output = Vector2<T>;
21
22     fn add(self, other: Vector2<T>) -> Vector2<T> {
23         match (self, other) {
24             (Vector2(x0, y0), Vector2(x1, y1)) => {
25                 Vector2(x0 + x1, y0 + y1)
26             }
27         }
28     }
29 }
30
31 #[derive(Zero)]
32 struct Vector3<T> {
33     x: T, y: T, z: T,
34 }
35
36 impl<T: Add<Output=T>> Add for Vector3<T> {
37     type Output = Vector3<T>;
38
39     fn add(self, other: Vector3<T>) -> Vector3<T> {
40         Vector3 {
41             x: self.x + other.x,
42             y: self.y + other.y,
43             z: self.z + other.z,
44         }
45     }
46 }
47
48 #[derive(Zero)]
49 struct Matrix3x2<T> {
50     x: Vector2<T>,
51     y: Vector2<T>,
52     z: Vector2<T>,
53 }
54
55 impl<T: Add<Output=T>> Add for Matrix3x2<T> {
56     type Output = Matrix3x2<T>;
57
58     fn add(self, other: Matrix3x2<T>) -> Matrix3x2<T> {
59         Matrix3x2 {
60             x: self.x + other.x,
61             y: self.y + other.y,
62             z: self.z + other.z,
63         }
64     }
65 }
66
67 pub fn main() {
68     let _: Vector2<int> = Zero::zero();
69     let _: Vector3<f64> = Zero::zero();
70     let _: Matrix3x2<u8> = Zero::zero();
71 }