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
use std::fmt;
use std::error::Error;


/// Error when spawning a new state machine
pub enum SpawnError<S: Sized> {
    /// The State Machine Slab capacity is reached
    ///
    /// The capacity is configured in the `rotor::Config` and is used
    /// for creating `rotor::Loop`.
    ///
    /// The item in this struct is the Seed that send to create a machine
    NoSlabSpace(S),
    /// Error returned from `Machine::create` handler
    UserError(Box<Error>),
}

impl<S> fmt::Display for SpawnError<S> {
    fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
        use self::SpawnError::*;
        match *self {
            NoSlabSpace(_) => {
                write!(fmt, "state machine slab capacity limit is reached")
            }
            UserError(ref err) => {
                write!(fmt, "{}", err)
            }
        }
    }
}

impl<S> SpawnError<S> {
    pub fn description(&self) -> &str {
        use self::SpawnError::*;
        match self {
            &NoSlabSpace(_) => "state machine slab capacity limit is reached",
            &UserError(ref err) => err.description(),
        }
    }
    pub fn cause(&self) -> Option<&Error> {
        use self::SpawnError::*;
        match self {
            &NoSlabSpace(_) => None,
            &UserError(ref err) => Some(&**err),
        }
    }
    pub fn map<T:Sized, F: FnOnce(S) -> T>(self, fun:F) -> SpawnError<T> {
        use self::SpawnError::*;
        match self {
            NoSlabSpace(x) => NoSlabSpace(fun(x)),
            UserError(e) => UserError(e),
        }
    }
}
impl<S: Error> Error for SpawnError<S> {
    fn description(&self) -> &str {
        self.description()
    }
    fn cause(&self) -> Option<&Error> {
        self.cause()
    }
}

impl<S> From<Box<Error>> for SpawnError<S> {
    fn from(x: Box<Error>) -> SpawnError<S> {
        SpawnError::UserError(x)
    }
}

impl<S> fmt::Debug for SpawnError<S> {
    fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
        use self::SpawnError::*;
        match *self {
            NoSlabSpace(..) => {
                write!(fmt, "NoSlabSpace(<hidden seed>)")
            }
            UserError(ref err) => {
                write!(fmt, "UserError({:?})", err)
            }
        }
    }
}