do not impl Component for Task (#4113)

# Objective

- `Task` are `Component`.
- They should not.

## Solution

- Remove the impl, and update the example to show a wrapper.

#4052 for reference
This commit is contained in:
François 2022-04-22 06:29:38 +00:00
parent 26c3b20f1c
commit 18c6a7b40e
2 changed files with 7 additions and 12 deletions

View File

@ -55,14 +55,6 @@ mod sealed {
impl Sealed for super::SparseStorage {} impl Sealed for super::SparseStorage {}
} }
// ECS dependencies cannot derive Component, so we must implement it manually for relevant structs.
impl<T> Component for bevy_tasks::Task<T>
where
Self: Send + Sync + 'static,
{
type Storage = TableStorage;
}
/// The storage used for a specific component type. /// The storage used for a specific component type.
/// ///
/// # Examples /// # Examples

View File

@ -43,6 +43,9 @@ fn add_assets(
commands.insert_resource(BoxMaterialHandle(box_material_handle)); commands.insert_resource(BoxMaterialHandle(box_material_handle));
} }
#[derive(Component)]
struct ComputeTransform(Task<Transform>);
/// This system generates tasks simulating computationally intensive /// This system generates tasks simulating computationally intensive
/// work that potentially spans multiple frames/ticks. A separate /// work that potentially spans multiple frames/ticks. A separate
/// system, `handle_tasks`, will poll the spawned tasks on subsequent /// system, `handle_tasks`, will poll the spawned tasks on subsequent
@ -66,7 +69,7 @@ fn spawn_tasks(mut commands: Commands, thread_pool: Res<AsyncComputeTaskPool>) {
}); });
// Spawn new entity and add our new task as a component // Spawn new entity and add our new task as a component
commands.spawn().insert(task); commands.spawn().insert(ComputeTransform(task));
} }
} }
} }
@ -78,12 +81,12 @@ fn spawn_tasks(mut commands: Commands, thread_pool: Res<AsyncComputeTaskPool>) {
/// removes the task component from the entity. /// removes the task component from the entity.
fn handle_tasks( fn handle_tasks(
mut commands: Commands, mut commands: Commands,
mut transform_tasks: Query<(Entity, &mut Task<Transform>)>, mut transform_tasks: Query<(Entity, &mut ComputeTransform)>,
box_mesh_handle: Res<BoxMeshHandle>, box_mesh_handle: Res<BoxMeshHandle>,
box_material_handle: Res<BoxMaterialHandle>, box_material_handle: Res<BoxMaterialHandle>,
) { ) {
for (entity, mut task) in transform_tasks.iter_mut() { for (entity, mut task) in transform_tasks.iter_mut() {
if let Some(transform) = future::block_on(future::poll_once(&mut *task)) { if let Some(transform) = future::block_on(future::poll_once(&mut task.0)) {
// Add our new PbrBundle of components to our tagged entity // Add our new PbrBundle of components to our tagged entity
commands.entity(entity).insert_bundle(PbrBundle { commands.entity(entity).insert_bundle(PbrBundle {
mesh: box_mesh_handle.clone(), mesh: box_mesh_handle.clone(),
@ -93,7 +96,7 @@ fn handle_tasks(
}); });
// Task is complete, so remove task component from entity // Task is complete, so remove task component from entity
commands.entity(entity).remove::<Task<Transform>>(); commands.entity(entity).remove::<ComputeTransform>();
} }
} }
} }