Guest User

Untitled

a guest
Jan 22nd, 2019
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.79 KB | None | 0 0
  1. trait DoerMaker {
  2. type T: StuffDoer;
  3. fn make_doer(&self, f: Foo) -> Self::T;
  4. }
  5.  
  6. trait StuffDoer {
  7. fn do_stuff(&self) -> Option<()>;
  8. }
  9.  
  10. impl<T: StuffDoer + ?Sized> StuffDoer for Box<T> {
  11. fn do_stuff(&self) -> Option<()> {
  12. (**self).do_stuff()
  13. }
  14. }
  15.  
  16. enum Foo {
  17. A,
  18. B,
  19. C,
  20. }
  21.  
  22. struct Bar;
  23. impl DoerMaker for Bar {
  24. type T=Box<StuffDoer>;
  25.  
  26. fn make_doer(&self, f: Foo) -> Box<StuffDoer> {
  27. match f {
  28. Foo::A => Box::new(Baz{}),
  29. Foo::B => Box::new(Bat{}),
  30. Foo::C => Box::new(Bang{}),
  31. }
  32. }
  33. }
  34.  
  35. struct Baz;
  36. impl StuffDoer for Baz {
  37. fn do_stuff(&self) -> Option<()> {
  38. None
  39. }
  40. }
  41.  
  42. struct Bat;
  43. impl StuffDoer for Bat {
  44. fn do_stuff(&self) -> Option<()> {
  45. None
  46. }
  47. }
  48.  
  49. struct Bang;
  50. impl StuffDoer for Bang {
  51. fn do_stuff(&self) -> Option<()> {
  52. None
  53. }
  54. }
Add Comment
Please, Sign In to add comment