Skip to content

Commit

Permalink
Expose SystemMeta's access field as part of public API (#16625)
Browse files Browse the repository at this point in the history
# Objective

Outside of the `bevy_ecs` crate it's hard to implement `SystemParam`
trait on params that require access to the `World`, because `init_state`
expects user to extend access in `SystemMeta` and access-related fields
of `SystemMeta` are private.

## Solution

Expose those fields as a functions
  • Loading branch information
vil-mo authored Dec 5, 2024
1 parent ea33fc0 commit 67bd2b0
Showing 1 changed file with 49 additions and 0 deletions.
49 changes: 49 additions & 0 deletions crates/bevy_ecs/src/system/function_system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,55 @@ impl SystemMeta {
{
self.param_warn_policy.try_warn::<P>(&self.name);
}

/// Archetype component access that is used to determine which systems can run in parallel with each other
/// in the multithreaded executor.
///
/// We use an [`ArchetypeComponentId`] as it is more precise than just checking [`ComponentId`]:
/// for example if you have one system with `Query<&mut A, With<B>`, and one system with `Query<&mut A, Without<B>`,
/// they conflict if you just look at the [`ComponentId`];
/// but no archetype that matches the first query will match the second and vice versa,
/// which means there's no risk of conflict.
#[inline]
pub fn archetype_component_access(&self) -> &Access<ArchetypeComponentId> {
&self.archetype_component_access
}

/// Returns a mutable reference to the [`Access`] for [`ArchetypeComponentId`].
/// This is used to determine which systems can run in parallel with each other
/// in the multithreaded executor.
///
/// We use an [`ArchetypeComponentId`] as it is more precise than just checking [`ComponentId`]:
/// for example if you have one system with `Query<&mut A, With<B>`, and one system with `Query<&mut A, Without<B>`,
/// they conflict if you just look at the [`ComponentId`];
/// but no archetype that matches the first query will match the second and vice versa,
/// which means there's no risk of conflict.
///
/// # Safety
///
/// No access can be removed from the returned [`Access`].
#[inline]
pub unsafe fn archetype_component_access_mut(&mut self) -> &mut Access<ArchetypeComponentId> {
&mut self.archetype_component_access
}

/// Returns a reference to the [`FilteredAccessSet`] for [`ComponentId`].
/// Used to check if systems and/or system params have conflicting access.
#[inline]
pub fn component_access_set(&self) -> &FilteredAccessSet<ComponentId> {
&self.component_access_set
}

/// Returns a mutable reference to the [`FilteredAccessSet`] for [`ComponentId`].
/// Used internally to statically check if systems have conflicting access.
///
/// # Safety
///
/// No access can be removed from the returned [`FilteredAccessSet`].
#[inline]
pub unsafe fn component_access_set_mut(&mut self) -> &mut FilteredAccessSet<ComponentId> {
&mut self.component_access_set
}
}

/// State machine for emitting warnings when [system params are invalid](System::validate_param).
Expand Down

0 comments on commit 67bd2b0

Please sign in to comment.