Skip to content

Commit 06f7c47

Browse files
committed
pull Schedules::ignored_scheduling_ambiguities into a separate resource
1 parent c5c7598 commit 06f7c47

File tree

5 files changed

+75
-75
lines changed

5 files changed

+75
-75
lines changed
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
use core::{
2+
fmt::Write as _,
3+
ops::{Deref, DerefMut},
4+
};
5+
6+
use alloc::{
7+
collections::BTreeSet,
8+
string::{String, ToString},
9+
};
10+
11+
use crate::{
12+
component::{ComponentId, Components},
13+
resource::Resource,
14+
};
15+
16+
/// List of [`ComponentId`]s to ignore when reporting system order ambiguity conflicts.
17+
#[derive(Resource, Default)]
18+
pub struct IgnoredAmbiguities(pub BTreeSet<ComponentId>);
19+
20+
impl IgnoredAmbiguities {
21+
/// Returns a string listing all ignored ambiguity component names.
22+
///
23+
/// May panic or retrieve incorrect names if [`Components`] is not from the
24+
/// same world.
25+
pub fn to_string(&self, components: &Components) -> String {
26+
let mut message =
27+
"System order ambiguities caused by conflicts on the following types are ignored:\n"
28+
.to_string();
29+
for id in self.iter() {
30+
writeln!(message, "{}", components.get_name(*id).unwrap()).unwrap();
31+
}
32+
message
33+
}
34+
}
35+
36+
impl Deref for IgnoredAmbiguities {
37+
type Target = BTreeSet<ComponentId>;
38+
39+
fn deref(&self) -> &Self::Target {
40+
&self.0
41+
}
42+
}
43+
44+
impl DerefMut for IgnoredAmbiguities {
45+
fn deref_mut(&mut self) -> &mut Self::Target {
46+
&mut self.0
47+
}
48+
}

crates/bevy_ecs/src/schedule/mod.rs

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
//! Contains APIs for ordering systems and executing them on a [`World`](crate::world::World)
22
3+
mod ambiguity;
34
mod auto_insert_apply_deferred;
45
mod condition;
56
mod config;
@@ -12,7 +13,9 @@ mod set;
1213
mod stepping;
1314

1415
pub use self::graph::GraphInfo;
15-
pub use self::{condition::*, config::*, error::*, executor::*, node::*, schedule::*, set::*};
16+
pub use self::{
17+
ambiguity::*, condition::*, config::*, error::*, executor::*, node::*, schedule::*, set::*,
18+
};
1619
pub use pass::ScheduleBuildPass;
1720

1821
/// An implementation of a graph data structure.
@@ -801,9 +804,6 @@ mod tests {
801804
}
802805

803806
mod system_ambiguity {
804-
#[cfg(feature = "trace")]
805-
use alloc::collections::BTreeSet;
806-
807807
use super::*;
808808
use crate::prelude::*;
809809

@@ -1145,9 +1145,7 @@ mod tests {
11451145
));
11461146

11471147
schedule.graph_mut().initialize(&mut world);
1148-
let _ = schedule
1149-
.graph_mut()
1150-
.build_schedule(&mut world, &BTreeSet::new());
1148+
let _ = schedule.graph_mut().build_schedule(&mut world);
11511149

