Guest User

Untitled

a guest
Jul 16th, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.94 KB | None | 0 0
  1. use std::io::Error;
  2.  
  3. pub trait State {
  4. fn handle(self) -> Result<Box<State>, Error>;
  5. }
  6.  
  7. struct A {}
  8. impl State for A {
  9. fn handle(self) -> Result<Box<State>, Error> {
  10. println!("Running handle of A");
  11. Ok(Box::new(B {}))
  12. }
  13. }
  14.  
  15. struct B {}
  16. impl State for B {
  17. fn handle(self) -> Result<Box<State>, Error> {
  18. println!("Running handle of B");
  19. Ok(Box::new(A {}))
  20. }
  21. }
  22.  
  23. #[test]
  24. fn assert_works() {
  25. macro_rules! assert_state {
  26. ($machine:ident, $state:ident) => {
  27. use std::any::Any;
  28. let any: Box<Any> = Box::new($machine);
  29. assert!(
  30. if let Ok(_) = any.downcast::<$state>() {
  31. true
  32. } else {
  33. false
  34. },
  35. "Failed to get to {} state.",
  36. stringify!($state)
  37. );
  38. };
  39. }
  40.  
  41. let to_b = Box::new(A {}).handle().unwrap();
  42.  
  43. assert_state!(to_b, B);
  44. }
Add Comment
Please, Sign In to add comment