]> git.proxmox.com Git - rustc.git/blob - src/test/run-pass/class-impl-very-parameterized-trait.rs
c3ced512afae4c01beee22a193b1fae1d1ef43a2
[rustc.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 #[derive(Copy, Debug)]
15 enum cat_type { tuxedo, tabby, tortoiseshell }
16
17 impl cmp::PartialEq 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 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 println!("OM NOM NOM");
42 self.how_hungry -= 2;
43 return true;
44 } else {
45 println!("Not hungry!");
46 return false;
47 }
48 }
49 fn len(&self) -> uint { self.meows as uint }
50 fn is_empty(&self) -> bool { self.meows == 0 }
51 fn clear(&mut self) {}
52 fn contains_key(&self, k: &int) -> bool { *k <= self.meows }
53
54 fn find(&self, k: &int) -> Option<&T> {
55 if *k <= self.meows {
56 Some(&self.name)
57 } else {
58 None
59 }
60 }
61 fn insert(&mut self, k: int, _: T) -> bool {
62 self.meows += k;
63 true
64 }
65
66 fn find_mut(&mut self, _k: &int) -> Option<&mut T> { panic!() }
67
68 fn remove(&mut self, k: &int) -> bool {
69 if self.find(k).is_some() {
70 self.meows -= *k; true
71 } else {
72 false
73 }
74 }
75
76 fn pop(&mut self, _k: &int) -> Option<T> { panic!() }
77
78 fn swap(&mut self, _k: int, _v: T) -> Option<T> { panic!() }
79 }
80
81 impl<T> cat<T> {
82 pub fn get(&self, k: &int) -> &T {
83 match self.find(k) {
84 Some(v) => { v }
85 None => { panic!("epic fail"); }
86 }
87 }
88
89 pub fn new(in_x: int, in_y: int, in_name: T) -> cat<T> {
90 cat{meows: in_x, how_hungry: in_y, name: in_name }
91 }
92 }
93
94 impl<T> cat<T> {
95 fn meow(&mut self) {
96 self.meows += 1;
97 println!("Meow {}", self.meows);
98 if self.meows % 5 == 0 {
99 self.how_hungry += 1;
100 }
101 }
102 }
103
104 pub fn main() {
105 let mut nyan: cat<String> = cat::new(0, 2, "nyan".to_string());
106 for _ in 1_usize..5 { nyan.speak(); }
107 assert!(*nyan.find(&1).unwrap() == "nyan".to_string());
108 assert_eq!(nyan.find(&10), None);
109 let mut spotty: cat<cat_type> = cat::new(2, 57, cat_type::tuxedo);
110 for _ in 0_usize..6 { spotty.speak(); }
111 assert_eq!(spotty.len(), 8);
112 assert!((spotty.contains_key(&2)));
113 assert_eq!(spotty.get(&3), &cat_type::tuxedo);
114 }