Guest User

Untitled

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