2012-12-11 01:32:48 +00: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 20:59:44 +00:00
|
|
|
// xfail-fast
|
2012-09-18 22:52:21 +00:00
|
|
|
|
2013-05-25 02:35:29 +00:00
|
|
|
use std::uint;
|
|
|
|
|
2012-07-31 17:27:51 +00:00
|
|
|
trait noisy {
|
2013-02-25 23:14:33 +00:00
|
|
|
fn speak(&mut self);
|
2012-04-11 23:18:00 +00:00
|
|
|
}
|
|
|
|
|
2012-09-08 02:04:40 +00:00
|
|
|
struct cat {
|
2013-02-25 23:14:33 +00:00
|
|
|
priv meows : uint,
|
2012-09-08 02:04:40 +00:00
|
|
|
|
2013-02-25 23:14:33 +00:00
|
|
|
how_hungry : int,
|
|
|
|
name : ~str,
|
2012-09-08 02:04:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
priv impl cat {
|
2013-02-25 23:14:33 +00:00
|
|
|
fn meow(&mut self) {
|
|
|
|
error!("Meow");
|
|
|
|
self.meows += 1u;
|
|
|
|
if self.meows % 5u == 0u {
|
|
|
|
self.how_hungry += 1;
|
|
|
|
}
|
2012-04-11 23:18:00 +00:00
|
|
|
}
|
2012-09-08 02:04:40 +00:00
|
|
|
}
|
2012-04-11 23:18:00 +00:00
|
|
|
|
2013-02-27 01:47:41 +00:00
|
|
|
pub impl cat {
|
2013-02-25 23:14:33 +00:00
|
|
|
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-04-11 23:18:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-14 19:47:00 +00:00
|
|
|
impl noisy for cat {
|
2013-02-25 23:14:33 +00:00
|
|
|
fn speak(&mut self) { self.meow(); }
|
2012-09-08 02:04:40 +00:00
|
|
|
}
|
|
|
|
|
2012-09-05 22:58:43 +00:00
|
|
|
fn cat(in_x : uint, in_y : int, in_name: ~str) -> cat {
|
|
|
|
cat {
|
|
|
|
meows: in_x,
|
|
|
|
how_hungry: in_y,
|
2013-03-15 22:27:15 +00:00
|
|
|
name: in_name.clone()
|
2012-09-05 22:58:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-02-25 23:14:33 +00:00
|
|
|
fn make_speak<C:noisy>(mut c: C) {
|
2012-05-24 13:20:46 +00:00
|
|
|
c.speak();
|
|
|
|
}
|
|
|
|
|
2013-02-02 03:43:17 +00:00
|
|
|
pub fn main() {
|
2013-02-25 23:14:33 +00:00
|
|
|
let mut nyan = cat(0u, 2, ~"nyan");
|
2012-04-11 23:18:00 +00:00
|
|
|
nyan.eat();
|
2013-03-29 01:39:09 +00:00
|
|
|
assert!((!nyan.eat()));
|
2013-04-23 04:19:58 +00:00
|
|
|
for uint::range(1u, 10u) |_i| {
|
|
|
|
make_speak(copy nyan);
|
|
|
|
}
|
2012-09-18 22:52:21 +00:00
|
|
|
}
|