use rotor::{Machine, Scope, EarlyScope, Loop, LoopInstance, SpawnError};
use rotor::{Response, Void};
pub trait LoopExt<M> {
fn add_and_fetch<F, W, T, N>(&mut self, fsm_wrapper: W, fun: F)
-> Result<T, SpawnError<()>>
where W: FnOnce(N) -> M,
F: FnOnce(&mut EarlyScope) -> Response<(N, T), Void>;
}
pub trait LoopInstanceExt<M: Machine> {
fn add_and_fetch<F, W, T, N>(&mut self, fsm_wrapper: W, fun: F)
-> Result<T, SpawnError<()>>
where W: FnOnce(N) -> M,
F: FnOnce(&mut Scope<M::Context>) -> Response<(N, T), Void>;
}
impl<M: Machine> LoopExt<M> for Loop<M> {
fn add_and_fetch<F, W, T, N>(&mut self, fsm_wrapper: W, fun: F)
-> Result<T, SpawnError<()>>
where W: FnOnce(N) -> M,
F: FnOnce(&mut EarlyScope) -> Response<(N, T), Void>
{
let mut result_opt = None;
try!(self.add_machine_with(|scope| {
fun(scope).wrap(|(fsm, value)| {
result_opt = Some(value);
fsm_wrapper(fsm)
})
}));
Ok(result_opt.unwrap())
}
}
impl<M: Machine> LoopInstanceExt<M> for LoopInstance<M> {
fn add_and_fetch<F, W, T, N>(&mut self, fsm_wrapper: W, fun: F)
-> Result<T, SpawnError<()>>
where W: FnOnce(N) -> M,
F: FnOnce(&mut Scope<M::Context>) -> Response<(N, T), Void>
{
let mut result_opt = None;
try!(self.add_machine_with(|scope| {
fun(scope).wrap(|(fsm, value)| {
result_opt = Some(value);
fsm_wrapper(fsm)
})
}));
Ok(result_opt.unwrap())
}
}