Runs a closure or future sometime before the scope completes. This is like [`Worker::spawn`], allows the work to borrow local data. Vald inputs to this method are: A `for<'worker> FnOnce(&'worker Worker)` closure, with no return type. A `Future ` future, with no return type.
(&'scope self, work: S)
| 159 | /// |
| 160 | /// * A `Future<Output = ()>` future, with no return type. |
| 161 | pub fn spawn<M, S>(&'scope self, work: S) |
| 162 | where |
| 163 | S: SpawnScoped<'scope, M> + Send, |
| 164 | { |
| 165 | let thread_pool = self.thread_pool; |
| 166 | let scheduler = |job_ref, worker: Option<&Worker>| match worker { |
| 167 | Some(worker) => worker.fifo_queue.push_new(job_ref), |
| 168 | None => thread_pool.push_shared_job(job_ref), |
| 169 | }; |
| 170 | // SAFETY: `spawn_scoped` requires that `!Send` work only be scheduled |
| 171 | // to run on the current thread. Here `S: Send`, so that obligation is |
| 172 | // vacuous. |
| 173 | unsafe { work.spawn_scoped(self, scheduler) }; |
| 174 | } |
| 175 | |
| 176 | /// Like [`spawn`](Self::spawn), but routes the work to a specific pool |
| 177 | /// member so it runs on that member's thread. |