]> git.lizzy.rs Git - rust.git/blob - src/libcore/task/context.rs
Rollup merge of #53110 - Xanewok:save-analysis-remap-path, r=nrc
[rust.git] / src / libcore / task / context.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 super::{Spawn, Waker, LocalWaker};
17
18 /// Information about the currently-running task.
19 ///
20 /// Contexts are always tied to the stack, since they are set up specifically
21 /// when performing a single `poll` step on a task.
22 pub struct Context<'a> {
23     local_waker: &'a LocalWaker,
24     spawner: &'a mut dyn Spawn,
25 }
26
27 impl<'a> fmt::Debug for Context<'a> {
28     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
29         f.debug_struct("Context")
30             .finish()
31     }
32 }
33
34 impl<'a> Context<'a> {
35     /// Create a new task `Context` with the provided `local_waker`, `waker`,
36     /// and `spawner`.
37     #[inline]
38     pub fn new(
39         local_waker: &'a LocalWaker,
40         spawner: &'a mut dyn Spawn,
41     ) -> Context<'a> {
42         Context { local_waker, spawner }
43     }
44
45     /// Get the `LocalWaker` associated with the current task.
46     #[inline]
47     pub fn local_waker(&self) -> &'a LocalWaker {
48         self.local_waker
49     }
50
51     /// Get the `Waker` associated with the current task.
52     #[inline]
53     pub fn waker(&self) -> &'a Waker {
54         unsafe { &*(self.local_waker as *const LocalWaker as *const Waker) }
55     }
56
57     /// Get the spawner associated with this task.
58     ///
59     /// This method is useful primarily if you want to explicitly handle
60     /// spawn failures.
61     #[inline]
62     pub fn spawner(&mut self) -> &mut dyn Spawn {
63         self.spawner
64     }
65
66     /// Produce a context like the current one, but using the given waker
67     /// instead.
68     ///
69     /// This advanced method is primarily used when building "internal
70     /// schedulers" within a task, where you want to provide some customized
71     /// wakeup logic.
72     #[inline]
73     pub fn with_waker<'b>(
74         &'b mut self,
75         local_waker: &'b LocalWaker,
76     ) -> Context<'b> {
77         Context {
78             local_waker,
79             spawner: self.spawner,
80         }
81     }
82
83     /// Produce a context like the current one, but using the given spawner
84     /// instead.
85     ///
86     /// This advanced method is primarily used when building "internal
87     /// schedulers" within a task.
88     #[inline]
89     pub fn with_spawner<'b, Sp: Spawn>(
90         &'b mut self,
91         spawner: &'b mut Sp,
92     ) -> Context<'b> {
93         Context {
94             local_waker: self.local_waker,
95             spawner,
96         }
97     }
98 }