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 {
|
2013-03-22 13:23:21 -05:00
|
|
|
fn eq(&self, other: &cat_type) -> bool {
|
2012-11-14 20:59:30 -06:00
|
|
|
((*self) as uint) == ((*other) as uint)
|
2012-08-27 18:26:35 -05:00
|
|
|
}
|
2013-03-22 13:23:21 -05:00
|
|
|
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
|
2013-02-25 17:14:33 -06:00
|
|
|
priv meows : int,
|
2012-05-07 16:42:28 -05:00
|
|
|
|
2013-02-25 17:14:33 -06:00
|
|
|
how_hungry : int,
|
2013-02-01 00:58:35 -06:00
|
|
|
name : T,
|
2012-09-07 21:04:40 -05:00
|
|
|
}
|
2012-05-07 16:42:28 -05:00
|
|
|
|
2013-02-26 19:47:41 -06:00
|
|
|
pub impl<T> cat<T> {
|
2013-02-01 00:58:35 -06:00
|
|
|
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-26 13:34:00 -06:00
|
|
|
impl<T> BaseIter<(int, &'self T)> for cat<T> {
|
2013-03-22 13:23:21 -05:00
|
|
|
fn each(&self, f: &fn(&(int, &'self T)) -> bool) {
|
2013-02-07 16:49:57 -06:00
|
|
|
let mut n = int::abs(self.meows);
|
|
|
|
while n > 0 {
|
|
|
|
if !f(&(n, &self.name)) { break; }
|
|
|
|
n -= 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-22 13:23:21 -05:00
|
|
|
fn size_hint(&self) -> Option<uint> { Some(self.len()) }
|
2013-02-07 16:49:57 -06:00
|
|
|
}
|
|
|
|
|
2013-02-14 13:47:00 -06:00
|
|
|
impl<T> Container for cat<T> {
|
2013-03-22 13:23:21 -05:00
|
|
|
fn len(&const self) -> uint { self.meows as uint }
|
|
|
|
fn is_empty(&const self) -> bool { self.meows == 0 }
|
2013-02-01 00:58:35 -06:00
|
|
|
}
|
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-03-22 13:23:21 -05:00
|
|
|
fn contains_key(&self, k: &int) -> bool { *k <= self.meows }
|
2012-11-11 03:01:37 -06:00
|
|
|
|
2013-03-22 13:23:21 -05:00
|
|
|
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-03-22 13:23:21 -05:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2013-03-13 16:07:23 -05:00
|
|
|
fn mutate_values(&mut self, f: &fn(&int, &mut T) -> bool) {
|
|
|
|
fail!(~"nope")
|
|
|
|
}
|
|
|
|
|
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-03-22 13:23:21 -05:00
|
|
|
fn find(&self, k: &int) -> Option<&'self T> {
|
2013-02-01 00:58:35 -06:00
|
|
|
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 {
|
2013-02-25 17:14:33 -06:00
|
|
|
if self.find(k).is_some() {
|
|
|
|
self.meows -= *k; true
|
|
|
|
} else {
|
|
|
|
false
|
2013-02-01 00:58:35 -06:00
|
|
|
}
|
|
|
|
}
|
2012-05-07 16:42:28 -05:00
|
|
|
}
|
|
|
|
|
2013-02-26 19:47:41 -06:00
|
|
|
pub impl<T> cat<T> {
|
2013-03-22 13:23:21 -05:00
|
|
|
fn get(&self, k: &int) -> &'self T {
|
2013-02-01 00:58:35 -06:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-22 13:23:21 -05:00
|
|
|
fn new(in_x: int, in_y: int, in_name: T) -> cat<T> {
|
2013-02-01 00:58:35 -06:00
|
|
|
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(); }
|
2013-03-06 15:58:02 -06:00
|
|
|
fail_unless!((*nyan.find(&1).unwrap() == ~"nyan"));
|
|
|
|
fail_unless!((nyan.find(&10) == None));
|
2013-02-01 00:58:35 -06:00
|
|
|
let mut spotty: cat<cat_type> = cat::new(2, 57, tuxedo);
|
|
|
|
for uint::range(0, 6) |_| { spotty.speak(); }
|
2013-03-06 15:58:02 -06:00
|
|
|
fail_unless!((spotty.len() == 8));
|
|
|
|
fail_unless!((spotty.contains_key(&2)));
|
|
|
|
fail_unless!((spotty.get(&3) == &tuxedo));
|
2012-05-07 16:42:28 -05:00
|
|
|
}
|