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