From 9e7739326a9d1cb446dd3f928223e4b2b0edaede Mon Sep 17 00:00:00 2001 From: Mike Hsu Date: Sat, 19 Nov 2022 13:47:27 -0800 Subject: [PATCH] don't move spawned array --- crates/bevy_tasks/src/task_pool.rs | 24 ++++-------------------- 1 file changed, 4 insertions(+), 20 deletions(-) diff --git a/crates/bevy_tasks/src/task_pool.rs b/crates/bevy_tasks/src/task_pool.rs index ad376aa903916..099f96e93d006 100644 --- a/crates/bevy_tasks/src/task_pool.rs +++ b/crates/bevy_tasks/src/task_pool.rs @@ -2,7 +2,6 @@ use std::{ future::Future, marker::PhantomData, mem, - pin::Pin, sync::Arc, thread::{self, JoinHandle}, }; @@ -268,9 +267,9 @@ impl TaskPool { if spawned.is_empty() { Vec::new() } else { - let get_results = async move { - let mut results = Vec::with_capacity(spawned.len()); - while let Ok(task) = spawned.pop() { + let get_results = async { + let mut results = Vec::with_capacity(spawned_ref.len()); + while let Ok(task) = spawned_ref.pop() { results.push(task.await.unwrap()); } @@ -280,23 +279,8 @@ impl TaskPool { // Pin the futures on the stack. pin!(get_results); - // SAFETY: This function blocks until all futures complete, so we do not read/write - // the data from futures outside of the 'scope lifetime. However, - // rust has no way of knowing this so we must convert to 'static - // here to appease the compiler as it is unable to validate safety. - let get_results: Pin<&mut (dyn Future> + 'static + Send)> = get_results; - let get_results: Pin<&'static mut (dyn Future> + 'static + Send)> = - unsafe { mem::transmute(get_results) }; - - // The thread that calls scope() will participate in driving tasks in the pool - // forward until the tasks that are spawned by this scope() call - // complete. (If the caller of scope() happens to be a thread in - // this thread pool, and we only have one thread in the pool, then - // simply calling future::block_on(spawned) would deadlock.) - let mut spawned = task_scope_executor.spawn(get_results); - loop { - if let Some(result) = future::block_on(future::poll_once(&mut spawned)) { + if let Some(result) = future::block_on(future::poll_once(&mut get_results)) { break result; };