futures_core/
executor.rs

1//! Executors.
2
3if_std! {
4    use std::boxed::Box;
5    use Future;
6    use never::Never;
7
8    /// A task executor.
9    ///
10    /// A *task* is a `()`-producing future that runs at the top level, and will
11    /// be `poll`ed until completion. It's also the unit at which wake-up
12    /// notifications occur. Executors, such as thread pools, allow tasks to be
13    /// spawned and are responsible for putting tasks onto ready queues when
14    /// they are woken up, and polling them when they are ready.
15    pub trait Executor {
16        /// Spawn the given task, polling it until completion.
17        ///
18        /// Tasks must be infallible, as the type suggests; it is the
19        /// client's reponsibility to route any errors elsewhere via a channel
20        /// or some other means of communication.
21        ///
22        /// # Errors
23        ///
24        /// The executor may be unable to spawn tasks, either because it has
25        /// been shut down or is resource-constrained.
26        fn spawn(&mut self, f: Box<Future<Item = (), Error = Never> + Send>) -> Result<(), SpawnError>;
27
28        /// Determine whether the executor is able to spawn new tasks.
29        ///
30        /// # Returns
31        ///
32        /// An `Ok` return means the executor is *likely* (but not guaranteed)
33        /// to accept a subsequent spawn attempt. Likewise, an `Err` return
34        /// means that `spawn` is likely, but not guaranteed, to yield an error.
35        fn status(&self) -> Result<(), SpawnError> {
36            Ok(())
37        }
38
39        // TODO: downcasting hooks
40    }
41
42    /// Provides the reason that an executor was unable to spawn.
43    #[derive(Debug)]
44    pub struct SpawnError {
45        _a: ()
46    }
47
48    impl SpawnError {
49        /// Spawning is failing because the executor has been shut down.
50        pub fn shutdown() -> SpawnError {
51            SpawnError { _a: () }
52        }
53
54        /// Check whether this error is the `shutdown` error.
55        pub fn is_shutdown() -> bool {
56            true
57        }
58    }
59}
60
61#[cfg(not(feature = "std"))]
62pub(crate) trait Executor {}