1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
//! The traits which make main loop construction nicer
use rotor::{Machine, Scope, EarlyScope, Loop, LoopInstance, SpawnError};
use rotor::{Response, Void};


/// Convenience enhancements to the main loop creator
pub trait LoopExt<M> {
    /// 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();
    /// ```
    ///
    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>;
}

/// Convenience enhancements to the main loop creator instance
pub trait LoopInstanceExt<M: Machine> {
    /// 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();
    /// ```
    ///
    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())
    }
}