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 cmp::PartialEq for cat_type {
18 fn eq(&self, other: &cat_type) -> bool {
19 ((*self) as uint) == ((*other) as uint)
21 fn ne(&self, other: &cat_type) -> bool { !(*self).eq(other) }
24 // Very silly -- this just returns the value of the name field
25 // for any int value that's less than the meows field
27 // ok: T should be in scope when resolving the trait ref for map
29 // Yes, you can have negative meows
37 pub fn speak(&mut self) { self.meow(); }
39 pub fn eat(&mut self) -> bool {
40 if self.how_hungry > 0 {
41 println!("OM NOM NOM");
45 println!("Not hungry!");
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 }
54 fn find(&self, k: &int) -> Option<&T> {
61 fn insert(&mut self, k: int, _: T) -> bool {
66 fn find_mut(&mut self, _k: &int) -> Option<&mut T> { panic!() }
68 fn remove(&mut self, k: &int) -> bool {
69 if self.find(k).is_some() {
70 self.meows -= *k; true
76 fn pop(&mut self, _k: &int) -> Option<T> { panic!() }
78 fn swap(&mut self, _k: int, _v: T) -> Option<T> { panic!() }
82 pub fn get(&self, k: &int) -> &T {
85 None => { panic!("epic fail"); }
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 }
97 println!("Meow {}", self.meows);
98 if self.meows % 5 == 0 {
105 let mut nyan: cat<String> = cat::new(0, 2, "nyan".to_string());
106 for _ in range(1u, 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, tuxedo);
110 for _ in range(0u, 6) { spotty.speak(); }
111 assert_eq!(spotty.len(), 8);
112 assert!((spotty.contains_key(&2)));
113 assert_eq!(spotty.get(&3), &tuxedo);