From 48b4a45d82dfb0e4bf31575c0f6282b596ca11ad Mon Sep 17 00:00:00 2001 From: JoJoJet <21144246+JoJoJet@users.noreply.github.com> Date: Sun, 25 Dec 2022 00:51:19 +0000 Subject: [PATCH] Add a const `PipeSystem` constructor (#7019) # Objective Fix #5914. `PipeSystem` cannot be constructed in `const` contexts. ## Solution Add a const `PipeSystem::new` function. --- crates/bevy_ecs/src/query/access.rs | 13 +++++++---- crates/bevy_ecs/src/system/system_piping.rs | 24 +++++++++++++++------ 2 files changed, 26 insertions(+), 11 deletions(-) diff --git a/crates/bevy_ecs/src/query/access.rs b/crates/bevy_ecs/src/query/access.rs index a28e6df23ef83..ea18adf841073 100644 --- a/crates/bevy_ecs/src/query/access.rs +++ b/crates/bevy_ecs/src/query/access.rs @@ -71,16 +71,21 @@ impl fmt::Debug for Access { } impl Default for Access { fn default() -> Self { + Self::new() + } +} + +impl Access { + /// Creates an empty [`Access`] collection. + pub const fn new() -> Self { Self { reads_all: false, - reads_and_writes: Default::default(), - writes: Default::default(), + reads_and_writes: FixedBitSet::new(), + writes: FixedBitSet::new(), marker: PhantomData, } } -} -impl Access { /// Increases the set capacity to the specified amount. /// /// Does nothing if `capacity` is less than or equal to the current value. diff --git a/crates/bevy_ecs/src/system/system_piping.rs b/crates/bevy_ecs/src/system/system_piping.rs index d28158b697836..dd343461b9e5f 100644 --- a/crates/bevy_ecs/src/system/system_piping.rs +++ b/crates/bevy_ecs/src/system/system_piping.rs @@ -54,6 +54,21 @@ pub struct PipeSystem { archetype_component_access: Access, } +impl PipeSystem { + /// Manual constructor for creating a [`PipeSystem`]. + /// This should only be used when [`IntoPipeSystem::pipe`] cannot be used, + /// such as in `const` contexts. + pub const fn new(system_a: SystemA, system_b: SystemB, name: Cow<'static, str>) -> Self { + Self { + system_a, + system_b, + name, + component_access: Access::new(), + archetype_component_access: Access::new(), + } + } +} + impl> System for PipeSystem { type In = SystemA::In; type Out = SystemB::Out; @@ -154,13 +169,8 @@ where fn pipe(self, system: SystemB) -> PipeSystem { let system_a = IntoSystem::into_system(self); let system_b = IntoSystem::into_system(system); - PipeSystem { - name: Cow::Owned(format!("Pipe({}, {})", system_a.name(), system_b.name())), - system_a, - system_b, - archetype_component_access: Default::default(), - component_access: Default::default(), - } + let name = format!("Pipe({}, {})", system_a.name(), system_b.name()); + PipeSystem::new(system_a, system_b, Cow::Owned(name)) } }