Skip to main content

LoopInstanceExt

Trait LoopInstanceExt 

Source
pub trait LoopInstanceExt<M: Machine> {
    // Required method
    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>;
}
Expand description

Convenience enhancements to the main loop creator instance

Required Methods§

Source

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();

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementations on Foreign Types§

Source§

impl<M: Machine> LoopInstanceExt<M> for LoopInstance<M>

Source§

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>,

Implementors§