1 use std::error::Error;
2 use 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)]
14 pub struct SpawnError {
15     is_shutdown: bool,
16 }
17 
18 impl SpawnError {
19     /// Return a new `SpawnError` reflecting a shutdown executor failure.
shutdown() -> Self20     pub fn shutdown() -> Self {
21         SpawnError { is_shutdown: true }
22     }
23 
24     /// Return a new `SpawnError` reflecting an executor at capacity failure.
at_capacity() -> Self25     pub fn at_capacity() -> Self {
26         SpawnError { is_shutdown: false }
27     }
28 
29     /// Returns `true` if the error reflects a shutdown executor failure.
is_shutdown(&self) -> bool30     pub fn is_shutdown(&self) -> bool {
31         self.is_shutdown
32     }
33 
34     /// Returns `true` if the error reflects an executor at capacity failure.
is_at_capacity(&self) -> bool35     pub fn is_at_capacity(&self) -> bool {
36         !self.is_shutdown
37     }
38 }
39 
40 impl fmt::Display for SpawnError {
fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result41     fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
42         write!(fmt, "{}", self.description())
43     }
44 }
45 
46 impl Error for SpawnError {
description(&self) -> &str47     fn description(&self) -> &str {
48         "attempted to spawn task while the executor is at capacity or shut down"
49     }
50 }
51