rust/src/test/compile-fail/class-cast-to-trait.rs

54 lines
846 B
Rust
Raw Normal View History

trait noisy {
fn speak();
}
struct cat {
priv mut meows : uint,
2012-09-06 19:40:15 -07:00
mut how_hungry : int,
name : ~str,
}
impl cat {
fn eat() -> bool {
if self.how_hungry > 0 {
2012-08-22 17:24:52 -07:00
error!("OM NOM NOM");
self.how_hungry -= 2;
2012-08-01 17:30:05 -07:00
return true;
}
else {
2012-08-22 17:24:52 -07:00
error!("Not hungry!");
2012-08-01 17:30:05 -07:00
return false;
}
}
}
impl cat : noisy {
fn speak() { self.meow(); }
}
priv impl cat {
fn meow() {
error!("Meow");
self.meows += 1;
if self.meows % 5 == 0 {
self.how_hungry += 1;
}
}
}
fn cat(in_x : uint, in_y : int, in_name: ~str) -> cat {
2012-09-05 15:58:43 -07:00
cat {
meows: in_x,
how_hungry: in_y,
name: in_name
}
}
fn main() {
let nyan : noisy = cat(0, 2, ~"nyan") as noisy;
2012-12-01 15:59:04 -08:00
nyan.eat(); //~ ERROR type `@noisy` does not implement any method in scope named `eat`
}