1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276
//! The task system. //! //! A [`Task`] handle represents a spawned future that is run by the executor. use std::fmt::Debug; use std::future::Future; use std::pin::Pin; use std::task::{Context, Poll}; use crate::blocking::BlockingExecutor; use crate::thread_local::ThreadLocalExecutor; use crate::work_stealing::WorkStealingExecutor; /// A runnable future, ready for execution. /// /// When a future is internally spawned using `async_task::spawn()` or `async_task::spawn_local()`, /// we get back two values: /// /// 1. an `async_task::Task<()>`, which we refer to as a `Runnable` /// 2. an `async_task::JoinHandle<T, ()>`, which is wrapped inside a `Task<T>` /// /// Once a `Runnable` is run, it "vanishes" and only reappears when its future is woken. When it's /// woken up, its schedule function is called, which means the `Runnable` gets pushed into a task /// queue in an executor. pub(crate) type Runnable = async_task::Task<()>; /// A spawned future. /// /// Tasks are also futures themselves and yield the output of the spawned future. /// /// When a task is dropped, its gets canceled and won't be polled again. To cancel a task a bit /// more gracefully and wait until it stops running, use the [`cancel()`][Task::cancel()] method. /// /// Tasks that panic get immediately canceled. Awaiting a canceled task also causes a panic. /// /// If the future panics, the panic will be unwound into the [`run()`] invocation that polled it. /// However, this does not apply to the blocking executor - it will simply ignore panics and /// continue running. /// /// # Examples /// /// ``` /// use smol::Task; /// /// # smol::run(async { /// // Spawn a task onto the work-stealing executor. /// let task = Task::spawn(async { /// println!("Hello from a task!"); /// 1 + 2 /// }); /// /// // Wait for the task to complete. /// assert_eq!(task.await, 3); /// # }); /// ``` /// /// [`run()`]: crate::run() #[must_use = "tasks get canceled when dropped, use `.detach()` to run them in the background"] #[derive(Debug)] pub struct Task<T>(pub(crate) Option<async_task::JoinHandle<T, ()>>); impl<T: 'static> Task<T> { /// Spawns a future onto the thread-local executor. /// /// Panics if the current thread is not inside an invocation of [`run()`]. /// /// # Examples /// /// ``` /// use smol::Task; /// /// # smol::run(async { /// let task = Task::local(async { 1 + 2 }); /// assert_eq!(task.await, 3); /// # }) /// ``` /// /// [`run()`]: crate::run() pub fn local(future: impl Future<Output = T> + 'static) -> Task<T> { ThreadLocalExecutor::spawn(future) } } impl<T: Send + 'static> Task<T> { /// Spawns a future onto the work-stealing executor. /// /// This future may be stolen and polled by any thread calling [`run()`]. /// /// # Examples /// /// ``` /// use smol::Task; /// /// # smol::run(async { /// let task = Task::spawn(async { 1 + 2 }); /// assert_eq!(task.await, 3); /// # }); /// ``` /// /// [`run()`]: crate::run() pub fn spawn(future: impl Future<Output = T> + Send + 'static) -> Task<T> { WorkStealingExecutor::get().spawn(future) } /// Spawns a future onto the blocking executor. /// /// This future is allowed to block for an indefinite length of time. /// /// For convenience, there is also the [`blocking!`] macro that spawns a blocking tasks and /// immediately awaits it. /// /// # Examples /// /// Read a line from the standard input: /// /// ```no_run /// use smol::Task; /// use std::io::stdin; /// /// # smol::block_on(async { /// let line = Task::blocking(async { /// let mut line = String::new(); /// std::io::stdin().read_line(&mut line).unwrap(); /// line /// }) /// .await; /// # }); /// ``` /// /// See also examples for [`blocking!`], [`iter()`], [`reader()`], and [`writer()`]. /// /// [`iter()`]: `crate::iter()` /// [`reader()`]: `crate::reader()` /// [`writer()`]: `crate::writer()` pub fn blocking(future: impl Future<Output = T> + Send + 'static) -> Task<T> { BlockingExecutor::get().spawn(future) } } impl<T, E> Task<Result<T, E>> where T: Send + 'static, E: Debug + Send + 'static, { /// Spawns a new task that awaits and unwraps the result. /// /// The new task will panic if the original task results in an error. /// /// # Examples /// /// ``` /// use smol::{Async, Task}; /// use std::net::TcpStream; /// /// # smol::run(async { /// let stream = Task::spawn(async { /// Async::<TcpStream>::connect("example.com:80").await /// }) /// .unwrap() /// .await; /// # }) /// ``` pub fn unwrap(self) -> Task<T> { Task::spawn(async { self.await.unwrap() }) } /// Spawns a new task that awaits and unwraps the result. /// /// The new task will panic with the provided message if the original task results in an error. /// /// # Examples /// /// ``` /// use smol::{Async, Task}; /// use std::net::TcpStream; /// /// # smol::run(async { /// let stream = Task::spawn(async { /// Async::<TcpStream>::connect("example.com:80").await /// }) /// .expect("cannot connect") /// .await; /// # }) /// ``` pub fn expect(self, msg: &str) -> Task<T> { let msg = msg.to_owned(); Task::spawn(async move { self.await.expect(&msg) }) } } impl Task<()> { /// Detaches the task to let it keep running in the background. /// /// # Examples /// /// ```no_run /// use smol::{Task, Timer}; /// use std::time::Duration; /// /// # smol::run(async { /// Task::spawn(async { /// loop { /// println!("I'm a daemon task looping forever."); /// Timer::after(Duration::from_secs(1)).await; /// } /// }) /// .detach(); /// # }) /// ``` pub fn detach(mut self) { self.0.take().unwrap(); } } impl<T> Task<T> { /// Cancels the task and waits for it to stop running. /// /// Returns the task's output if it was completed just before it got canceled, or `None` if it /// didn't complete. /// /// While it's possible to simply drop the [`Task`] to cancel it, this is a cleaner way of /// canceling because it also waits for the task to stop running. /// /// # Examples /// /// ``` /// use smol::{Task, Timer}; /// use std::time::Duration; /// /// # smol::run(async { /// let task = Task::spawn(async { /// loop { /// println!("Even though I'm in an infinite loop, you can still cancel me!"); /// Timer::after(Duration::from_secs(1)).await; /// } /// }); /// /// Timer::after(Duration::from_secs(3)).await; /// task.cancel().await; /// # }) /// ``` pub async fn cancel(self) -> Option<T> { // There's a bug in rustdoc causing it to render `mut self` as `__arg0: Self`, so we just // do `{ self }` here to avoid marking `self` as mutable. let handle = { self }.0.take().unwrap(); handle.cancel(); handle.await } } impl<T> Drop for Task<T> { fn drop(&mut self) { if let Some(handle) = &self.0 { handle.cancel(); } } } impl<T> Future for Task<T> { type Output = T; fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> { match Pin::new(&mut self.0.as_mut().unwrap()).poll(cx) { Poll::Pending => Poll::Pending, Poll::Ready(output) => Poll::Ready(output.expect("task has failed")), } } } impl<T> Into<async_task::JoinHandle<T, ()>> for Task<T> { fn into(mut self) -> async_task::JoinHandle<T, ()> { self.0 .take() .expect("task was already canceled or has failed") } }