]> git.lizzy.rs Git - rust.git/blob - src/libcore/task/context.rs
121f93b666bcecbe3dbe4e180e1a68e5fa33473b
[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::{Executor, 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     executor: &'a mut dyn Executor,
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`, and `executor`.
36     #[inline]
37     pub fn new(local_waker: &'a LocalWaker, executor: &'a mut dyn Executor) -> Context<'a> {
38         Context {
39             local_waker,
40             executor,
41         }
42     }
43
44     /// Get the `LocalWaker` associated with the current task.
45     #[inline]
46     pub fn local_waker(&self) -> &'a LocalWaker {
47         self.local_waker
48     }
49
50     /// Get the `Waker` associated with the current task.
51     #[inline]
52     pub fn waker(&self) -> &'a Waker {
53         unsafe { &*(self.local_waker as *const LocalWaker as *const Waker) }
54     }
55
56     /// Get the default executor associated with this task.
57     ///
58     /// This method is useful primarily if you want to explicitly handle
59     /// spawn failures.
60     #[inline]
61     pub fn executor(&mut self) -> &mut dyn Executor {
62         self.executor
63     }
64
65     /// Produce a context like the current one, but using the given waker instead.
66     ///
67     /// This advanced method is primarily used when building "internal
68     /// schedulers" within a task, where you want to provide some customized
69     /// wakeup logic.
70     #[inline]
71     pub fn with_waker<'b>(&'b mut self, local_waker: &'b LocalWaker) -> Context<'b> {
72         Context {
73             local_waker,
74             executor: self.executor,
75         }
76     }
77
78     /// Produce a context like the current one, but using the given executor
79     /// instead.
80     ///
81     /// This advanced method is primarily used when building "internal
82     /// schedulers" within a task.
83     #[inline]
84     pub fn with_executor<'b, E>(&'b mut self, executor: &'b mut E) -> Context<'b>
85         where E: Executor
86     {
87         Context {
88             local_waker: self.local_waker,
89             executor,
90         }
91     }
92 }