2014-02-07 19:08:32 +00:00
|
|
|
// Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT
|
2012-12-11 01:32:48 +00:00
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
2014-02-07 19:08:32 +00:00
|
|
|
// ignore-fast
|
2012-09-18 22:52:21 +00:00
|
|
|
|
2013-05-25 02:35:29 +00:00
|
|
|
use std::cmp;
|
2012-05-07 21:42:28 +00:00
|
|
|
|
2014-02-28 09:23:06 +00:00
|
|
|
#[deriving(Show)]
|
2012-05-07 21:42:28 +00:00
|
|
|
enum cat_type { tuxedo, tabby, tortoiseshell }
|
|
|
|
|
2013-02-14 19:47:00 +00:00
|
|
|
impl cmp::Eq for cat_type {
|
2013-03-22 18:23:21 +00:00
|
|
|
fn eq(&self, other: &cat_type) -> bool {
|
2012-11-15 02:59:30 +00:00
|
|
|
((*self) as uint) == ((*other) as uint)
|
2012-08-27 23:26:35 +00:00
|
|
|
}
|
2013-03-22 18:23:21 +00:00
|
|
|
fn ne(&self, other: &cat_type) -> bool { !(*self).eq(other) }
|
2012-08-27 23:26:35 +00:00
|
|
|
}
|
|
|
|
|
2012-05-07 21:42:28 +00:00
|
|
|
// Very silly -- this just returns the value of the name field
|
|
|
|
// for any int value that's less than the meows field
|
|
|
|
|
2012-07-31 17:27:51 +00:00
|
|
|
// ok: T should be in scope when resolving the trait ref for map
|
2013-01-28 18:46:43 +00:00
|
|
|
struct cat<T> {
|
2013-02-01 06:58:35 +00:00
|
|
|
// Yes, you can have negative meows
|
2014-01-24 19:02:03 +00:00
|
|
|
meows : int,
|
2012-05-07 21:42:28 +00:00
|
|
|
|
2013-02-25 23:14:33 +00:00
|
|
|
how_hungry : int,
|
2013-02-01 06:58:35 +00:00
|
|
|
name : T,
|
2012-09-08 02:04:40 +00:00
|
|
|
}
|
2012-05-07 21:42:28 +00:00
|
|
|
|
2013-05-31 22:17:22 +00:00
|
|
|
impl<T> cat<T> {
|
|
|
|
pub fn speak(&mut self) { self.meow(); }
|
2013-02-01 06:58:35 +00:00
|
|
|
|
2013-05-31 22:17:22 +00:00
|
|
|
pub fn eat(&mut self) -> bool {
|
2013-02-01 06:58:35 +00:00
|
|
|
if self.how_hungry > 0 {
|
2013-10-21 20:08:31 +00:00
|
|
|
error!("OM NOM NOM");
|
2013-02-01 06:58:35 +00:00
|
|
|
self.how_hungry -= 2;
|
|
|
|
return true;
|
|
|
|
} else {
|
2013-10-21 20:08:31 +00:00
|
|
|
error!("Not hungry!");
|
2013-02-01 06:58:35 +00:00
|
|
|
return false;
|
|
|
|
}
|
2012-05-07 21:42:28 +00:00
|
|
|
}
|
2012-09-08 02:04:40 +00:00
|
|
|
}
|
2012-05-07 21:42:28 +00:00
|
|
|
|
2013-02-14 19:47:00 +00:00
|
|
|
impl<T> Container for cat<T> {
|
2013-08-03 04:41:06 +00:00
|
|
|
fn len(&self) -> uint { self.meows as uint }
|
|
|
|
fn is_empty(&self) -> bool { self.meows == 0 }
|
2013-02-01 06:58:35 +00:00
|
|
|
}
|
2012-11-11 09:01:37 +00:00
|
|
|
|
2013-02-14 19:47:00 +00:00
|
|
|
impl<T> Mutable for cat<T> {
|
2013-02-01 06:58:35 +00:00
|
|
|
fn clear(&mut self) {}
|
|
|
|
}
|
2012-11-21 07:33:31 +00:00
|
|
|
|
2013-02-14 19:47:00 +00:00
|
|
|
impl<T> Map<int, T> for cat<T> {
|
2013-03-22 18:23:21 +00:00
|
|
|
fn contains_key(&self, k: &int) -> bool { *k <= self.meows }
|
2012-11-11 09:01:37 +00:00
|
|
|
|
2013-04-09 00:43:55 +00:00
|
|
|
fn find<'a>(&'a self, k: &int) -> Option<&'a T> {
|
2013-02-01 06:58:35 +00:00
|
|
|
if *k <= self.meows {
|
|
|
|
Some(&self.name)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
2013-07-14 02:44:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<T> MutableMap<int, T> for cat<T> {
|
|
|
|
fn insert(&mut self, k: int, _: T) -> bool {
|
|
|
|
self.meows += k;
|
|
|
|
true
|
|
|
|
}
|
2012-08-02 22:42:56 +00:00
|
|
|
|
2013-10-21 20:08:31 +00:00
|
|
|
fn find_mut<'a>(&'a mut self, _k: &int) -> Option<&'a mut T> { fail!() }
|
2013-03-25 00:40:17 +00:00
|
|
|
|
2013-02-01 06:58:35 +00:00
|
|
|
fn remove(&mut self, k: &int) -> bool {
|
2013-02-25 23:14:33 +00:00
|
|
|
if self.find(k).is_some() {
|
|
|
|
self.meows -= *k; true
|
|
|
|
} else {
|
|
|
|
false
|
2013-02-01 06:58:35 +00:00
|
|
|
}
|
|
|
|
}
|
2013-05-04 13:54:58 +00:00
|
|
|
|
2013-10-21 20:08:31 +00:00
|
|
|
fn pop(&mut self, _k: &int) -> Option<T> { fail!() }
|
2013-05-04 13:54:58 +00:00
|
|
|
|
2013-10-21 20:08:31 +00:00
|
|
|
fn swap(&mut self, _k: int, _v: T) -> Option<T> { fail!() }
|
2012-05-07 21:42:28 +00:00
|
|
|
}
|
|
|
|
|
2013-05-31 22:17:22 +00:00
|
|
|
impl<T> cat<T> {
|
|
|
|
pub fn get<'a>(&'a self, k: &int) -> &'a T {
|
2013-02-01 06:58:35 +00:00
|
|
|
match self.find(k) {
|
|
|
|
Some(v) => { v }
|
2013-10-21 20:08:31 +00:00
|
|
|
None => { fail!("epic fail"); }
|
2013-02-01 06:58:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-31 22:17:22 +00:00
|
|
|
pub fn new(in_x: int, in_y: int, in_name: T) -> cat<T> {
|
2013-02-01 06:58:35 +00:00
|
|
|
cat{meows: in_x, how_hungry: in_y, name: in_name }
|
2012-09-08 02:04:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-31 22:17:22 +00:00
|
|
|
impl<T> cat<T> {
|
2013-02-01 06:58:35 +00:00
|
|
|
fn meow(&mut self) {
|
|
|
|
self.meows += 1;
|
2013-10-21 20:08:31 +00:00
|
|
|
error!("Meow {}", self.meows);
|
2013-02-01 06:58:35 +00:00
|
|
|
if self.meows % 5 == 0 {
|
|
|
|
self.how_hungry += 1;
|
|
|
|
}
|
2012-09-05 22:58:43 +00:00
|
|
|
}
|
|
|
|
}
|
2012-05-07 21:42:28 +00:00
|
|
|
|
2013-02-17 22:36:43 +00:00
|
|
|
pub fn main() {
|
2013-02-01 06:58:35 +00:00
|
|
|
let mut nyan: cat<~str> = cat::new(0, 2, ~"nyan");
|
2013-08-03 16:45:23 +00:00
|
|
|
for _ in range(1u, 5) { nyan.speak(); }
|
2013-05-19 02:02:45 +00:00
|
|
|
assert!(*nyan.find(&1).unwrap() == ~"nyan");
|
|
|
|
assert_eq!(nyan.find(&10), None);
|
2013-02-01 06:58:35 +00:00
|
|
|
let mut spotty: cat<cat_type> = cat::new(2, 57, tuxedo);
|
2013-08-03 16:45:23 +00:00
|
|
|
for _ in range(0u, 6) { spotty.speak(); }
|
2013-05-19 02:02:45 +00:00
|
|
|
assert_eq!(spotty.len(), 8);
|
2013-03-29 01:39:09 +00:00
|
|
|
assert!((spotty.contains_key(&2)));
|
2013-05-19 02:02:45 +00:00
|
|
|
assert_eq!(spotty.get(&3), &tuxedo);
|
2012-05-07 21:42:28 +00:00
|
|
|
}
|