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;
22 enum Color { cyan, magenta, yellow, black }
24 impl Copy for Color {}
26 impl Equal for Color {
27 fn isEq(a: &Color, b: &Color) -> bool {
29 (cyan, cyan) => { true }
30 (magenta, magenta) => { true }
31 (yellow, yellow) => { true }
32 (black, black) => { true }
41 branch(Box<ColorTree>, Box<ColorTree>)
44 impl Equal for ColorTree {
45 fn isEq(a: &ColorTree, b: &ColorTree) -> bool {
47 (&leaf(ref x), &leaf(ref y)) => {
48 Equal::isEq(&(*x).clone(), &(*y).clone())
50 (&branch(ref l1, ref r1), &branch(ref l2, ref r2)) => {
51 Equal::isEq(&(**l1).clone(), &(**l2).clone()) &&
52 Equal::isEq(&(**r1).clone(), &(**r2).clone())
60 assert!(Equal::isEq(&cyan, &cyan));
61 assert!(Equal::isEq(&magenta, &magenta));
62 assert!(!Equal::isEq(&cyan, &yellow));
63 assert!(!Equal::isEq(&magenta, &cyan));
65 assert!(Equal::isEq(&leaf(cyan), &leaf(cyan)));
66 assert!(!Equal::isEq(&leaf(cyan), &leaf(yellow)));
68 assert!(Equal::isEq(&branch(box leaf(magenta), box leaf(cyan)),
69 &branch(box leaf(magenta), box leaf(cyan))));
71 assert!(!Equal::isEq(&branch(box leaf(magenta), box leaf(cyan)),
72 &branch(box leaf(magenta), box leaf(magenta))));
74 println!("Assertions all succeeded!");