Skip to content

Commit 4083b7c

Browse files
joseph-giogeieredgar
authored andcommitted
special-case interned system sets
1 parent 2089e12 commit 4083b7c

File tree

3 files changed

+48
-3
lines changed

3 files changed

+48
-3
lines changed

crates/bevy_ecs/macros/src/set.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use bevy_macro_utils::BevyManifest;
12
use proc_macro::TokenStream;
23
use quote::{quote, quote_spanned};
34
use syn::spanned::Spanned;
@@ -9,6 +10,8 @@ use syn::spanned::Spanned;
910
/// - `input`: The [`syn::DeriveInput`] for the struct that we want to derive the set trait for
1011
/// - `trait_path`: The [`syn::Path`] to the set trait
1112
pub fn derive_set(input: syn::DeriveInput, trait_path: &syn::Path) -> TokenStream {
13+
let bevy_utils_path = BevyManifest::default().get_path("bevy_utils");
14+
1215
let ident = input.ident;
1316

1417
let (impl_generics, ty_generics, where_clause) = input.generics.split_for_impl();
@@ -69,6 +72,15 @@ pub fn derive_set(input: syn::DeriveInput, trait_path: &syn::Path) -> TokenStrea
6972
std::boxed::Box::new(std::clone::Clone::clone(self))
7073
}
7174

75+
fn as_dyn_eq(&self) -> &dyn #bevy_utils_path::label::DynEq {
76+
self
77+
}
78+
79+
fn dyn_hash(&self, mut state: &mut dyn ::std::hash::Hasher) {
80+
std::hash::Hash::hash(&std::any::TypeId::of::<Self>(), &mut state);
81+
std::hash::Hash::hash(self, &mut state);
82+
}
83+
7284
fn dyn_static_ref(&self) -> std::option::Option<&'static dyn #trait_path> {
7385
#dyn_static_ref_impl
7486
}

crates/bevy_ecs/src/schedule/set.rs

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use std::marker::PhantomData;
66
pub use bevy_ecs_macros::{ScheduleLabel, SystemSet};
77
use bevy_utils::define_label;
88
use bevy_utils::intern::{Interned, Interner, Leak};
9-
use bevy_utils::label::DynHash;
9+
use bevy_utils::label::DynEq;
1010

1111
use crate::system::{
1212
ExclusiveSystemParamFunction, IsExclusiveFunctionSystem, IsFunctionSystem, SystemParamFunction,
@@ -25,7 +25,7 @@ pub type InternedSystemSet = Interned<dyn SystemSet>;
2525
pub type InternedScheduleLabel = Interned<dyn ScheduleLabel>;
2626

2727
/// Types that identify logical groups of systems.
28-
pub trait SystemSet: DynHash + Debug + Send + Sync + 'static {
28+
pub trait SystemSet: Debug + Send + Sync + 'static {
2929
/// Returns `Some` if this system set is a [`SystemTypeSet`].
3030
fn system_type(&self) -> Option<TypeId> {
3131
None
@@ -39,6 +39,12 @@ pub trait SystemSet: DynHash + Debug + Send + Sync + 'static {
3939
/// Creates a boxed clone of the label corresponding to this system set.
4040
fn dyn_clone(&self) -> Box<dyn SystemSet>;
4141

42+
/// Casts this value to a form where it can be compared with other type-erased values.
43+
fn as_dyn_eq(&self) -> &dyn DynEq;
44+
45+
/// Feeds this value into the given [`Hasher`].
46+
fn dyn_hash(&self, state: &mut dyn Hasher);
47+
4248
/// Returns a static reference to a value equal to `self`, if possible.
4349
/// This method is used to optimize [interning](bevy_utils::intern).
4450
///
@@ -97,6 +103,14 @@ impl SystemSet for Interned<dyn SystemSet> {
97103
(**self).dyn_clone()
98104
}
99105

106+
fn as_dyn_eq(&self) -> &dyn DynEq {
107+
(**self).as_dyn_eq()
108+
}
109+
110+
fn dyn_hash(&self, state: &mut dyn Hasher) {
111+
(**self).dyn_hash(state)
112+
}
113+
100114
fn dyn_static_ref(&self) -> Option<&'static dyn SystemSet> {
101115
Some(self.0)
102116
}
@@ -111,7 +125,7 @@ impl SystemSet for Interned<dyn SystemSet> {
111125

112126
impl PartialEq for dyn SystemSet {
113127
fn eq(&self, other: &Self) -> bool {
114-
self.dyn_eq(other.as_dyn_eq())
128+
self.as_dyn_eq().dyn_eq(other.as_dyn_eq())
115129
}
116130
}
117131

@@ -187,6 +201,15 @@ impl<T> SystemSet for SystemTypeSet<T> {
187201
Box::new(*self)
188202
}
189203

204+
fn as_dyn_eq(&self) -> &dyn DynEq {
205+
self
206+
}
207+
208+
fn dyn_hash(&self, mut state: &mut dyn Hasher) {
209+
std::any::TypeId::of::<Self>().hash(&mut state);
210+
self.hash(&mut state);
211+
}
212+
190213
fn dyn_static_ref(&self) -> Option<&'static dyn SystemSet> {
191214
Some(&Self(PhantomData))
192215
}
@@ -208,6 +231,15 @@ impl SystemSet for AnonymousSet {
208231
true
209232
}
210233

234+
fn as_dyn_eq(&self) -> &dyn DynEq {
235+
self
236+
}
237+
238+
fn dyn_hash(&self, mut state: &mut dyn Hasher) {
239+
std::any::TypeId::of::<Self>().hash(&mut state);
240+
self.hash(&mut state);
241+
}
242+
211243
fn dyn_clone(&self) -> Box<dyn SystemSet> {
212244
Box::new(*self)
213245
}

crates/bevy_transform/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ bevy_ecs = { path = "../bevy_ecs", version = "0.12.0-dev", features = ["bevy_ref
1515
bevy_hierarchy = { path = "../bevy_hierarchy", version = "0.12.0-dev" }
1616
bevy_math = { path = "../bevy_math", version = "0.12.0-dev" }
1717
bevy_reflect = { path = "../bevy_reflect", version = "0.12.0-dev", features = ["bevy"] }
18+
bevy_utils = { path = "../bevy_utils", version = "0.12.0-dev" }
1819
serde = { version = "1", features = ["derive"], optional = true }
1920

2021
[dev-dependencies]

0 commit comments

Comments
 (0)