Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds transition! macro to simplify state transitions #16

Merged
merged 1 commit into from
Feb 12, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 14 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ boilerplate for you.
* [Guide](#guide)
* [Example](#example)
* [Attributes](#attributes)
* [Macro](#macro)
* [Features](#features)
* [License](#license)
* [Contribution](#contribution)
Expand Down Expand Up @@ -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>(
Expand All @@ -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(),
Expand Down Expand Up @@ -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:
Expand Down
19 changes: 16 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ boilerplate for you.
* [Guide](#guide)
* [Example](#example)
* [Attributes](#attributes)
* [Macro](#macro)
* [Features](#features)
* [License](#license)
* [Contribution](#contribution)
Expand Down Expand Up @@ -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>(
Expand All @@ -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(),
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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>;
Expand Down
5 changes: 5 additions & 0 deletions src/transition.rs
Original file line number Diff line number Diff line change
@@ -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())));
}
9 changes: 8 additions & 1 deletion tests/into_after.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand All @@ -25,4 +28,8 @@ impl PollMachine for Machine {
fn poll_start<'a>(_: &'a mut RentToOwn<'a, Start>) -> Poll<AfterStart, usize> {
Ok(Async::Ready(Ready(1).into()))
}

fn poll_transition_macro<'a>(_: &'a mut RentToOwn<'a, TransitionMacro>) -> Poll<AfterTransitionMacro, usize> {
transition!(Ready(2))
}
}