]> git.lizzy.rs Git - rust.git/blob - src/libcore/task/spawn.rs
Auto merge of #54174 - parched:park, r=alexcrichton
[rust.git] / src / libcore / task / spawn.rs
1 // Copyright 2018 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 #![unstable(feature = "futures_api",
12             reason = "futures in libcore are unstable",
13             issue = "50547")]
14
15 use fmt;
16 use future::{FutureObj, LocalFutureObj};
17
18 /// Spawns tasks that poll futures to completion onto its associated task
19 /// executor.
20 ///
21 /// The term "task" refers to a kind of lightweight "thread". Task executors
22 /// are responsible for scheduling the execution of tasks on operating system
23 /// threads.
24 pub trait Spawn {
25     /// Spawns a new task with the given future. The future will be polled until
26     /// completion.
27     ///
28     /// # Errors
29     ///
30     /// The executor may be unable to spawn tasks, either because it has
31     /// been shut down or is resource-constrained.
32     fn spawn_obj(
33         &mut self,
34         future: FutureObj<'static, ()>,
35     ) -> Result<(), SpawnObjError>;
36
37     /// Determines whether the executor is able to spawn new tasks.
38     ///
39     /// # Returns
40     ///
41     /// An `Ok` return means the executor is *likely* (but not guaranteed)
42     /// to accept a subsequent spawn attempt. Likewise, an `Err` return
43     /// means that `spawn` is likely, but not guaranteed, to yield an error.
44     #[inline]
45     fn status(&self) -> Result<(), SpawnErrorKind> {
46         Ok(())
47     }
48 }
49
50 /// Provides the reason that an executor was unable to spawn.
51 pub struct SpawnErrorKind {
52     _hidden: (),
53 }
54
55 impl fmt::Debug for SpawnErrorKind {
56     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
57         f.debug_tuple("SpawnErrorKind")
58             .field(&"shutdown")
59             .finish()
60     }
61 }
62
63 impl SpawnErrorKind {
64     /// Spawning is failing because the executor has been shut down.
65     pub fn shutdown() -> SpawnErrorKind {
66         SpawnErrorKind { _hidden: () }
67     }
68
69     /// Check whether this error is the `shutdown` error.
70     pub fn is_shutdown(&self) -> bool {
71         true
72     }
73 }
74
75 /// The result of a failed spawn
76 #[derive(Debug)]
77 pub struct SpawnObjError {
78     /// The kind of error
79     pub kind: SpawnErrorKind,
80
81     /// The future for which spawning inside a task was attempted
82     pub future: FutureObj<'static, ()>,
83 }
84
85 /// The result of a failed spawn
86 #[derive(Debug)]
87 pub struct SpawnLocalObjError {
88     /// The kind of error
89     pub kind: SpawnErrorKind,
90
91     /// The future for which spawning inside a task was attempted
92     pub future: LocalFutureObj<'static, ()>,
93 }