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
//! Uniform state machine abstraction
//!
//! By "uniform" we mean a state machine which does the same action on any
//! event handler. I.e. it can check some global state on every wakeup,
//! timeout, or other event and react the same way.

use rotor::{Scope, Machine, Response, EventSet, Void};

pub struct Uniform<T: Action>(pub T);

pub trait Action: Sized {
    type Context;
    type Seed;
    fn create(seed: Self::Seed, scope: &mut Scope<Self::Context>)
        -> Response<Self, Void>;
    fn action(self, scope: &mut Scope<Self::Context>)
        -> Response<Self, Self::Seed>;
}

impl<T: Action> Machine for Uniform<T> {
    type Context = T::Context;
    type Seed = T::Seed;
    fn create(seed: Self::Seed, scope: &mut Scope<Self::Context>)
        -> Response<Self, Void>
    {
        Action::create(seed, scope).wrap(Uniform)
    }
    fn ready(self, _events: EventSet, scope: &mut Scope<Self::Context>)
        -> Response<Self, Self::Seed>
    {
        self.0.action(scope).wrap(Uniform)
    }
    fn spawned(self, scope: &mut Scope<Self::Context>)
        -> Response<Self, Self::Seed>
    {
        self.0.action(scope).wrap(Uniform)
    }
    fn timeout(self, scope: &mut Scope<Self::Context>)
        -> Response<Self, Self::Seed>
    {
        self.0.action(scope).wrap(Uniform)
    }
    fn wakeup(self, scope: &mut Scope<Self::Context>)
        -> Response<Self, Self::Seed>
    {
        self.0.action(scope).wrap(Uniform)
    }
}