Advertisement
Guest User

Untitled

a guest
Oct 8th, 2015
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.63 KB | None | 0 0
  1. use std::ops::Deref;
  2.  
  3. pub struct Foo {
  4. baz: Bar,
  5. }
  6. pub struct Bar;
  7.  
  8. impl Foo {
  9. pub fn foo_method(&self) {
  10. /* do something, doesn't really matter */
  11. }
  12. }
  13.  
  14. impl Bar {
  15. pub fn bar_method(&self) {
  16.  
  17. }
  18. }
  19.  
  20. impl Deref for Foo {
  21. type Target = Bar;
  22.  
  23. fn deref<'a>(&'a self) -> &'a Self::Target {
  24. &self.baz
  25. }
  26. }
  27.  
  28. impl Deref for Bar {
  29. type Target = Foo;
  30.  
  31. fn deref<'a>(&'a self) -> &'a Self::Target {
  32. panic!()
  33. }
  34. }
  35.  
  36. fn main() {
  37. let foo = Foo {
  38. baz: Bar,
  39. };
  40. let bar = Bar;
  41.  
  42. // should work
  43. foo.bar_method();
  44. // should compile but panic
  45. bar.foo_method();
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement