tokio_executor/error.rs
1use std::error::Error;
2use std::fmt;
3
4/// Errors returned by `Executor::spawn`.
5///
6/// Spawn errors should represent relatively rare scenarios. Currently, the two
7/// scenarios represented by `SpawnError` are:
8///
9/// * An executor being at capacity or full. As such, the executor is not able
10/// to accept a new future. This error state is expected to be transient.
11/// * An executor has been shutdown and can no longer accept new futures. This
12/// error state is expected to be permanent.
13#[derive(Debug)]
14pub struct SpawnError {
15 is_shutdown: bool,
16}
17
18impl SpawnError {
19 /// Return a new `SpawnError` reflecting a shutdown executor failure.
20 pub fn shutdown() -> Self {
21 SpawnError { is_shutdown: true }
22 }
23
24 /// Return a new `SpawnError` reflecting an executor at capacity failure.
25 pub fn at_capacity() -> Self {
26 SpawnError { is_shutdown: false }
27 }
28
29 /// Returns `true` if the error reflects a shutdown executor failure.
30 pub fn is_shutdown(&self) -> bool {
31 self.is_shutdown
32 }
33
34 /// Returns `true` if the error reflects an executor at capacity failure.
35 pub fn is_at_capacity(&self) -> bool {
36 !self.is_shutdown
37 }
38}
39
40impl fmt::Display for SpawnError {
41 fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
42 write!(fmt, "{}", self.description())
43 }
44}
45
46impl Error for SpawnError {
47 fn description(&self) -> &str {
48 "attempted to spawn task while the executor is at capacity or shut down"
49 }
50}