Trait rotor_tools::loop_ext::LoopInstanceExt [] [src]

pub trait LoopInstanceExt<M: Machine> {
    fn add_and_fetch<F, W, T, E, N>(&mut self, fsm_wrapper: W, fun: F) -> Result<T, SpawnError<()>> where E: Error + 'static, W: FnOnce(N) -> M, F: FnOnce(&mut Scope<M::Context>) -> Result<(N, T), E>;
}

Convenience enhancements to the main loop creator instance

Required Methods

fn add_and_fetch<F, W, T, E, N>(&mut self, fsm_wrapper: W, fun: F) -> Result<T, SpawnError<()>> where E: Error + 'static, W: FnOnce(N) -> M, F: FnOnce(&mut Scope<M::Context>) -> Result<(N, T), E>

This method is useful for things that have a state machine and an accessor to it.

Function looks a little bit complicated with generic params. But it solves very useful pattern in easier way.

Examples:

  • rotor_dns::create_resolve()
  • rotor_carbon::connect_ip()

Usage is simple (carbon example): ignore let sink = loop_inst.add_and_fetch(Fsm::Carbon, |scope| { connect_ip(addr, scope) }); Compare it to traditional way: ignore let mut sink_opt = None; loop_creator.add_machine_with(|scope| { let (fsm, sink) = connect_ip(addr, scope).unwrap(); sink_opt = Some(sink); Ok(Fsm::Carbon(fsm)) }).unwrap(); let sink = sink_opt.unwrap();

Implementors