tokio_timer/
error.rs

1use self::Kind::*;
2
3use std::error;
4use std::fmt;
5
6/// Errors encountered by the timer implementation.
7///
8/// Currently, there are two different errors that can occur:
9///
10/// * `shutdown` occurs when a timer operation is attempted, but the timer
11///   instance has been dropped. In this case, the operation will never be able
12///   to complete and the `shutdown` error is returned. This is a permanent
13///   error, i.e., once this error is observed, timer operations will never
14///   succeed in the future.
15///
16/// * `at_capacity` occurs when a timer operation is attempted, but the timer
17///   instance is currently handling its maximum number of outstanding delays.
18///   In this case, the operation is not able to be performed at the current
19///   moment, and `at_capacity` is returned. This is a transient error, i.e., at
20///   some point in the future, if the operation is attempted again, it might
21///   succeed. Callers that observe this error should attempt to [shed load]. One
22///   way to do this would be dropping the future that issued the timer operation.
23///
24/// [shed load]: https://en.wikipedia.org/wiki/Load_Shedding
25#[derive(Debug)]
26pub struct Error(Kind);
27
28#[derive(Debug)]
29enum Kind {
30    Shutdown,
31    AtCapacity,
32}
33
34impl Error {
35    /// Create an error representing a shutdown timer.
36    pub fn shutdown() -> Error {
37        Error(Shutdown)
38    }
39
40    /// Returns `true` if the error was caused by the timer being shutdown.
41    pub fn is_shutdown(&self) -> bool {
42        match self.0 {
43            Kind::Shutdown => true,
44            _ => false,
45        }
46    }
47
48    /// Create an error representing a timer at capacity.
49    pub fn at_capacity() -> Error {
50        Error(AtCapacity)
51    }
52
53    /// Returns `true` if the error was caused by the timer being at capacity.
54    pub fn is_at_capacity(&self) -> bool {
55        match self.0 {
56            Kind::AtCapacity => true,
57            _ => false,
58        }
59    }
60}
61
62impl error::Error for Error {
63    fn description(&self) -> &str {
64        use self::Kind::*;
65
66        match self.0 {
67            Shutdown => "timer is shutdown",
68            AtCapacity => "timer is at capacity and cannot create a new entry",
69        }
70    }
71}
72
73impl fmt::Display for Error {
74    fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
75        use std::error::Error;
76        self.description().fmt(fmt)
77    }
78}