Skip to content

Commit d6d78d2

Browse files
committed
Update uses of labels in the repo
1 parent 61f86e0 commit d6d78d2

File tree

4 files changed

+31
-34
lines changed

4 files changed

+31
-34
lines changed

benches/benches/bevy_ecs/scheduling/schedule.rs

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -64,14 +64,17 @@ pub fn build_schedule(criterion: &mut Criterion) {
6464
// Use multiple different kinds of label to ensure that dynamic dispatch
6565
// doesn't somehow get optimized away.
6666
#[derive(Debug, Clone, Copy)]
67-
struct NumLabel(usize);
67+
struct NumLabel(u64);
6868
#[derive(Debug, Clone, Copy, SystemLabel)]
6969
struct DummyLabel;
7070

7171
impl SystemLabel for NumLabel {
72-
fn as_str(&self) -> &'static str {
73-
let s = self.0.to_string();
74-
Box::leak(s.into_boxed_str())
72+
#[inline]
73+
fn data(&self) -> u64 {
74+
self.0
75+
}
76+
fn fmt(data: u64, f: &mut std::fmt::Formatter) -> std::fmt::Result {
77+
f.debug_tuple("NumLabel").field(&data).finish()
7578
}
7679
}
7780

@@ -82,10 +85,6 @@ pub fn build_schedule(criterion: &mut Criterion) {
8285
// Method: generate a set of `graph_size` systems which have a One True Ordering.
8386
// Add system to the stage with full constraints. Hopefully this should be maximimally
8487
// difficult for bevy to figure out.
85-
// Also, we are performing the `as_label` operation outside of the loop since that
86-
// requires an allocation and a leak. This is not something that would be necessary in a
87-
// real scenario, just a contrivance for the benchmark.
88-
let labels: Vec<_> = (0..1000).map(|i| NumLabel(i).as_label()).collect();
8988

9089
// Benchmark graphs of different sizes.
9190
for graph_size in [100, 500, 1000] {
@@ -109,12 +108,12 @@ pub fn build_schedule(criterion: &mut Criterion) {
109108
// Build a fully-connected dependency graph describing the One True Ordering.
110109
// Not particularly realistic but this can be refined later.
111110
for i in 0..graph_size {
112-
let mut sys = empty_system.label(labels[i]).before(DummyLabel);
111+
let mut sys = empty_system.label(NumLabel(i)).before(DummyLabel);
113112
for a in 0..i {
114-
sys = sys.after(labels[a]);
113+
sys = sys.after(NumLabel(a));
115114
}
116115
for b in i + 1..graph_size {
117-
sys = sys.before(labels[b]);
116+
sys = sys.before(NumLabel(b));
118117
}
119118
app.add_system(sys);
120119
}

crates/bevy_app/src/app.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -379,9 +379,9 @@ impl App {
379379
stage_label: impl StageLabel,
380380
system: impl IntoSystemDescriptor<Params>,
381381
) -> &mut Self {
382-
use std::any::TypeId;
382+
let stage_label = stage_label.as_label();
383383
assert!(
384-
stage_label.type_id() != TypeId::of::<StartupStage>(),
384+
!stage_label.is::<StartupStage>(),
385385
"add systems to a startup stage using App::add_startup_system_to_stage"
386386
);
387387
self.schedule.add_system_to_stage(stage_label, system);
@@ -414,9 +414,9 @@ impl App {
414414
stage_label: impl StageLabel,
415415
system_set: SystemSet,
416416
) -> &mut Self {
417-
use std::any::TypeId;
417+
let stage_label = stage_label.as_label();
418418
assert!(
419-
stage_label.type_id() != TypeId::of::<StartupStage>(),
419+
!stage_label.is::<StartupStage>(),
420420
"add system sets to a startup stage using App::add_startup_system_set_to_stage"
421421
);
422422
self.schedule
@@ -952,7 +952,7 @@ impl App {
952952
pub fn sub_app_mut(&mut self, label: impl AppLabel) -> &mut App {
953953
match self.get_sub_app_mut(label) {
954954
Ok(app) => app,
955-
Err(label) => panic!("Sub-App with label '{:?}' does not exist", label.as_str()),
955+
Err(label) => panic!("Sub-App with label '{:?}' does not exist", label.as_label()),
956956
}
957957
}
958958

@@ -974,13 +974,13 @@ impl App {
974974
pub fn sub_app(&self, label: impl AppLabel) -> &App {
975975
match self.get_sub_app(label) {
976976
Ok(app) => app,
977-
Err(label) => panic!("Sub-App with label '{:?}' does not exist", label.as_str()),
977+
Err(label) => panic!("Sub-App with label '{:?}' does not exist", label.as_label()),
978978
}
979979
}
980980

981981
/// Retrieves a `SubApp` inside this [`App`] with the given label, if it exists. Otherwise returns
982982
/// an [`Err`] containing the given label.
983-
pub fn get_sub_app(&self, label: impl AppLabel) -> Result<&App, impl AppLabel> {
983+
pub fn get_sub_app<L: AppLabel>(&self, label: L) -> Result<&App, L> {
984984
self.sub_apps
985985
.get(&label.as_label())
986986
.map(|sub_app| &sub_app.app)

crates/bevy_ecs/src/schedule/state.rs

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ use crate::{
66
system::{In, IntoChainSystem, Local, Res, ResMut, Resource},
77
};
88
use std::{
9-
any::TypeId,
109
fmt::{self, Debug},
1110
hash::Hash,
1211
};
@@ -54,20 +53,16 @@ enum ScheduledOperation<T: StateData> {
5453
Push(T),
5554
}
5655

57-
#[derive(Debug, PartialEq, Eq, Clone, Hash)]
58-
struct DriverLabel(TypeId, &'static str);
59-
impl RunCriteriaLabel for DriverLabel {
60-
fn type_id(&self) -> core::any::TypeId {
61-
self.0
62-
}
63-
fn as_str(&self) -> &'static str {
64-
self.1
65-
}
66-
}
67-
56+
struct DriverLabel;
6857
impl DriverLabel {
69-
fn of<T: 'static>() -> Self {
70-
Self(TypeId::of::<T>(), std::any::type_name::<T>())
58+
fn of<T: 'static>() -> impl RunCriteriaLabel {
59+
use std::marker::PhantomData;
60+
61+
#[derive(RunCriteriaLabel)]
62+
#[run_criteria_label(ignore_fields)]
63+
struct DriverLabel<T>(PhantomData<fn() -> T>);
64+
65+
DriverLabel::<T>(PhantomData)
7166
}
7267
}
7368

crates/bevy_ecs/src/system/function_system.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -455,8 +455,11 @@ pub struct SystemTypeIdLabel<T: 'static>(PhantomData<fn() -> T>);
455455

456456
impl<T: 'static> SystemLabel for SystemTypeIdLabel<T> {
457457
#[inline]
458-
fn as_str(&self) -> &'static str {
459-
std::any::type_name::<T>()
458+
fn data(&self) -> u64 {
459+
0
460+
}
461+
fn fmt(_: u64, f: &mut std::fmt::Formatter) -> std::fmt::Result {
462+
write!(f, "{}", std::any::type_name::<T>())
460463
}
461464
}
462465

0 commit comments

Comments
 (0)