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.
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.
11 #![unstable(feature = "futures_api",
12 reason = "futures in libcore are unstable",
16 use super::{Executor, Waker, LocalWaker};
18 /// Information about the currently-running task.
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,
27 impl<'a> fmt::Debug for Context<'a> {
28 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
29 f.debug_struct("Context")
34 impl<'a> Context<'a> {
35 /// Create a new task `Context` with the provided `local_waker`, `waker`, and `executor`.
37 pub fn new(local_waker: &'a LocalWaker, executor: &'a mut dyn Executor) -> Context<'a> {
44 /// Get the `LocalWaker` associated with the current task.
46 pub fn local_waker(&self) -> &'a LocalWaker {
50 /// Get the `Waker` associated with the current task.
52 pub fn waker(&self) -> &'a Waker {
53 unsafe { &*(self.local_waker as *const LocalWaker as *const Waker) }
56 /// Get the default executor associated with this task.
58 /// This method is useful primarily if you want to explicitly handle
61 pub fn executor(&mut self) -> &mut dyn Executor {
65 /// Produce a context like the current one, but using the given waker instead.
67 /// This advanced method is primarily used when building "internal
68 /// schedulers" within a task, where you want to provide some customized
71 pub fn with_waker<'b>(&'b mut self, local_waker: &'b LocalWaker) -> Context<'b> {
74 executor: self.executor,
78 /// Produce a context like the current one, but using the given executor
81 /// This advanced method is primarily used when building "internal
82 /// schedulers" within a task.
84 pub fn with_executor<'b, E>(&'b mut self, executor: &'b mut E) -> Context<'b>
88 local_waker: self.local_waker,