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::{Spawn, 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 spawner: &'a mut dyn Spawn,
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`,
39 local_waker: &'a LocalWaker,
40 spawner: &'a mut dyn Spawn,
42 Context { local_waker, spawner }
45 /// Get the `LocalWaker` associated with the current task.
47 pub fn local_waker(&self) -> &'a LocalWaker {
51 /// Get the `Waker` associated with the current task.
53 pub fn waker(&self) -> &'a Waker {
54 unsafe { &*(self.local_waker as *const LocalWaker as *const Waker) }
57 /// Get the spawner associated with this task.
59 /// This method is useful primarily if you want to explicitly handle
62 pub fn spawner(&mut self) -> &mut dyn Spawn {
66 /// Produce a context like the current one, but using the given waker
69 /// This advanced method is primarily used when building "internal
70 /// schedulers" within a task, where you want to provide some customized
73 pub fn with_waker<'b>(
75 local_waker: &'b LocalWaker,
79 spawner: self.spawner,
83 /// Produce a context like the current one, but using the given spawner
86 /// This advanced method is primarily used when building "internal
87 /// schedulers" within a task.
89 pub fn with_spawner<'b, Sp: Spawn>(
94 local_waker: self.local_waker,