Skip to content

Instantly share code, notes, and snippets.

@erickt
Created September 11, 2016 00:58
Show Gist options
  • Save erickt/3fe90ba2c889bdaeaab7200614c32fde to your computer and use it in GitHub Desktop.
Save erickt/3fe90ba2c889bdaeaab7200614c32fde to your computer and use it in GitHub Desktop.
fn long_running_future(timer: Timer,
times: u64,
a: u64)
-> Box<Future<Item = u64, Error = TimerError> + Send> {
struct StateMachine<S, F> {
state: S,
next: F,
}
impl<S, F, Item, Error> StateMachine<S, F>
where S: ::std::default::Default,
F: Fn(S) -> ::std::result::Result<(::futures::Async<Item>, S), Error>
{
fn new(initial_state: S, next: F) -> Self {
StateMachine {
state: initial_state,
next: next,
}
}
}
impl<S, F, Item, Error> ::futures::Future for StateMachine<S, F>
where S: ::std::default::Default,
F: Fn(S) -> ::std::result::Result<(::futures::Async<Item>, S), Error>
{
type Item = Item;
type Error = Error;
fn poll(&mut self) -> ::futures::Poll<Item, Error> {
let state = ::std::mem::replace(&mut self.state, S::default());
let (value, state) = try!((self.next)(state));
self.state = state;
Ok(value)
}
}
enum State<T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T11, T12, T14> {
Illegal,
State0Start { timer: T0, times: T1, a: T2 },
State1End { return_: T3 },
State2AwaitLoop {
timer: T0,
times: T1,
a: T2,
future: T4,
},
State3AwaitExit {
timer: T0,
times: T1,
a: T2,
future: T4,
},
State4AwaitReady {
timer: T0,
times: T1,
a: T2,
future: T4,
result: T5,
},
State5EndScope {
timer: T0,
times: T1,
a: T2,
future: T4,
},
State6AwaitNotReady {
timer: T0,
times: T1,
a: T2,
future: T4,
},
State7Loop {
timer: T0,
times: T1,
a: T2,
future: T4,
__stateful_iter: T6,
},
State8LoopExit {
timer: T0,
times: T1,
a: T2,
future: T4,
__stateful_iter: T6,
},
State9Arm {
timer: T0,
times: T1,
a: T2,
future: T4,
__stateful_iter: T6,
i: T7,
},
State10Arm {
timer: T0,
times: T1,
a: T2,
future: T4,
__stateful_iter: T6,
},
State11MatchJoin {
timer: T0,
times: T1,
a: T2,
future: T4,
__stateful_iter: T6,
},
State12Then {
timer: T0,
times: T1,
a: T2,
future: T4,
__stateful_iter: T6,
i: T7,
},
State13Else {
timer: T0,
times: T1,
a: T2,
future: T4,
__stateful_iter: T6,
i: T7,
},
State14AwaitLoop {
timer: T0,
times: T1,
a: T2,
future_shadowed_4: T4,
__stateful_iter: T6,
i: T7,
sleep_time: T8,
future: T9,
},
State15AwaitExit {
timer: T0,
times: T1,
a: T2,
future_shadowed_4: T4,
__stateful_iter: T6,
i: T7,
sleep_time: T8,
future: T9,
},
State16AwaitReady {
timer: T0,
times: T1,
a: T2,
future_shadowed_4: T4,
__stateful_iter: T6,
i: T7,
sleep_time: T8,
future: T9,
result: T11,
},
State17EndScope {
timer: T0,
times: T1,
a: T2,
future_shadowed_4: T4,
__stateful_iter: T6,
i: T7,
sleep_time: T8,
future: T9,
},
State18AwaitNotReady {
timer: T0,
times: T1,
a: T2,
future_shadowed_4: T4,
__stateful_iter: T6,
i: T7,
sleep_time: T8,
future: T9,
},
State19EndScope {
timer: T0,
times: T1,
a: T2,
future: T4,
__stateful_iter: T6,
i: T7,
},
State20AwaitLoop {
timer: T0,
times: T1,
a: T2,
future_shadowed_4: T4,
__stateful_iter: T6,
i: T7,
future: T12,
},
State21AwaitExit {
timer: T0,
times: T1,
a: T2,
future_shadowed_4: T4,
__stateful_iter: T6,
i: T7,
future: T12,
},
State22AwaitReady {
timer: T0,
times: T1,
a: T2,
future_shadowed_4: T4,
__stateful_iter: T6,
i: T7,
future: T12,
result: T14,
},
State23EndScope {
timer: T0,
times: T1,
a: T2,
future_shadowed_4: T4,
__stateful_iter: T6,
i: T7,
future: T12,
},
State24AwaitNotReady {
timer: T0,
times: T1,
a: T2,
future_shadowed_4: T4,
__stateful_iter: T6,
i: T7,
future: T12,
},
State25EndScope {
timer: T0,
times: T1,
a: T2,
future: T4,
__stateful_iter: T6,
i: T7,
},
State26IfJoin {
timer: T0,
times: T1,
a: T2,
future: T4,
__stateful_iter: T6,
i: T7,
},
State27EndScope {
timer: T0,
times: T1,
a: T2,
future: T4,
__stateful_iter: T6,
i: T7,
},
State28EndScope {
timer: T0,
times: T1,
a: T2,
future: T4,
__stateful_iter: T6,
},
State29EndScope {
timer: T0,
times: T1,
a: T2,
future: T4,
__stateful_iter: T6,
},
State30EndScope {
timer: T0,
times: T1,
a: T2,
future: T4,
},
State31EndScope {
timer: T0,
times: T1,
a: T2,
return_: T3,
},
}
impl <T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T11, T12, T14>
::std::default::Default for
State<T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T11, T12, T14> {
fn default() -> Self { State::Illegal }
}
::std::boxed::Box::new(StateMachine::new(State::State0Start {
timer: timer,
times: times,
a: a,
},
|mut state| {
loop {
match state {
State::State0Start { timer, times, a } => {
let future;
future = short_running_future(timer.clone());
state = State::State2AwaitLoop {
timer: timer,
times: times,
a: a,
future: future,
};
continue;
}
State::State1End { return_ } => {
return ::std::result::Result::Ok((::futures::Async::Ready(return_),
State::Illegal));
}
State::State2AwaitLoop { timer, times, a, mut future } => {
match match ::futures::Future::poll(&mut future) {
::std::result::Result::Ok(value) => value,
::std::result::Result::Err(value) => {
return ::std::result::Result::Err(value)
}
} {
::futures::Async::Ready(result) => {
state = State::State4AwaitReady {
timer: timer,
times: times,
a: a,
future: future,
result: result,
};
continue;
}
::futures::Async::NotReady => {
state = State::State6AwaitNotReady {
timer: timer,
times: times,
a: a,
future: future,
};
continue;
}
}
}
State::State3AwaitExit { timer, times, a, mut future } => {
let __stateful_iter;
__stateful_iter = ::std::iter::IntoIterator::into_iter(0..times);
state = State::State7Loop {
timer: timer,
times: times,
a: a,
future: future,
__stateful_iter: __stateful_iter,
};
continue;
}
State::State4AwaitReady { timer, times, a, mut future, result } => {
::std::mem::drop(result);
state = State::State5EndScope {
timer: timer,
times: times,
a: a,
future: future,
};
continue;
}
State::State5EndScope { timer, times, a, mut future } => {
state = State::State3AwaitExit {
timer: timer,
times: times,
a: a,
future: future,
};
continue;
}
State::State6AwaitNotReady { timer, times, a, mut future } => {
return ::std::result::Result::Ok((::futures::Async::NotReady,
State::State2AwaitLoop {
timer: timer,
times: times,
a: a,
future: future,
}));
}
State::State7Loop { timer, times, a, mut future, mut __stateful_iter } => {
match __stateful_iter.next() {
::std::option::Option::Some(i) => {
state = State::State9Arm {
timer: timer,
times: times,
a: a,
future: future,
__stateful_iter: __stateful_iter,
i: i,
};
continue;
}
::std::option::Option::None => {
state = State::State10Arm {
timer: timer,
times: times,
a: a,
future: future,
__stateful_iter: __stateful_iter,
};
continue;
}
}
}
State::State8LoopExit { timer, times, a, mut future, mut __stateful_iter } => {
let _stmt_result_temp = ();
::std::mem::drop(__stateful_iter);
state = State::State30EndScope {
timer: timer,
times: times,
a: a,
future: future,
};
continue;
}
State::State9Arm { timer, times, a, mut future, mut __stateful_iter, i } => {
if i % 2 == 0 {
state = State::State12Then {
timer: timer,
times: times,
a: a,
future: future,
__stateful_iter: __stateful_iter,
i: i,
};
continue;
} else {
state = State::State13Else {
timer: timer,
times: times,
a: a,
future: future,
__stateful_iter: __stateful_iter,
i: i,
};
continue;
}
}
State::State10Arm { timer, times, a, mut future, mut __stateful_iter } => {
state = State::State8LoopExit {
timer: timer,
times: times,
a: a,
future: future,
__stateful_iter: __stateful_iter,
};
continue;
}
State::State11MatchJoin { timer, times, a, mut future, mut __stateful_iter } => {
state = State::State29EndScope {
timer: timer,
times: times,
a: a,
future: future,
__stateful_iter: __stateful_iter,
};
continue;
}
State::State12Then { timer, times, a, mut future, mut __stateful_iter, i } => {
let sleep_time;
sleep_time = a * i;
print!("sleep {} {}", i, sleep_time);
let future_shadowed_4;
future_shadowed_4 = future;
let future;
future = timer.sleep(Duration::from_millis(sleep_time));
state = State::State14AwaitLoop {
timer: timer,
times: times,
a: a,
future_shadowed_4: future_shadowed_4,
__stateful_iter: __stateful_iter,
i: i,
sleep_time: sleep_time,
future: future,
};
continue;
}
State::State13Else { timer, times, a, mut future, mut __stateful_iter, i } => {
let future_shadowed_4;
future_shadowed_4 = future;
let future;
future = short_running_future(timer.clone());
state = State::State20AwaitLoop {
timer: timer,
times: times,
a: a,
future_shadowed_4: future_shadowed_4,
__stateful_iter: __stateful_iter,
i: i,
future: future,
};
continue;
}
State::State14AwaitLoop { timer,
times,
a,
mut future_shadowed_4,
mut __stateful_iter,
i,
sleep_time,
mut future } => {
match match ::futures::Future::poll(&mut future) {
::std::result::Result::Ok(value) => value,
::std::result::Result::Err(value) => {
return ::std::result::Result::Err(value)
}
} {
::futures::Async::Ready(result) => {
state = State::State16AwaitReady {
timer: timer,
times: times,
a: a,
future_shadowed_4: future_shadowed_4,
__stateful_iter: __stateful_iter,
i: i,
sleep_time: sleep_time,
future: future,
result: result,
};
continue;
}
::futures::Async::NotReady => {
state = State::State18AwaitNotReady {
timer: timer,
times: times,
a: a,
future_shadowed_4: future_shadowed_4,
__stateful_iter: __stateful_iter,
i: i,
sleep_time: sleep_time,
future: future,
};
continue;
}
}
}
State::State15AwaitExit { timer,
times,
a,
mut future_shadowed_4,
mut __stateful_iter,
i,
sleep_time,
mut future } => {
let _loop_result_temp = ();
::std::mem::drop(future);
let mut future = future_shadowed_4;
::std::mem::drop(sleep_time);
state = State::State19EndScope {
timer: timer,
times: times,
a: a,
future: future,
__stateful_iter: __stateful_iter,
i: i,
};
continue;
}
State::State16AwaitReady { timer,
times,
a,
mut future_shadowed_4,
mut __stateful_iter,
i,
sleep_time,
mut future,
result } => {
::std::mem::drop(result);
state = State::State17EndScope {
timer: timer,
times: times,
a: a,
future_shadowed_4: future_shadowed_4,
__stateful_iter: __stateful_iter,
i: i,
sleep_time: sleep_time,
future: future,
};
continue;
}
State::State17EndScope { timer,
times,
a,
mut future_shadowed_4,
mut __stateful_iter,
i,
sleep_time,
mut future } => {
state = State::State15AwaitExit {
timer: timer,
times: times,
a: a,
future_shadowed_4: future_shadowed_4,
__stateful_iter: __stateful_iter,
i: i,
sleep_time: sleep_time,
future: future,
};
continue;
}
State::State18AwaitNotReady { timer,
times,
a,
mut future_shadowed_4,
mut __stateful_iter,
i,
sleep_time,
mut future } => {
return ::std::result::Result::Ok((::futures::Async::NotReady,
State::State14AwaitLoop {
timer: timer,
times: times,
a: a,
future_shadowed_4: future_shadowed_4,
__stateful_iter: __stateful_iter,
i: i,
sleep_time: sleep_time,
future: future,
}));
}
State::State19EndScope { timer, times, a, mut future, mut __stateful_iter, i } => {
state = State::State26IfJoin {
timer: timer,
times: times,
a: a,
future: future,
__stateful_iter: __stateful_iter,
i: i,
};
continue;
}
State::State20AwaitLoop { timer,
times,
a,
mut future_shadowed_4,
mut __stateful_iter,
i,
mut future } => {
match match ::futures::Future::poll(&mut future) {
::std::result::Result::Ok(value) => value,
::std::result::Result::Err(value) => {
return ::std::result::Result::Err(value)
}
} {
::futures::Async::Ready(result) => {
state = State::State22AwaitReady {
timer: timer,
times: times,
a: a,
future_shadowed_4: future_shadowed_4,
__stateful_iter: __stateful_iter,
i: i,
future: future,
result: result,
};
continue;
}
::futures::Async::NotReady => {
state = State::State24AwaitNotReady {
timer: timer,
times: times,
a: a,
future_shadowed_4: future_shadowed_4,
__stateful_iter: __stateful_iter,
i: i,
future: future,
};
continue;
}
}
}
State::State21AwaitExit { timer,
times,
a,
mut future_shadowed_4,
mut __stateful_iter,
i,
mut future } => {
let _loop_result_temp = ();
::std::mem::drop(future);
let mut future = future_shadowed_4;
state = State::State25EndScope {
timer: timer,
times: times,
a: a,
future: future,
__stateful_iter: __stateful_iter,
i: i,
};
continue;
}
State::State22AwaitReady { timer,
times,
a,
mut future_shadowed_4,
mut __stateful_iter,
i,
mut future,
result } => {
::std::mem::drop(result);
state = State::State23EndScope {
timer: timer,
times: times,
a: a,
future_shadowed_4: future_shadowed_4,
__stateful_iter: __stateful_iter,
i: i,
future: future,
};
continue;
}
State::State23EndScope { timer,
times,
a,
mut future_shadowed_4,
mut __stateful_iter,
i,
mut future } => {
state = State::State21AwaitExit {
timer: timer,
times: times,
a: a,
future_shadowed_4: future_shadowed_4,
__stateful_iter: __stateful_iter,
i: i,
future: future,
};
continue;
}
State::State24AwaitNotReady { timer,
times,
a,
mut future_shadowed_4,
mut __stateful_iter,
i,
mut future } => {
return ::std::result::Result::Ok((::futures::Async::NotReady,
State::State20AwaitLoop {
timer: timer,
times: times,
a: a,
future_shadowed_4: future_shadowed_4,
__stateful_iter: __stateful_iter,
i: i,
future: future,
}));
}
State::State25EndScope { timer, times, a, mut future, mut __stateful_iter, i } => {
state = State::State26IfJoin {
timer: timer,
times: times,
a: a,
future: future,
__stateful_iter: __stateful_iter,
i: i,
};
continue;
}
State::State26IfJoin { timer, times, a, mut future, mut __stateful_iter, i } => {
state = State::State27EndScope {
timer: timer,
times: times,
a: a,
future: future,
__stateful_iter: __stateful_iter,
i: i,
};
continue;
}
State::State27EndScope { timer, times, a, mut future, mut __stateful_iter, i } => {
::std::mem::drop(i);
state = State::State28EndScope {
timer: timer,
times: times,
a: a,
future: future,
__stateful_iter: __stateful_iter,
};
continue;
}
State::State28EndScope { timer, times, a, mut future, mut __stateful_iter } => {
state = State::State11MatchJoin {
timer: timer,
times: times,
a: a,
future: future,
__stateful_iter: __stateful_iter,
};
continue;
}
State::State29EndScope { timer, times, a, mut future, mut __stateful_iter } => {
state = State::State7Loop {
timer: timer,
times: times,
a: a,
future: future,
__stateful_iter: __stateful_iter,
};
continue;
}
State::State30EndScope { timer, times, a, mut future } => {
println!("done sleeping");
let return_;
return_ = a;
::std::mem::drop(future);
state = State::State31EndScope {
timer: timer,
times: times,
a: a,
return_: return_,
};
continue;
}
State::State31EndScope { timer, times, a, return_ } => {
::std::mem::drop(return_);
::std::mem::drop(a);
::std::mem::drop(times);
::std::mem::drop(timer);
state = State::State1End { return_: return_ };
continue;
}
State::Illegal => unreachable!("illegal state"),
}
}
}))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment