]> git.lizzy.rs Git - rust.git/blob - src/test/ui/structs-enums/class-impl-very-parameterized-trait.rs
Override rustc version in ui and mir-opt tests to get stable hashes
[rust.git] / src / test / ui / structs-enums / class-impl-very-parameterized-trait.rs
1 // run-pass
2 #![allow(dead_code)]
3 #![allow(non_camel_case_types)]
4
5 use std::cmp;
6
7 #[derive(Copy, Clone, Debug)]
8 enum cat_type { tuxedo, tabby, tortoiseshell }
9
10 impl cmp::PartialEq for cat_type {
11     fn eq(&self, other: &cat_type) -> bool {
12         ((*self) as usize) == ((*other) as usize)
13     }
14     fn ne(&self, other: &cat_type) -> bool { !(*self).eq(other) }
15 }
16
17 // Very silly -- this just returns the value of the name field
18 // for any isize value that's less than the meows field
19
20 // ok: T should be in scope when resolving the trait ref for map
21 struct cat<T> {
22     // Yes, you can have negative meows
23     meows : isize,
24
25     how_hungry : isize,
26     name : T,
27 }
28
29 impl<T> cat<T> {
30     pub fn speak(&mut self) { self.meow(); }
31
32     pub fn eat(&mut self) -> bool {
33         if self.how_hungry > 0 {
34             println!("OM NOM NOM");
35             self.how_hungry -= 2;
36             return true;
37         } else {
38             println!("Not hungry!");
39             return false;
40         }
41     }
42     fn len(&self) -> usize { self.meows as usize }
43     fn is_empty(&self) -> bool { self.meows == 0 }
44     fn clear(&mut self) {}
45     fn contains_key(&self, k: &isize) -> bool { *k <= self.meows }
46
47     fn find(&self, k: &isize) -> Option<&T> {
48         if *k <= self.meows {
49             Some(&self.name)
50         } else {
51             None
52         }
53     }
54     fn insert(&mut self, k: isize, _: T) -> bool {
55         self.meows += k;
56         true
57     }
58
59     fn find_mut(&mut self, _k: &isize) -> Option<&mut T> { panic!() }
60
61     fn remove(&mut self, k: &isize) -> bool {
62         if self.find(k).is_some() {
63             self.meows -= *k; true
64         } else {
65             false
66         }
67     }
68
69     fn pop(&mut self, _k: &isize) -> Option<T> { panic!() }
70
71     fn swap(&mut self, _k: isize, _v: T) -> Option<T> { panic!() }
72 }
73
74 impl<T> cat<T> {
75     pub fn get(&self, k: &isize) -> &T {
76         match self.find(k) {
77           Some(v) => { v }
78           None    => { panic!("epic fail"); }
79         }
80     }
81
82     pub fn new(in_x: isize, in_y: isize, in_name: T) -> cat<T> {
83         cat{meows: in_x, how_hungry: in_y, name: in_name }
84     }
85 }
86
87 impl<T> cat<T> {
88     fn meow(&mut self) {
89         self.meows += 1;
90         println!("Meow {}", self.meows);
91         if self.meows % 5 == 0 {
92             self.how_hungry += 1;
93         }
94     }
95 }
96
97 pub fn main() {
98     let mut nyan: cat<String> = cat::new(0, 2, "nyan".to_string());
99     for _ in 1_usize..5 { nyan.speak(); }
100     assert_eq!(*nyan.find(&1).unwrap(), "nyan".to_string());
101     assert_eq!(nyan.find(&10), None);
102     let mut spotty: cat<cat_type> = cat::new(2, 57, cat_type::tuxedo);
103     for _ in 0_usize..6 { spotty.speak(); }
104     assert_eq!(spotty.len(), 8);
105     assert!((spotty.contains_key(&2)));
106     assert_eq!(spotty.get(&3), &cat_type::tuxedo);
107 }