]> git.lizzy.rs Git - rust.git/blob - src/test/ui/typeclasses-eq-example.rs
Auto merge of #75936 - sdroege:chunks-exact-construction-bounds-check, r=nagisa
[rust.git] / src / test / ui / typeclasses-eq-example.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.
9 use Color::{cyan, magenta, yellow, black};
10 use ColorTree::{leaf, branch};
11
12 trait Equal {
13     fn isEq(&self, a: &Self) -> bool;
14 }
15
16 #[derive(Clone, Copy)]
17 enum Color { cyan, magenta, yellow, black }
18
19 impl Equal for Color {
20     fn isEq(&self, a: &Color) -> bool {
21         match (*self, *a) {
22           (cyan, cyan)       => { true  }
23           (magenta, magenta) => { true  }
24           (yellow, yellow)   => { true  }
25           (black, black)     => { true  }
26           _                  => { false }
27         }
28     }
29 }
30
31 #[derive(Clone)]
32 enum ColorTree {
33     leaf(Color),
34     branch(Box<ColorTree>, Box<ColorTree>)
35 }
36
37 impl Equal for ColorTree {
38     fn isEq(&self, a: &ColorTree) -> bool {
39         match (self, a) {
40           (&leaf(ref x), &leaf(ref y)) => { x.isEq(&(*y).clone()) }
41           (&branch(ref l1, ref r1), &branch(ref l2, ref r2)) => {
42             (*l1).isEq(&(**l2).clone()) && (*r1).isEq(&(**r2).clone())
43           }
44           _ => { false }
45         }
46     }
47 }
48
49 pub fn main() {
50     assert!(cyan.isEq(&cyan));
51     assert!(magenta.isEq(&magenta));
52     assert!(!cyan.isEq(&yellow));
53     assert!(!magenta.isEq(&cyan));
54
55     assert!(leaf(cyan).isEq(&leaf(cyan)));
56     assert!(!leaf(cyan).isEq(&leaf(yellow)));
57
58     assert!(branch(box leaf(magenta), box leaf(cyan))
59         .isEq(&branch(box leaf(magenta), box leaf(cyan))));
60
61     assert!(!branch(box leaf(magenta), box leaf(cyan))
62         .isEq(&branch(box leaf(magenta), box leaf(magenta))));
63
64     println!("Assertions all succeeded!");
65 }