]> git.lizzy.rs Git - rust.git/blob - src/test/ui/typeclasses-eq-example-static.rs
Auto merge of #75936 - sdroege:chunks-exact-construction-bounds-check, r=nagisa
[rust.git] / src / test / ui / typeclasses-eq-example-static.rs
1 // run-pass
2
3 #![allow(non_camel_case_types)]
4 #![allow(non_snake_case)]
5 #![allow(dead_code)]
6 #![feature(box_syntax)]
7
8 // Example from lkuper's intern talk, August 2012 -- now with static
9 // methods!
10 use Color::{cyan, magenta, yellow, black};
11 use ColorTree::{leaf, branch};
12
13 trait Equal {
14     fn isEq(a: &Self, b: &Self) -> bool;
15 }
16
17 #[derive(Clone, Copy)]
18 enum Color { cyan, magenta, yellow, black }
19
20 impl Equal for Color {
21     fn isEq(a: &Color, b: &Color) -> bool {
22         match (*a, *b) {
23           (cyan, cyan)       => { true  }
24           (magenta, magenta) => { true  }
25           (yellow, yellow)   => { true  }
26           (black, black)     => { true  }
27           _                  => { false }
28         }
29     }
30 }
31
32 #[derive(Clone)]
33 enum ColorTree {
34     leaf(Color),
35     branch(Box<ColorTree>, Box<ColorTree>)
36 }
37
38 impl Equal for ColorTree {
39     fn isEq(a: &ColorTree, b: &ColorTree) -> bool {
40         match (a, b) {
41           (&leaf(ref x), &leaf(ref y)) => {
42               Equal::isEq(&(*x).clone(), &(*y).clone())
43           }
44           (&branch(ref l1, ref r1), &branch(ref l2, ref r2)) => {
45             Equal::isEq(&(**l1).clone(), &(**l2).clone()) &&
46                 Equal::isEq(&(**r1).clone(), &(**r2).clone())
47           }
48           _ => { false }
49         }
50     }
51 }
52
53 pub fn main() {
54     assert!(Equal::isEq(&cyan, &cyan));
55     assert!(Equal::isEq(&magenta, &magenta));
56     assert!(!Equal::isEq(&cyan, &yellow));
57     assert!(!Equal::isEq(&magenta, &cyan));
58
59     assert!(Equal::isEq(&leaf(cyan), &leaf(cyan)));
60     assert!(!Equal::isEq(&leaf(cyan), &leaf(yellow)));
61
62     assert!(Equal::isEq(&branch(box leaf(magenta), box leaf(cyan)),
63                 &branch(box leaf(magenta), box leaf(cyan))));
64
65     assert!(!Equal::isEq(&branch(box leaf(magenta), box leaf(cyan)),
66                  &branch(box leaf(magenta), box leaf(magenta))));
67
68     println!("Assertions all succeeded!");
69 }