rotor/
error.rs

1use std::fmt;
2use std::error::Error;
3
4
5/// Error when spawning a new state machine
6pub enum SpawnError<S: Sized> {
7    /// The State Machine Slab capacity is reached
8    ///
9    /// The capacity is configured in the `rotor::Config` and is used
10    /// for creating `rotor::Loop`.
11    ///
12    /// The item in this struct is the Seed that send to create a machine
13    NoSlabSpace(S),
14    /// Error returned from `Machine::create` handler
15    UserError(Box<Error>),
16}
17
18impl<S> fmt::Display for SpawnError<S> {
19    fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
20        use self::SpawnError::*;
21        match *self {
22            NoSlabSpace(_) => {
23                write!(fmt, "state machine slab capacity limit is reached")
24            }
25            UserError(ref err) => {
26                write!(fmt, "{}", err)
27            }
28        }
29    }
30}
31
32impl<S> SpawnError<S> {
33    pub fn description(&self) -> &str {
34        use self::SpawnError::*;
35        match self {
36            &NoSlabSpace(_) => "state machine slab capacity limit is reached",
37            &UserError(ref err) => err.description(),
38        }
39    }
40    pub fn cause(&self) -> Option<&Error> {
41        use self::SpawnError::*;
42        match self {
43            &NoSlabSpace(_) => None,
44            &UserError(ref err) => Some(&**err),
45        }
46    }
47    pub fn map<T:Sized, F: FnOnce(S) -> T>(self, fun:F) -> SpawnError<T> {
48        use self::SpawnError::*;
49        match self {
50            NoSlabSpace(x) => NoSlabSpace(fun(x)),
51            UserError(e) => UserError(e),
52        }
53    }
54}
55impl<S: Error> Error for SpawnError<S> {
56    fn description(&self) -> &str {
57        self.description()
58    }
59    fn cause(&self) -> Option<&Error> {
60        self.cause()
61    }
62}
63
64impl<S> From<Box<Error>> for SpawnError<S> {
65    fn from(x: Box<Error>) -> SpawnError<S> {
66        SpawnError::UserError(x)
67    }
68}
69
70impl<S> fmt::Debug for SpawnError<S> {
71    fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
72        use self::SpawnError::*;
73        match *self {
74            NoSlabSpace(..) => {
75                write!(fmt, "NoSlabSpace(<hidden seed>)")
76            }
77            UserError(ref err) => {
78                write!(fmt, "UserError({:?})", err)
79            }
80        }
81    }
82}