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