]> git.lizzy.rs Git - rust.git/blob - src/test/run-pass/class-cast-to-trait.rs
auto merge of #11784 : eminence/rust/fix_run_tests, r=alexcrichton
[rust.git] / src / test / run-pass / class-cast-to-trait.rs
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.
4 //
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.
10
11 #[feature(managed_boxes)];
12
13 trait noisy {
14   fn speak(&mut self);
15 }
16
17 struct cat {
18   meows: uint,
19   how_hungry: int,
20   name: ~str,
21 }
22
23 impl noisy for cat {
24   fn speak(&mut self) { self.meow(); }
25 }
26
27 impl cat {
28   pub fn eat(&mut self) -> bool {
29     if self.how_hungry > 0 {
30         error!("OM NOM NOM");
31         self.how_hungry -= 2;
32         return true;
33     }
34     else {
35         error!("Not hungry!");
36         return false;
37     }
38   }
39 }
40
41 impl cat {
42     fn meow(&mut self) {
43       error!("Meow");
44       self.meows += 1u;
45       if self.meows % 5u == 0u {
46           self.how_hungry += 1;
47       }
48     }
49 }
50
51 fn cat(in_x : uint, in_y : int, in_name: ~str) -> cat {
52     cat {
53         meows: in_x,
54         how_hungry: in_y,
55         name: in_name
56     }
57 }
58
59
60 pub fn main() {
61     let mut nyan = cat(0u, 2, ~"nyan");
62     let mut nyan: &mut noisy = &mut nyan;
63     nyan.speak();
64 }