]> git.lizzy.rs Git - rust.git/blob - src/test/ui/structs-enums/class-cast-to-trait-multiple-types.rs
Override rustc version in ui and mir-opt tests to get stable hashes
[rust.git] / src / test / ui / structs-enums / class-cast-to-trait-multiple-types.rs
1 // run-pass
2 #![allow(non_camel_case_types)]
3 #![allow(dead_code)]
4
5 trait noisy {
6   fn speak(&mut self) -> isize;
7 }
8
9 struct dog {
10   barks: usize,
11
12   volume: isize,
13 }
14
15 impl dog {
16     fn bark(&mut self) -> isize {
17       println!("Woof {} {}", self.barks, self.volume);
18       self.barks += 1_usize;
19       if self.barks % 3_usize == 0_usize {
20           self.volume += 1;
21       }
22       if self.barks % 10_usize == 0_usize {
23           self.volume -= 2;
24       }
25       println!("Grrr {} {}", self.barks, self.volume);
26       self.volume
27     }
28 }
29
30 impl noisy for dog {
31     fn speak(&mut self) -> isize {
32         self.bark()
33     }
34 }
35
36 fn dog() -> dog {
37     dog {
38         volume: 0,
39         barks: 0_usize
40     }
41 }
42
43 #[derive(Clone)]
44 struct cat {
45   meows: usize,
46
47   how_hungry: isize,
48   name: String,
49 }
50
51 impl noisy for cat {
52     fn speak(&mut self) -> isize {
53         self.meow() as isize
54     }
55 }
56
57 impl cat {
58     pub fn meow_count(&self) -> usize {
59         self.meows
60     }
61 }
62
63 impl cat {
64     fn meow(&mut self) -> usize {
65         println!("Meow");
66         self.meows += 1_usize;
67         if self.meows % 5_usize == 0_usize {
68             self.how_hungry += 1;
69         }
70         self.meows
71     }
72 }
73
74 fn cat(in_x: usize, in_y: isize, in_name: String) -> cat {
75     cat {
76         meows: in_x,
77         how_hungry: in_y,
78         name: in_name
79     }
80 }
81
82
83 fn annoy_neighbors(critter: &mut dyn noisy) {
84     for _i in 0_usize..10 { critter.speak(); }
85 }
86
87 pub fn main() {
88   let mut nyan: cat = cat(0_usize, 2, "nyan".to_string());
89   let mut whitefang: dog = dog();
90   annoy_neighbors(&mut nyan);
91   annoy_neighbors(&mut whitefang);
92   assert_eq!(nyan.meow_count(), 10_usize);
93   assert_eq!(whitefang.volume, 1);
94 }