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