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

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>;
}

Convenience enhancements to the main loop creator instance

Required Methods

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>

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):

let sink = loop_inst.add_and_fetch(Fsm::Carbon, |scope| {
    connect_ip(addr, scope)
});

Compare it to traditional way:

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