PageRenderTime 52ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://bitbucket.org/graydon/rust
Rust | 54 lines | 44 code | 9 blank | 1 comment | 4 complexity | c37a9a404cc1bcdab427faaa8bfbdc9c MD5 | raw file
  1. // error-pattern: attempted access of field `eat` on type `@noisy`
  2. trait noisy {
  3. fn speak();
  4. }
  5. struct cat {
  6. priv mut meows : uint,
  7. mut how_hungry : int,
  8. name : str,
  9. }
  10. impl cat {
  11. fn eat() -> bool {
  12. if self.how_hungry > 0 {
  13. error!("OM NOM NOM");
  14. self.how_hungry -= 2;
  15. return true;
  16. }
  17. else {
  18. error!("Not hungry!");
  19. return false;
  20. }
  21. }
  22. }
  23. impl cat : noisy {
  24. fn speak() { self.meow(); }
  25. }
  26. priv impl cat {
  27. fn meow() {
  28. error!("Meow");
  29. self.meows += 1u;
  30. if self.meows % 5u == 0u {
  31. self.how_hungry += 1;
  32. }
  33. }
  34. }
  35. fn cat(in_x : uint, in_y : int, in_name: str) -> cat {
  36. cat {
  37. meows: in_x,
  38. how_hungry: in_y,
  39. name: in_name
  40. }
  41. }
  42. fn main() {
  43. let nyan : noisy = cat(0u, 2, "nyan") as noisy;
  44. nyan.eat();
  45. }