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.
11 #![allow(unknown_features)]
12 #![feature(box_syntax)]
14 // Example from lkuper's intern talk, August 2012.
15 use Color::{cyan, magenta, yellow, black};
16 use ColorTree::{leaf, branch};
19 fn isEq(&self, a: &Self) -> bool;
22 #[derive(Clone, Copy)]
23 enum Color { cyan, magenta, yellow, black }
25 impl Equal for Color {
26 fn isEq(&self, a: &Color) -> bool {
28 (cyan, cyan) => { true }
29 (magenta, magenta) => { true }
30 (yellow, yellow) => { true }
31 (black, black) => { true }
40 branch(Box<ColorTree>, Box<ColorTree>)
43 impl Equal for ColorTree {
44 fn isEq(&self, a: &ColorTree) -> bool {
46 (&leaf(ref x), &leaf(ref y)) => { x.isEq(&(*y).clone()) }
47 (&branch(ref l1, ref r1), &branch(ref l2, ref r2)) => {
48 (*l1).isEq(&(**l2).clone()) && (*r1).isEq(&(**r2).clone())
56 assert!(cyan.isEq(&cyan));
57 assert!(magenta.isEq(&magenta));
58 assert!(!cyan.isEq(&yellow));
59 assert!(!magenta.isEq(&cyan));
61 assert!(leaf(cyan).isEq(&leaf(cyan)));
62 assert!(!leaf(cyan).isEq(&leaf(yellow)));
64 assert!(branch(box leaf(magenta), box leaf(cyan))
65 .isEq(&branch(box leaf(magenta), box leaf(cyan))));
67 assert!(!branch(box leaf(magenta), box leaf(cyan))
68 .isEq(&branch(box leaf(magenta), box leaf(magenta))));
70 println!("Assertions all succeeded!");