Skip to content

Commit c01cd76

Browse files
Allow the use of implicit system labels with ambiguous_with
1 parent fda1ebe commit c01cd76

File tree

2 files changed

+36
-17
lines changed

2 files changed

+36
-17
lines changed

crates/bevy_ecs/src/schedule/system_descriptor.rs

+27-11
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ pub trait ParallelSystemDescriptorCoercion<Params> {
154154

155155
/// Specifies that the system is exempt from execution order ambiguity detection
156156
/// with other systems with the given label.
157-
fn ambiguous_with(self, label: impl SystemLabel) -> ParallelSystemDescriptor;
157+
fn ambiguous_with<Marker>(self, label: impl AsSystemLabel<Marker>) -> ParallelSystemDescriptor;
158158
}
159159

160160
impl ParallelSystemDescriptorCoercion<()> for ParallelSystemDescriptor {
@@ -186,14 +186,20 @@ impl ParallelSystemDescriptorCoercion<()> for ParallelSystemDescriptor {
186186
self
187187
}
188188

189-
fn ambiguous_with(mut self, label: impl SystemLabel) -> ParallelSystemDescriptor {
189+
fn ambiguous_with<Marker>(
190+
mut self,
191+
label: impl AsSystemLabel<Marker>,
192+
) -> ParallelSystemDescriptor {
193+
let system_label = label.as_system_label();
194+
let boxed_system_label: Box<dyn SystemLabel> = Box::new(system_label);
195+
190196
match &mut self.ambiguity_detection {
191197
AmbiguityDetection::IgnoreWithLabel(v) => {
192-
v.push(Box::new(label));
198+
v.push(boxed_system_label);
193199
}
194200
_ => {
195201
self.ambiguity_detection =
196-
AmbiguityDetection::IgnoreWithLabel(vec![Box::new(label)]);
202+
AmbiguityDetection::IgnoreWithLabel(vec![boxed_system_label]);
197203
}
198204
}
199205
self
@@ -228,7 +234,7 @@ where
228234
new_parallel_descriptor(Box::new(IntoSystem::into_system(self))).ignore_all_ambiguities()
229235
}
230236

231-
fn ambiguous_with(self, label: impl SystemLabel) -> ParallelSystemDescriptor {
237+
fn ambiguous_with<Marker>(self, label: impl AsSystemLabel<Marker>) -> ParallelSystemDescriptor {
232238
new_parallel_descriptor(Box::new(IntoSystem::into_system(self))).ambiguous_with(label)
233239
}
234240
}
@@ -257,7 +263,7 @@ impl ParallelSystemDescriptorCoercion<()> for BoxedSystem<(), ()> {
257263
new_parallel_descriptor(self).ignore_all_ambiguities()
258264
}
259265

260-
fn ambiguous_with(self, label: impl SystemLabel) -> ParallelSystemDescriptor {
266+
fn ambiguous_with<Marker>(self, label: impl AsSystemLabel<Marker>) -> ParallelSystemDescriptor {
261267
new_parallel_descriptor(self).ambiguous_with(label)
262268
}
263269
}
@@ -324,7 +330,8 @@ pub trait ExclusiveSystemDescriptorCoercion {
324330

325331
/// Specifies that the system is exempt from execution order ambiguity detection
326332
/// with other systems with the given label.
327-
fn ambiguous_with(self, label: impl SystemLabel) -> ExclusiveSystemDescriptor;
333+
fn ambiguous_with<Marker>(self, label: impl AsSystemLabel<Marker>)
334+
-> ExclusiveSystemDescriptor;
328335
}
329336

330337
impl ExclusiveSystemDescriptorCoercion for ExclusiveSystemDescriptor {
@@ -371,14 +378,20 @@ impl ExclusiveSystemDescriptorCoercion for ExclusiveSystemDescriptor {
371378
self
372379
}
373380

374-
fn ambiguous_with(mut self, label: impl SystemLabel) -> ExclusiveSystemDescriptor {
381+
fn ambiguous_with<Marker>(
382+
mut self,
383+
label: impl AsSystemLabel<Marker>,
384+
) -> ExclusiveSystemDescriptor {
385+
let system_label = label.as_system_label();
386+
let boxed_system_label: Box<dyn SystemLabel> = Box::new(system_label);
387+
375388
match &mut self.ambiguity_detection {
376389
AmbiguityDetection::IgnoreWithLabel(v) => {
377-
v.push(Box::new(label));
390+
v.push(boxed_system_label);
378391
}
379392
_ => {
380393
self.ambiguity_detection =
381-
AmbiguityDetection::IgnoreWithLabel(vec![Box::new(label)]);
394+
AmbiguityDetection::IgnoreWithLabel(vec![boxed_system_label]);
382395
}
383396
}
384397
self
@@ -424,7 +437,10 @@ where
424437
new_exclusive_descriptor(Box::new(self)).silence_ambiguity_checks()
425438
}
426439

427-
fn ambiguous_with(self, label: impl SystemLabel) -> ExclusiveSystemDescriptor {
440+
fn ambiguous_with<Marker>(
441+
self,
442+
label: impl AsSystemLabel<Marker>,
443+
) -> ExclusiveSystemDescriptor {
428444
new_exclusive_descriptor(Box::new(self)).ambiguous_with(label)
429445
}
430446
}

examples/ecs/ambiguity_checker.rs

+9-6
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,20 @@ fn main() {
1818
.insert_resource(MyOtherResource(0))
1919
// It is possible to mark a system as ambiguous if this is intended behavior; the ambiguity checker will ignore this system.
2020
.add_system(system_a.ignore_all_ambiguities())
21-
.add_system(system_b.label("my_label"))
22-
// It is also possible to mark a system as deliberately ambiguous with a provided lable,
21+
.add_system(system_b)
22+
// It is also possible to mark a system as deliberately ambiguous with a provided system or label,
2323
// making the checker ignore any ambiguities between them.
24-
.add_system(system_c.ambiguous_with("my_label"))
24+
.add_system(system_c.ambiguous_with(system_b))
2525
// If there's an whole group of systems that are supposed to be ambiguous with each other,
26-
// add a common label, and then ignore any conflicts with that label.
27-
.add_system(system_d.label("my_set").ambiguous_with("my_set"))
28-
.add_system(system_e.label("my_set").ambiguous_with("my_set"))
26+
// add a shared label, and then ignore any conflicts with that label.
27+
.add_system(system_d.label(AmbiguitySet).ambiguous_with(AmbiguitySet))
28+
.add_system(system_e.label(AmbiguitySet).ambiguous_with(AmbiguitySet))
2929
.run();
3030
}
3131

32+
#[derive(SystemLabel, Debug, Clone, PartialEq, Eq, Hash)]
33+
struct AmbiguitySet;
34+
3235
struct MyStartupResource(i32);
3336
fn startup_system_a(mut res: ResMut<MyStartupResource>) {
3437
res.0 += 1;

0 commit comments

Comments
 (0)