Guest User

Untitled

a guest
Dec 12th, 2018
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.37 KB | None | 0 0
  1. trait StateSet {
  2. fn next(self) -> (u32, Self);
  3. }
  4.  
  5. trait State {
  6. type S: StateSet;
  7. fn next(self) -> (u32, Self::S);
  8. }
  9.  
  10. macro_rules! delegate {
  11. ($fn:ident => enum $name:ident { $($variant:tt,)*}) => {
  12. #[derive(Debug)]
  13. enum $name {
  14. $($variant($variant),)+
  15. }
  16.  
  17. impl StateSet for $name {
  18. fn next(self) -> (u32, Self) {
  19. match self {
  20. $(
  21. $name::$variant(e) => e.$fn(),
  22. )+
  23. }
  24. }
  25. }
  26. }
  27. }
  28.  
  29. #[derive(Debug)]
  30. struct First(u32);
  31. #[derive(Debug)]
  32. struct Second;
  33. #[derive(Debug)]
  34. struct Third;
  35.  
  36. impl State for First {
  37. type S = SMTP;
  38. fn next(self) -> (u32, Self::S) {
  39. (self.0 + 1, SMTP::Second(Second))
  40. }
  41. }
  42.  
  43. impl State for Second {
  44. type S = SMTP;
  45. fn next(self) -> (u32, Self::S) {
  46. (2, SMTP::Third(Third))
  47. }
  48. }
  49.  
  50. impl State for Third {
  51. type S = SMTP;
  52. fn next(self) -> (u32, Self::S) {
  53. (3, SMTP::First(First(4)))
  54. }
  55. }
  56.  
  57. delegate!{next =>
  58. enum SMTP {
  59. First,
  60. Second,
  61. Third,
  62. }
  63. }
  64.  
  65. fn main() {
  66. let machine = SMTP::First(First(0));
  67. let (n, machine) = machine.next();
  68. println!("{:?}", (n, &machine));
  69. let (n, machine) = machine.next();
  70. println!("{:?}", (n, &machine));
  71. let (n, machine) = machine.next();
  72. println!("{:?}", (n, &machine));
  73. }
Add Comment
Please, Sign In to add comment