From 7aba2833df2f9ef2e5636571e95abefb0c1cc67d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bastian=20K=C3=B6cher?= Date: Mon, 12 Feb 2018 20:41:47 +0100 Subject: [PATCH] Adds `transition!` macro to simplify state transitions --- README.md | 17 ++++++++++++++--- src/lib.rs | 19 ++++++++++++++++--- src/transition.rs | 5 +++++ tests/into_after.rs | 9 ++++++++- 4 files changed, 43 insertions(+), 7 deletions(-) create mode 100644 src/transition.rs diff --git a/README.md b/README.md index 7ee2584..1e6d705 100644 --- a/README.md +++ b/README.md @@ -12,6 +12,7 @@ boilerplate for you. * [Guide](#guide) * [Example](#example) * [Attributes](#attributes) +* [Macro](#macro) * [Features](#features) * [License](#license) * [Contribution](#contribution) @@ -335,7 +336,7 @@ impl PollGame for Game { active: invite.from, idle: invite.to, }; - Ok(Async::Ready(waiting.into())) + transition!(waiting) } fn poll_waiting_for_turn<'a>( @@ -352,8 +353,7 @@ impl PollGame for Game { // and request the turn over HTTP. let waiting = waiting.take(); if let Some(game_result) = process_turn(turn) { - let finished = Finished(game_result); - Ok(Async::Ready(finished.into())) + transition!(Finished(game_result)) } else { let next_waiting = WaitingForTurn { turn: waiting.idle.request_turn(), @@ -407,6 +407,17 @@ generated `Future` implementation uses the field's type as `Future::Error`. a variant of the state machine description `enum`. Describes the states that this one can transition to. +### Macro + +An auxiliary macro is provided that helps reducing boilerplate code for state +transitions. So, the following code: + +```Ok(Ready(NextState(1).into()))``` + +Can be reduced to: + +```transition!(NextState(1))``` + ### Features Here are the `cargo` features that you can enable: diff --git a/src/lib.rs b/src/lib.rs index 78e1ead..c3172b7 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -12,6 +12,7 @@ boilerplate for you. * [Guide](#guide) * [Example](#example) * [Attributes](#attributes) +* [Macro](#macro) * [Features](#features) * [License](#license) * [Contribution](#contribution) @@ -358,7 +359,7 @@ impl PollGame for Game { active: invite.from, idle: invite.to, }; - Ok(Async::Ready(waiting.into())) + transition!(waiting) } fn poll_waiting_for_turn<'a>( @@ -375,8 +376,7 @@ impl PollGame for Game { // and request the turn over HTTP. let waiting = waiting.take(); if let Some(game_result) = process_turn(turn) { - let finished = Finished(game_result); - Ok(Async::Ready(finished.into())) + transition!(Finished(game_result)) } else { let next_waiting = WaitingForTurn { turn: waiting.idle.request_turn(), @@ -435,6 +435,17 @@ generated `Future` implementation uses the field's type as `Future::Error`. a variant of the state machine description `enum`. Describes the states that this one can transition to. +## Macro + +An auxiliary macro is provided that helps reducing boilerplate code for state +transitions. So, the following code: + +```Ok(Ready(NextState(1).into()))``` + +Can be reduced to: + +```transition!(NextState(1))``` + ## Features Here are the `cargo` features that you can enable: @@ -483,6 +494,8 @@ extern crate derive_state_machine_future; pub use derive_state_machine_future::*; mod compile_fail_tests; +#[macro_use] +mod transition; /// Re-export of `rent_to_own::RentToOwn`. pub type RentToOwn<'a, T> = rent_to_own::RentToOwn<'a, T>; diff --git a/src/transition.rs b/src/transition.rs new file mode 100644 index 0000000..be3f263 --- /dev/null +++ b/src/transition.rs @@ -0,0 +1,5 @@ +/// Auxiliary macro for `poll_state_xy` functions to transition into a new state. +#[macro_export] +macro_rules! transition { + ( $new_state:expr ) => (return Ok(::futures::Async::Ready($new_state.into()))); +} diff --git a/tests/into_after.rs b/tests/into_after.rs index 65bee33..155c374 100644 --- a/tests/into_after.rs +++ b/tests/into_after.rs @@ -13,9 +13,12 @@ pub enum Machine { /// Choose which next state to go into depending on what start value is /// given. #[state_machine_future(start)] - #[state_machine_future(transitions(Ready))] + #[state_machine_future(transitions(Ready, TransitionMacro))] Start, + #[state_machine_future(transitions(Ready))] + TransitionMacro, + #[state_machine_future(ready)] Ready(usize), #[state_machine_future(error)] Error(usize), @@ -25,4 +28,8 @@ impl PollMachine for Machine { fn poll_start<'a>(_: &'a mut RentToOwn<'a, Start>) -> Poll { Ok(Async::Ready(Ready(1).into())) } + + fn poll_transition_macro<'a>(_: &'a mut RentToOwn<'a, TransitionMacro>) -> Poll { + transition!(Ready(2)) + } }