2012-12-10 19:32:48 -06:00
|
|
|
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// 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.
|
|
|
|
|
2012-09-19 15:59:44 -05:00
|
|
|
// xfail-fast
|
2012-09-18 17:52:21 -05:00
|
|
|
|
2013-02-01 00:58:35 -06:00
|
|
|
use core::container::{Container, Mutable, Map};
|
2013-02-07 16:49:57 -06:00
|
|
|
use core::iter::BaseIter;
|
2012-05-07 16:42:28 -05:00
|
|
|
|
|
|
|
enum cat_type { tuxedo, tabby, tortoiseshell }
|
|
|
|
|
2013-02-14 13:47:00 -06:00
|
|
|
impl cmp::Eq for cat_type {
|
2012-11-14 20:59:30 -06:00
|
|
|
pure fn eq(&self, other: &cat_type) -> bool {
|
|
|
|
((*self) as uint) == ((*other) as uint)
|
2012-08-27 18:26:35 -05:00
|
|
|
}
|
2012-11-14 20:59:30 -06:00
|
|
|
pure fn ne(&self, other: &cat_type) -> bool { !(*self).eq(other) }
|
2012-08-27 18:26:35 -05:00
|
|
|
}
|
|
|
|
|
2012-05-07 16:42:28 -05: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 12:27:51 -05:00
|
|
|
// ok: T should be in scope when resolving the trait ref for map
|
2013-01-28 12:46:43 -06:00
|
|
|
struct cat<T> {
|
2013-02-01 00:58:35 -06:00
|
|
|
// Yes, you can have negative meows
|
|
|
|
priv mut meows : int,
|
2012-05-07 16:42:28 -05:00
|
|
|
|
2013-02-01 00:58:35 -06:00
|
|
|
mut how_hungry : int,
|
|
|
|
name : T,
|
2012-09-07 21:04:40 -05:00
|
|
|
}
|
2012-05-07 16:42:28 -05:00
|
|
|
|
2013-02-01 00:58:35 -06:00
|
|
|
impl<T> cat<T> {
|
|
|
|
fn speak(&mut self) { self.meow(); }
|
|
|
|
|
|
|
|
fn eat(&mut self) -> bool {
|
|
|
|
if self.how_hungry > 0 {
|
|
|
|
error!("OM NOM NOM");
|
|
|
|
self.how_hungry -= 2;
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
error!("Not hungry!");
|
|
|
|
return false;
|
|
|
|
}
|
2012-05-07 16:42:28 -05:00
|
|
|
}
|
2012-09-07 21:04:40 -05:00
|
|
|
}
|
2012-05-07 16:42:28 -05:00
|
|
|
|
2013-02-14 13:47:00 -06:00
|
|
|
impl<T> BaseIter<(int, &T)> for cat<T> {
|
2013-02-07 16:49:57 -06:00
|
|
|
pure fn each(&self, f: fn(&(int, &self/T)) -> bool) {
|
|
|
|
let mut n = int::abs(self.meows);
|
|
|
|
while n > 0 {
|
|
|
|
if !f(&(n, &self.name)) { break; }
|
|
|
|
n -= 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pure fn size_hint(&self) -> Option<uint> { Some(self.len()) }
|
|
|
|
}
|
|
|
|
|
2013-02-14 13:47:00 -06:00
|
|
|
impl<T> Container for cat<T> {
|
2013-02-01 00:58:35 -06:00
|
|
|
pure fn len(&self) -> uint { self.meows as uint }
|
|
|
|
pure fn is_empty(&self) -> bool { self.meows == 0 }
|
|
|
|
}
|
2012-11-11 03:01:37 -06:00
|
|
|
|
2013-02-14 13:47:00 -06:00
|
|
|
impl<T> Mutable for cat<T> {
|
2013-02-01 00:58:35 -06:00
|
|
|
fn clear(&mut self) {}
|
|
|
|
}
|
2012-11-21 01:33:31 -06:00
|
|
|
|
2013-02-14 13:47:00 -06:00
|
|
|
impl<T> Map<int, T> for cat<T> {
|
2013-02-01 00:58:35 -06:00
|
|
|
pure fn contains_key(&self, k: &int) -> bool { *k <= self.meows }
|
2012-11-11 03:01:37 -06:00
|
|
|
|
2013-02-01 00:58:35 -06:00
|
|
|
pure fn each_key(&self, f: fn(v: &int) -> bool) {
|
2013-02-07 16:49:57 -06:00
|
|
|
for self.each |&(k, _)| { if !f(&k) { break; } loop;};
|
2012-05-07 16:42:28 -05:00
|
|
|
}
|
2012-06-04 13:21:11 -05:00
|
|
|
|
2013-02-01 00:58:35 -06:00
|
|
|
pure fn each_value(&self, f: fn(v: &T) -> bool) {
|
2013-02-07 16:49:57 -06:00
|
|
|
for self.each |&(_, v)| { if !f(v) { break; } loop;};
|
2013-02-01 00:58:35 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
fn insert(&mut self, k: int, _: T) -> bool {
|
|
|
|
self.meows += k;
|
|
|
|
true
|
|
|
|
}
|
2012-08-02 17:42:56 -05:00
|
|
|
|
2013-02-01 00:58:35 -06:00
|
|
|
pure fn find(&self, k: &int) -> Option<&self/T> {
|
|
|
|
if *k <= self.meows {
|
|
|
|
Some(&self.name)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
2012-08-02 17:42:56 -05:00
|
|
|
|
2013-02-01 00:58:35 -06:00
|
|
|
fn remove(&mut self, k: &int) -> bool {
|
|
|
|
match self.find(k) {
|
|
|
|
Some(_) => {
|
|
|
|
self.meows -= *k; true
|
|
|
|
}
|
|
|
|
None => { false }
|
|
|
|
}
|
|
|
|
}
|
2012-05-07 16:42:28 -05:00
|
|
|
}
|
|
|
|
|
2013-02-01 00:58:35 -06:00
|
|
|
impl<T> cat<T> {
|
|
|
|
pure fn get(&self, k: &int) -> &self/T {
|
|
|
|
match self.find(k) {
|
|
|
|
Some(v) => { v }
|
2013-02-11 21:26:38 -06:00
|
|
|
None => { fail!(~"epic fail"); }
|
2013-02-01 00:58:35 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static pure fn new(in_x: int, in_y: int, in_name: T) -> cat<T> {
|
|
|
|
cat{meows: in_x, how_hungry: in_y, name: in_name }
|
2012-09-07 21:04:40 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-01 00:58:35 -06:00
|
|
|
priv impl<T> cat<T> {
|
|
|
|
fn meow(&mut self) {
|
|
|
|
self.meows += 1;
|
|
|
|
error!("Meow %d", self.meows);
|
|
|
|
if self.meows % 5 == 0 {
|
|
|
|
self.how_hungry += 1;
|
|
|
|
}
|
2012-09-05 17:58:43 -05:00
|
|
|
}
|
|
|
|
}
|
2012-05-07 16:42:28 -05:00
|
|
|
|
2013-02-17 16:36:43 -06:00
|
|
|
pub fn main() {
|
2013-02-01 00:58:35 -06:00
|
|
|
let mut nyan: cat<~str> = cat::new(0, 2, ~"nyan");
|
|
|
|
for uint::range(1, 5) |_| { nyan.speak(); }
|
|
|
|
assert(*nyan.find(&1).unwrap() == ~"nyan");
|
|
|
|
assert(nyan.find(&10) == None);
|
|
|
|
let mut spotty: cat<cat_type> = cat::new(2, 57, tuxedo);
|
|
|
|
for uint::range(0, 6) |_| { spotty.speak(); }
|
|
|
|
assert(spotty.len() == 8);
|
|
|
|
assert(spotty.contains_key(&2));
|
|
|
|
assert(spotty.get(&3) == &tuxedo);
|
2012-05-07 16:42:28 -05:00
|
|
|
}
|