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