11521150
let ambiguities: Vec<_> = schedule
11531151
.graph()
@@ -1204,9 +1202,7 @@ mod tests {
12041202

12051203
let mut world = World::new();
12061204
schedule.graph_mut().initialize(&mut world);
1207-
let _ = schedule
1208-
.graph_mut()
1209-
.build_schedule(&mut world, &BTreeSet::new());
1205+
let _ = schedule.graph_mut().build_schedule(&mut world);
12101206

12111207
let ambiguities: Vec<_> = schedule
12121208
.graph()

crates/bevy_ecs/src/schedule/schedule.rs

Lines changed: 11 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -14,24 +14,17 @@ use bevy_platform::collections::{HashMap, HashSet};
1414
use bevy_utils::{default, TypeIdMap};
1515
use core::{
1616
any::{Any, TypeId},
17-
fmt::{Debug, Write},
17+
fmt::Debug,
1818
};
1919
use fixedbitset::FixedBitSet;
20-
use log::{info, warn};
20+
use log::warn;
2121
use pass::ScheduleBuildPassObj;
2222
use thiserror::Error;
2323
#[cfg(feature = "trace")]
2424
use tracing::info_span;
2525

2626
use crate::{change_detection::CheckChangeTicks, system::System};
27-
use crate::{
28-
component::{ComponentId, Components},
29-
prelude::Component,
30-
resource::Resource,
31-
schedule::*,
32-
system::ScheduleSystem,
33-
world::World,
34-
};
27+
use crate::{resource::Resource, schedule::*, system::ScheduleSystem, world::World};
3528

3629
pub use stepping::Stepping;
3730
use Direction::{Incoming, Outgoing};
@@ -40,8 +33,6 @@ use Direction::{Incoming, Outgoing};
4033
#[derive(Default, Resource)]
4134
pub struct Schedules {
4235
inner: HashMap<InternedScheduleLabel, Schedule>,
43-
/// List of [`ComponentId`]s to ignore when reporting system order ambiguity conflicts
44-
pub ignored_scheduling_ambiguities: BTreeSet<ComponentId>,
4536
}
4637

4738
impl Schedules {
@@ -135,38 +126,6 @@ impl Schedules {
135126
}
136127
}
137128

138-
/// Ignore system order ambiguities caused by conflicts on [`Component`]s of type `T`.
139-
pub fn allow_ambiguous_component<T: Component>(&mut self, world: &mut World) {
140-
self.ignored_scheduling_ambiguities
141-
.insert(world.register_component::<T>());
142-
}
143-
144-
/// Ignore system order ambiguities caused by conflicts on [`Resource`]s of type `T`.
145-
pub fn allow_ambiguous_resource<T: Resource>(&mut self, world: &mut World) {
146-
self.ignored_scheduling_ambiguities
147-
.insert(world.components_registrator().register_resource::<T>());
148-
}
149-
150-
/// Iterate through the [`ComponentId`]'s that will be ignored.
151-
pub fn iter_ignored_ambiguities(&self) -> impl Iterator<Item = &ComponentId> + '_ {
152-
self.ignored_scheduling_ambiguities.iter()
153-
}
154-
155-
/// Prints the names of the components and resources with [`info`]
156-
///
157-
/// May panic or retrieve incorrect names if [`Components`] is not from the same
158-
/// world
159-
pub fn print_ignored_ambiguities(&self, components: &Components) {
160-
let mut message =
161-
"System order ambiguities caused by conflicts on the following types are ignored:\n"
162-
.to_string();
163-
for id in self.iter_ignored_ambiguities() {
164-
writeln!(message, "{}", components.get_name(*id).unwrap()).unwrap();
165-
}
166-
167-
info!("{message}");
168-
}
169-
170129
/// Adds one or more systems to the [`Schedule`] matching the provided [`ScheduleLabel`].
171130
pub fn add_systems<M>(
172131
&mut self,
@@ -562,16 +521,9 @@ impl Schedule {
562521
pub fn initialize(&mut self, world: &mut World) -> Result<(), ScheduleBuildError> {
563522
if self.graph.changed {
564523
self.graph.initialize(world);
565-
let ignored_ambiguities = world
566-
.get_resource_or_init::<Schedules>()
567-
.ignored_scheduling_ambiguities
568-
.clone();
569-
self.warnings = self.graph.update_schedule(
570-
world,
571-
&mut self.executable,
572-
&ignored_ambiguities,
573-
self.label,
574-
)?;
524+
self.warnings = self
525+
.graph
526+
.update_schedule(world, &mut self.executable, self.label)?;
575527
self.graph.changed = false;
576528
self.executor_initialized = false;
577529
}
@@ -1105,7 +1057,6 @@ impl ScheduleGraph {
11051057
pub fn build_schedule(
11061058
&mut self,
11071059
world: &mut World,
1108-
ignored_ambiguities: &BTreeSet<ComponentId>,
11091060
) -> Result<(SystemSchedule, Vec<ScheduleBuildWarning>), ScheduleBuildError> {
11101061
let mut warnings = Vec::new();
11111062

@@ -1189,7 +1140,10 @@ impl ScheduleGraph {
11891140
&flat_dependency_analysis,
11901141
&flat_ambiguous_with,
11911142
&self.ambiguous_with_all,
1192-
ignored_ambiguities,
1143+
world
1144+
.get_resource::<IgnoredAmbiguities>()
1145+
.map(|ia| &ia.0)
1146+
.unwrap_or(&BTreeSet::new()),
11931147
);
11941148
// If there are any ambiguities, log warnings or return errors as configured.
11951149
if self.settings.ambiguity_detection != LogLevel::Ignore
@@ -1309,7 +1263,6 @@ impl ScheduleGraph {
13091263
&mut self,
13101264
world: &mut World,
13111265
schedule: &mut SystemSchedule,
1312-
ignored_ambiguities: &BTreeSet<ComponentId>,
13131266
schedule_label: InternedScheduleLabel,
13141267
) -> Result<Vec<ScheduleBuildWarning>, ScheduleBuildError> {
13151268
if !self.systems.is_initialized() || !self.system_sets.is_initialized() {
@@ -1342,7 +1295,7 @@ impl ScheduleGraph {
13421295
}
13431296
}
13441297

1345-
let (new_schedule, warnings) = self.build_schedule(world, ignored_ambiguities)?;
1298+
let (new_schedule, warnings) = self.build_schedule(world)?;
13461299
*schedule = new_schedule;
13471300

13481301
for warning in &warnings {

crates/bevy_ecs/src/world/mod.rs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ use crate::{
5353
query::{DebugCheckedUnwrap, QueryData, QueryFilter, QueryState},
5454
relationship::RelationshipHookMode,
5555
resource::Resource,
56-
schedule::{Schedule, ScheduleLabel, Schedules},
56+
schedule::{IgnoredAmbiguities, Schedule, ScheduleLabel, Schedules},
5757
storage::{ResourceData, Storages},
5858
system::Commands,
5959
world::{
@@ -3675,16 +3675,14 @@ impl World {
36753675

36763676
/// Ignore system order ambiguities caused by conflicts on [`Component`]s of type `T`.
36773677
pub fn allow_ambiguous_component<T: Component>(&mut self) {
3678-
let mut schedules = self.remove_resource::<Schedules>().unwrap_or_default();
3679-
schedules.allow_ambiguous_component::<T>(self);
3680-
self.insert_resource(schedules);
3678+
let id = self.register_component::<T>();
3679+
self.get_resource_or_init::<IgnoredAmbiguities>().insert(id);
36813680
}
36823681

36833682
/// Ignore system order ambiguities caused by conflicts on [`Resource`]s of type `T`.
36843683
pub fn allow_ambiguous_resource<T: Resource>(&mut self) {
3685-
let mut schedules = self.remove_resource::<Schedules>().unwrap_or_default();
3686-
schedules.allow_ambiguous_resource::<T>(self);
3687-
self.insert_resource(schedules);
3684+
let id = self.components_registrator().register_resource::<T>();
3685+
self.get_resource_or_init::<IgnoredAmbiguities>().insert(id);
36883686
}
36893687
}
36903688

release-content/migration-guides/schedule_cleanup.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,8 @@ pull_requests: [21608, 21817]
2222
- `ScheduleBuildPass::collapse_set` now takes `&HashSet<SystemKey>` instead of a slice, to reduce redundant allocations.
2323
- `simple_cycles_in_component` has been changed from a free function into a method on `DiGraph`.
2424
- `DiGraph::try_into`/`UnGraph::try_into` was renamed to `DiGraph::try_convert`/`UnGraph::try_convert` to prevent overlap with the `TryInto` trait, and now makes use of `TryInto` instead of `TryFrom` for conversions.
25+
- Removed `Schedules::ignored_scheduling_ambiguities`: access the same info via the new `IgnoredAmbiguities` resource instead.
26+
- Removed `Schedules::allow_ambiguous_component` and `Schedules::allow_ambiguous_resource`: use the equivalent functions on `World` instead.
27+
- Removed `Schedules::iter_ignored_ambiguities`: use `IgnoredAmbiguities::iter` via Deref instead.
28+
- Removed `Schedules::print_ignored_ambiguities`: print the string returned by `IgnoredAmbiguities::to_string` yourself.
29+
- Removed the `ignored_ambiguities` parameter from `Schedule::build_schedule`, it is now fetched internally from the new `IgnoredAmbiguities` resource.

0 commit comments

Comments
 (0)