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