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.
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.
15 enum cat_type { tuxedo, tabby, tortoiseshell }
17 impl Copy for cat_type {}
19 impl cmp::PartialEq for cat_type {
20 fn eq(&self, other: &cat_type) -> bool {
21 ((*self) as uint) == ((*other) as uint)
23 fn ne(&self, other: &cat_type) -> bool { !(*self).eq(other) }
26 // Very silly -- this just returns the value of the name field
27 // for any int value that's less than the meows field
29 // ok: T should be in scope when resolving the trait ref for map
31 // Yes, you can have negative meows
39 pub fn speak(&mut self) { self.meow(); }
41 pub fn eat(&mut self) -> bool {
42 if self.how_hungry > 0 {
43 println!("OM NOM NOM");
47 println!("Not hungry!");
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 }
56 fn find(&self, k: &int) -> Option<&T> {
63 fn insert(&mut self, k: int, _: T) -> bool {
68 fn find_mut(&mut self, _k: &int) -> Option<&mut T> { panic!() }
70 fn remove(&mut self, k: &int) -> bool {
71 if self.find(k).is_some() {
72 self.meows -= *k; true
78 fn pop(&mut self, _k: &int) -> Option<T> { panic!() }
80 fn swap(&mut self, _k: int, _v: T) -> Option<T> { panic!() }
84 pub fn get(&self, k: &int) -> &T {
87 None => { panic!("epic fail"); }
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 }
99 println!("Meow {}", self.meows);
100 if self.meows % 5 == 0 {
101 self.how_hungry += 1;
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);