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
|
|
|
#[legacy_modes];
|
|
|
|
|
2012-07-31 17:27:51 +00:00
|
|
|
trait noisy {
|
2012-04-11 23:18:00 +00:00
|
|
|
fn speak();
|
|
|
|
}
|
|
|
|
|
2012-09-08 02:04:40 +00:00
|
|
|
struct cat {
|
2012-09-11 01:56:07 +00:00
|
|
|
priv mut meows : uint,
|
2012-09-08 02:04:40 +00:00
|
|
|
|
|
|
|
mut how_hungry : int,
|
|
|
|
name : ~str,
|
|
|
|
}
|
|
|
|
|
|
|
|
priv impl cat {
|
2012-04-11 23:18:00 +00:00
|
|
|
fn meow() {
|
2012-08-23 00:24:52 +00:00
|
|
|
error!("Meow");
|
2012-04-11 23:18:00 +00:00
|
|
|
self.meows += 1u;
|
|
|
|
if self.meows % 5u == 0u {
|
|
|
|
self.how_hungry += 1;
|
|
|
|
}
|
|
|
|
}
|
2012-09-08 02:04:40 +00:00
|
|
|
}
|
2012-04-11 23:18:00 +00:00
|
|
|
|
2012-09-08 02:04:40 +00:00
|
|
|
impl cat {
|
2012-04-11 23:18:00 +00:00
|
|
|
fn eat() -> bool {
|
|
|
|
if self.how_hungry > 0 {
|
2012-08-23 00:24:52 +00:00
|
|
|
error!("OM NOM NOM");
|
2012-04-11 23:18:00 +00:00
|
|
|
self.how_hungry -= 2;
|
2012-08-02 00:30:05 +00:00
|
|
|
return true;
|
2012-04-11 23:18:00 +00:00
|
|
|
}
|
|
|
|
else {
|
2012-08-23 00:24:52 +00:00
|
|
|
error!("Not hungry!");
|
2012-08-02 00:30:05 +00:00
|
|
|
return false;
|
2012-04-11 23:18:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-14 19:47:00 +00:00
|
|
|
impl noisy for cat {
|
2012-09-08 02:04:40 +00:00
|
|
|
fn speak() { self.meow(); }
|
|
|
|
}
|
|
|
|
|
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,
|
2012-12-05 19:40:47 +00:00
|
|
|
name: copy in_name
|
2012-09-05 22:58:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-02-21 01:07:17 +00:00
|
|
|
fn make_speak<C:noisy>(c: C) {
|
2012-05-24 13:20:46 +00:00
|
|
|
c.speak();
|
|
|
|
}
|
|
|
|
|
2013-02-02 03:43:17 +00:00
|
|
|
pub fn main() {
|
2012-07-14 05:57:48 +00:00
|
|
|
let nyan = cat(0u, 2, ~"nyan");
|
2012-04-11 23:18:00 +00:00
|
|
|
nyan.eat();
|
|
|
|
assert(!nyan.eat());
|
2012-06-30 23:19:07 +00:00
|
|
|
for uint::range(1u, 10u) |_i| { make_speak(nyan); };
|
2012-04-11 23:18:00 +00:00
|
|
|
assert(nyan.eat());
|
2012-09-18 22:52:21 +00:00
|
|
|
}
|