3 #![allow(non_camel_case_types)]
7 #[derive(Copy, Clone, Debug)]
8 enum cat_type { tuxedo, tabby, tortoiseshell }
10 impl cmp::PartialEq for cat_type {
11 fn eq(&self, other: &cat_type) -> bool {
12 ((*self) as usize) == ((*other) as usize)
14 fn ne(&self, other: &cat_type) -> bool { !(*self).eq(other) }
17 // Very silly -- this just returns the value of the name field
18 // for any isize value that's less than the meows field
20 // ok: T should be in scope when resolving the trait ref for map
22 // Yes, you can have negative meows
30 pub fn speak(&mut self) { self.meow(); }
32 pub fn eat(&mut self) -> bool {
33 if self.how_hungry > 0 {
34 println!("OM NOM NOM");
38 println!("Not hungry!");
42 fn len(&self) -> usize { self.meows as usize }
43 fn is_empty(&self) -> bool { self.meows == 0 }
44 fn clear(&mut self) {}
45 fn contains_key(&self, k: &isize) -> bool { *k <= self.meows }
47 fn find(&self, k: &isize) -> Option<&T> {
54 fn insert(&mut self, k: isize, _: T) -> bool {
59 fn find_mut(&mut self, _k: &isize) -> Option<&mut T> { panic!() }
61 fn remove(&mut self, k: &isize) -> bool {
62 if self.find(k).is_some() {
63 self.meows -= *k; true
69 fn pop(&mut self, _k: &isize) -> Option<T> { panic!() }
71 fn swap(&mut self, _k: isize, _v: T) -> Option<T> { panic!() }
75 pub fn get(&self, k: &isize) -> &T {
78 None => { panic!("epic fail"); }
82 pub fn new(in_x: isize, in_y: isize, in_name: T) -> cat<T> {
83 cat{meows: in_x, how_hungry: in_y, name: in_name }
90 println!("Meow {}", self.meows);
91 if self.meows % 5 == 0 {
98 let mut nyan: cat<String> = cat::new(0, 2, "nyan".to_string());
99 for _ in 1_usize..5 { nyan.speak(); }
100 assert_eq!(*nyan.find(&1).unwrap(), "nyan".to_string());
101 assert_eq!(nyan.find(&10), None);
102 let mut spotty: cat<cat_type> = cat::new(2, 57, cat_type::tuxedo);
103 for _ in 0_usize..6 { spotty.speak(); }
104 assert_eq!(spotty.len(), 8);
105 assert!((spotty.contains_key(&2)));
106 assert_eq!(spotty.get(&3), &cat_type::tuxedo);