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