Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- use std::io::Error;
- pub trait State {
- fn handle(self) -> Result<Box<State>, Error>;
- }
- struct A {}
- impl State for A {
- fn handle(self) -> Result<Box<State>, Error> {
- println!("Running handle of A");
- Ok(Box::new(B {}))
- }
- }
- struct B {}
- impl State for B {
- fn handle(self) -> Result<Box<State>, Error> {
- println!("Running handle of B");
- Ok(Box::new(A {}))
- }
- }
- #[test]
- fn assert_works() {
- macro_rules! assert_state {
- ($machine:ident, $state:ident) => {
- use std::any::Any;
- let any: Box<Any> = Box::new($machine);
- assert!(
- if let Ok(_) = any.downcast::<$state>() {
- true
- } else {
- false
- },
- "Failed to get to {} state.",
- stringify!($state)
- );
- };
- }
- let to_b = Box::new(A {}).handle().unwrap();
- assert_state!(to_b, B);
- }
Add Comment
Please, Sign In to add comment