Skip to content

Commit

Permalink
tuple methods and chaining
Browse files Browse the repository at this point in the history
  • Loading branch information
maniwani committed Nov 15, 2022
1 parent 3c1cdbf commit 7754cda
Show file tree
Hide file tree
Showing 4 changed files with 155 additions and 120 deletions.
102 changes: 54 additions & 48 deletions crates/bevy_ecs/src/schedule_v3/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -340,50 +340,51 @@ mod sealed {
}

/// A collection of [`SystemConfig`].
pub struct SystemCollection {
inner: Vec<SystemConfig>,
pub struct SystemConfigs {
pub(super) systems: Vec<SystemConfig>,
pub(super) chained: bool,
}

/// Methods that can configure multiple systems at once.
pub trait IntoSystemCollection<Params>
/// Types that can convert into a [`SystemConfigs`].
pub trait IntoSystemConfigs<Params>
where
Self: Sized,
{
/// Convert into a [`SystemCollection`].
/// Convert into a [`SystemConfigs`].
#[doc(hidden)]
fn into_collection(self) -> SystemCollection;
fn into_configs(self) -> SystemConfigs;

/// Add to `set` membership.
fn in_set(self, set: impl SystemSet) -> SystemCollection {
self.into_collection().in_set(set)
fn in_set(self, set: impl SystemSet) -> SystemConfigs {
self.into_configs().in_set(set)
}

/// Run before all members of `set`.
fn before<M>(self, set: impl IntoSystemSet<M>) -> SystemCollection {
self.into_collection().before(set)
fn before<M>(self, set: impl IntoSystemSet<M>) -> SystemConfigs {
self.into_configs().before(set)
}

/// Run after all members of `set`.
fn after<M>(self, set: impl IntoSystemSet<M>) -> SystemCollection {
self.into_collection().after(set)
fn after<M>(self, set: impl IntoSystemSet<M>) -> SystemConfigs {
self.into_configs().after(set)
}

/// Treat this collection as a sequence.
///
/// Ordering constraints will be applied between the successive collection elements.
fn chain(self) -> SystemCollection {
self.into_collection().chain()
fn chain(self) -> SystemConfigs {
self.into_configs().chain()
}
}

impl IntoSystemCollection<()> for SystemCollection {
fn into_collection(self) -> Self {
impl IntoSystemConfigs<()> for SystemConfigs {
fn into_configs(self) -> Self {
self
}

fn in_set(mut self, set: impl SystemSet) -> Self {
assert!(!set.is_system_type(), "invalid use of system type set");
for config in self.inner.iter_mut() {
for config in self.systems.iter_mut() {
config.graph_info.sets.insert(set.dyn_clone());
}

Expand All @@ -392,7 +393,7 @@ impl IntoSystemCollection<()> for SystemCollection {

fn before<M>(mut self, set: impl IntoSystemSet<M>) -> Self {
let set = set.into_system_set();
for config in self.inner.iter_mut() {
for config in self.systems.iter_mut() {
config
.graph_info
.edges
Expand All @@ -404,7 +405,7 @@ impl IntoSystemCollection<()> for SystemCollection {

fn after<M>(mut self, set: impl IntoSystemSet<M>) -> Self {
let set = set.into_system_set();
for config in self.inner.iter_mut() {
for config in self.systems.iter_mut() {
config
.graph_info
.edges
Expand All @@ -415,55 +416,57 @@ impl IntoSystemCollection<()> for SystemCollection {
}

fn chain(mut self) -> Self {
todo!()
self.chained = true;
self
}
}

/// A collection of [`SystemSetConfig`].
pub struct SystemSetCollection {
inner: Vec<SystemSetConfig>,
pub struct SystemSetConfigs {
pub(super) sets: Vec<SystemSetConfig>,
pub(super) chained: bool,
}

/// Methods that can configure multiple system sets at once.
pub trait IntoSystemSetCollection
/// Types that can convert into a [`SystemSetConfigs`].
pub trait IntoSystemSetConfigs
where
Self: Sized,
{
/// Convert into a [`SystemSetCollection`].
/// Convert into a [`SystemSetConfigs`].
#[doc(hidden)]
fn into_collection(self) -> SystemSetCollection;
fn into_configs(self) -> SystemSetConfigs;

/// Add to `set` membership.
fn in_set(self, set: impl SystemSet) -> SystemSetCollection {
self.into_collection().in_set(set)
fn in_set(self, set: impl SystemSet) -> SystemSetConfigs {
self.into_configs().in_set(set)
}

/// Run before all members of `set`.
fn before<M>(self, set: impl IntoSystemSet<M>) -> SystemSetCollection {
self.into_collection().before(set)
fn before<M>(self, set: impl IntoSystemSet<M>) -> SystemSetConfigs {
self.into_configs().before(set)
}

/// Run after all members of `set`.
fn after<M>(self, set: impl IntoSystemSet<M>) -> SystemSetCollection {
self.into_collection().after(set)
fn after<M>(self, set: impl IntoSystemSet<M>) -> SystemSetConfigs {
self.into_configs().after(set)
}

/// Treat this collection as a sequence.
///
/// Ordering constraints will be applied between the successive collection elements.
fn chain(self) -> SystemSetCollection {
self.into_collection().chain()
fn chain(self) -> SystemSetConfigs {
self.into_configs().chain()
}
}

impl IntoSystemSetCollection for SystemSetCollection {
fn into_collection(self) -> Self {
impl IntoSystemSetConfigs for SystemSetConfigs {
fn into_configs(self) -> Self {
self
}

fn in_set(mut self, set: impl SystemSet) -> Self {
assert!(!set.is_system_type(), "invalid use of system type set");
for config in self.inner.iter_mut() {
for config in self.sets.iter_mut() {
config.graph_info.sets.insert(set.dyn_clone());
}

Expand All @@ -472,7 +475,7 @@ impl IntoSystemSetCollection for SystemSetCollection {

fn before<M>(mut self, set: impl IntoSystemSet<M>) -> Self {
let set = set.into_system_set();
for config in self.inner.iter_mut() {
for config in self.sets.iter_mut() {
config
.graph_info
.edges
Expand All @@ -484,7 +487,7 @@ impl IntoSystemSetCollection for SystemSetCollection {

fn after<M>(mut self, set: impl IntoSystemSet<M>) -> Self {
let set = set.into_system_set();
for config in self.inner.iter_mut() {
for config in self.sets.iter_mut() {
config
.graph_info
.edges
Expand All @@ -495,21 +498,23 @@ impl IntoSystemSetCollection for SystemSetCollection {
}

fn chain(mut self) -> Self {
todo!()
self.chained = true;
self
}
}

macro_rules! impl_system_collection {
($($param: ident, $sys: ident),*) => {
impl<$($param, $sys),*> IntoSystemCollection<($($param),*)> for ($($sys),*)
impl<$($param, $sys),*> IntoSystemConfigs<($($param),*)> for ($($sys),*)
where
$($sys: IntoSystemConfig<$param>),*
{
#[allow(non_snake_case)]
fn into_collection(self) -> SystemCollection {
fn into_configs(self) -> SystemConfigs {
let ($($sys,)*) = self;
SystemCollection {
inner: vec![$($sys.into_config(),)*],
SystemConfigs {
systems: vec![$($sys.into_config(),)*],
chained: false,
}
}
}
Expand All @@ -518,13 +523,14 @@ macro_rules! impl_system_collection {

macro_rules! impl_system_set_collection {
($($set: ident),*) => {
impl<$($set: IntoSystemSetConfig),*> IntoSystemSetCollection for ($($set),*)
impl<$($set: IntoSystemSetConfig),*> IntoSystemSetConfigs for ($($set),*)
{
#[allow(non_snake_case)]
fn into_collection(self) -> SystemSetCollection {
fn into_configs(self) -> SystemSetConfigs {
let ($($set,)*) = self;
SystemSetCollection {
inner: vec![$($set.into_config(),)*],
SystemSetConfigs {
sets: vec![$($set.into_config(),)*],
chained: false,
}
}
}
Expand Down
11 changes: 0 additions & 11 deletions crates/bevy_ecs/src/schedule_v3/executor/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,14 +83,3 @@ pub(super) fn is_apply_system_buffers(system: &BoxedSystem) -> bool {
let type_id = get_type_id(&IntoSystem::into_system(apply_system_buffers));
(&*system as &dyn Any).type_id() == type_id
}

#[cfg(test)]
mod tests {

#[test]
fn executor_parity() {
// In the absence of ambiguities, the single-threaded and
// multi-threaded executors must return the same results.
todo!();
}
}
14 changes: 7 additions & 7 deletions crates/bevy_ecs/src/schedule_v3/executor/multi_threaded.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ impl SystemExecutor for MultiThreadedExecutor {
self.spawn_system_tasks(scope, schedule, world);
}

if self.running_systems.count_ones(..) != 0 {
if self.running_systems.count_ones(..) > 0 {
// wait for systems to complete
let index = self
.receiver
Expand Down Expand Up @@ -271,7 +271,7 @@ impl MultiThreadedExecutor {
.extend(&system_meta.archetype_component_access);

self.ready_systems.set(system_index, false);
self.running_systems.set(system_index, true);
self.running_systems.insert(system_index);

if system_meta.is_send {
scope.spawn(task);
Expand Down Expand Up @@ -411,7 +411,7 @@ impl MultiThreadedExecutor {
unsafe { condition.run_unsafe((), world) }
});

self.completed_sets.set(set_idx, true);
self.completed_sets.insert(set_idx);

if !set_conditions_met {
// mark all members as completed
Expand Down Expand Up @@ -470,14 +470,14 @@ impl MultiThreadedExecutor {
}

self.running_systems.set(system_index, false);
self.completed_systems.set(system_index, true);
self.unapplied_systems.set(system_index, true);
self.completed_systems.insert(system_index);
self.unapplied_systems.insert(system_index);
self.signal_dependents(system_index);
}

fn skip_system_and_signal_dependents(&mut self, system_index: usize) {
self.ready_systems.set(system_index, false);
self.completed_systems.set(system_index, true);
self.completed_systems.insert(system_index);
self.signal_dependents(system_index);
}

Expand All @@ -493,7 +493,7 @@ impl MultiThreadedExecutor {
if (dependent_meta.dependencies_remaining == 0)
&& !self.completed_systems.contains(dep_idx)
{
self.ready_systems.set(dep_idx, true);
self.ready_systems.insert(dep_idx);
}
}

Expand Down
Loading

0 comments on commit 7754cda

Please sign in to comment.