Skip to content

Commit 18c6a7b

Browse files
committed
do not impl Component for Task (bevyengine#4113)
# Objective - `Task` are `Component`. - They should not. ## Solution - Remove the impl, and update the example to show a wrapper. bevyengine#4052 for reference
1 parent 26c3b20 commit 18c6a7b

File tree

2 files changed

+7
-12
lines changed

2 files changed

+7
-12
lines changed

crates/bevy_ecs/src/component.rs

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -55,14 +55,6 @@ mod sealed {
5555
impl Sealed for super::SparseStorage {}
5656
}
5757

58-
// ECS dependencies cannot derive Component, so we must implement it manually for relevant structs.
59-
impl<T> Component for bevy_tasks::Task<T>
60-
where
61-
Self: Send + Sync + 'static,
62-
{
63-
type Storage = TableStorage;
64-
}
65-
6658
/// The storage used for a specific component type.
6759
///
6860
/// # Examples

examples/async_tasks/async_compute.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ fn add_assets(
4343
commands.insert_resource(BoxMaterialHandle(box_material_handle));
4444
}
4545

46+
#[derive(Component)]
47+
struct ComputeTransform(Task<Transform>);
48+
4649
/// This system generates tasks simulating computationally intensive
4750
/// work that potentially spans multiple frames/ticks. A separate
4851
/// system, `handle_tasks`, will poll the spawned tasks on subsequent
@@ -66,7 +69,7 @@ fn spawn_tasks(mut commands: Commands, thread_pool: Res<AsyncComputeTaskPool>) {
6669
});
6770

6871
// Spawn new entity and add our new task as a component
69-
commands.spawn().insert(task);
72+
commands.spawn().insert(ComputeTransform(task));
7073
}
7174
}
7275
}
@@ -78,12 +81,12 @@ fn spawn_tasks(mut commands: Commands, thread_pool: Res<AsyncComputeTaskPool>) {
7881
/// removes the task component from the entity.
7982
fn handle_tasks(
8083
mut commands: Commands,
81-
mut transform_tasks: Query<(Entity, &mut Task<Transform>)>,
84+
mut transform_tasks: Query<(Entity, &mut ComputeTransform)>,
8285
box_mesh_handle: Res<BoxMeshHandle>,
8386
box_material_handle: Res<BoxMaterialHandle>,
8487
) {
8588
for (entity, mut task) in transform_tasks.iter_mut() {
86-
if let Some(transform) = future::block_on(future::poll_once(&mut *task)) {
89+
if let Some(transform) = future::block_on(future::poll_once(&mut task.0)) {
8790
// Add our new PbrBundle of components to our tagged entity
8891
commands.entity(entity).insert_bundle(PbrBundle {
8992
mesh: box_mesh_handle.clone(),
@@ -93,7 +96,7 @@ fn handle_tasks(
9396
});
9497

9598
// Task is complete, so remove task component from entity
96-
commands.entity(entity).remove::<Task<Transform>>();
99+
commands.entity(entity).remove::<ComputeTransform>();
97100
}
98101
}
99102
}

0 commit comments

Comments
 (0)