1 // Copyright 2012 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.
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.
12 // Example from lkuper's intern talk, August 2012 -- now with static
14 use Color::{cyan, magenta, yellow, black};
15 use ColorTree::{leaf, branch};
18 fn isEq(a: &Self, b: &Self) -> bool;
21 enum Color { cyan, magenta, yellow, black }
23 impl Equal for Color {
24 fn isEq(a: &Color, b: &Color) -> bool {
26 (cyan, cyan) => { true }
27 (magenta, magenta) => { true }
28 (yellow, yellow) => { true }
29 (black, black) => { true }
37 branch(Box<ColorTree>, Box<ColorTree>)
40 impl Equal for ColorTree {
41 fn isEq(a: &ColorTree, b: &ColorTree) -> bool {
43 (&leaf(x), &leaf(y)) => { Equal::isEq(&x, &y) }
44 (&branch(ref l1, ref r1), &branch(ref l2, ref r2)) => {
45 Equal::isEq(&**l1, &**l2) && Equal::isEq(&**r1, &**r2)
53 assert!(Equal::isEq(&cyan, &cyan));
54 assert!(Equal::isEq(&magenta, &magenta));
55 assert!(!Equal::isEq(&cyan, &yellow));
56 assert!(!Equal::isEq(&magenta, &cyan));
58 assert!(Equal::isEq(&leaf(cyan), &leaf(cyan)));
59 assert!(!Equal::isEq(&leaf(cyan), &leaf(yellow)));
61 assert!(Equal::isEq(&branch(box leaf(magenta), box leaf(cyan)),
62 &branch(box leaf(magenta), box leaf(cyan))));
64 assert!(!Equal::isEq(&branch(box leaf(magenta), box leaf(cyan)),
65 &branch(box leaf(magenta), box leaf(magenta))));
67 println!("Assertions all succeeded!");