3 #![allow(non_camel_case_types)]
4 #![allow(non_snake_case)]
7 // Example from lkuper's intern talk, August 2012.
8 use Color::{cyan, magenta, yellow, black};
9 use ColorTree::{leaf, branch};
12 fn isEq(&self, a: &Self) -> bool;
15 #[derive(Clone, Copy)]
16 enum Color { cyan, magenta, yellow, black }
18 impl Equal for Color {
19 fn isEq(&self, a: &Color) -> bool {
21 (cyan, cyan) => { true }
22 (magenta, magenta) => { true }
23 (yellow, yellow) => { true }
24 (black, black) => { true }
33 branch(Box<ColorTree>, Box<ColorTree>)
36 impl Equal for ColorTree {
37 fn isEq(&self, a: &ColorTree) -> bool {
39 (&leaf(ref x), &leaf(ref y)) => { x.isEq(&(*y).clone()) }
40 (&branch(ref l1, ref r1), &branch(ref l2, ref r2)) => {
41 (*l1).isEq(&(**l2).clone()) && (*r1).isEq(&(**r2).clone())
49 assert!(cyan.isEq(&cyan));
50 assert!(magenta.isEq(&magenta));
51 assert!(!cyan.isEq(&yellow));
52 assert!(!magenta.isEq(&cyan));
54 assert!(leaf(cyan).isEq(&leaf(cyan)));
55 assert!(!leaf(cyan).isEq(&leaf(yellow)));
57 assert!(branch(Box::new(leaf(magenta)), Box::new(leaf(cyan)))
58 .isEq(&branch(Box::new(leaf(magenta)), Box::new(leaf(cyan)))));
60 assert!(!branch(Box::new(leaf(magenta)), Box::new(leaf(cyan)))
61 .isEq(&branch(Box::new(leaf(magenta)), Box::new(leaf(magenta)))));
63 println!("Assertions all succeeded!");