@@ -42,11 +42,11 @@ pub struct ReflectComponent(ReflectComponentFns);
42
42
#[ derive( Clone ) ]
43
43
pub struct ReflectComponentFns {
44
44
/// Function pointer implementing [`ReflectComponent::insert()`].
45
- pub insert : fn ( & mut World , Entity , & dyn Reflect ) ,
45
+ pub insert : fn ( & mut EntityMut , & dyn Reflect ) ,
46
46
/// Function pointer implementing [`ReflectComponent::apply()`].
47
47
pub apply : fn ( & mut EntityMut , & dyn Reflect ) ,
48
48
/// Function pointer implementing [`ReflectComponent::apply_or_insert()`].
49
- pub apply_or_insert : fn ( & mut World , Entity , & dyn Reflect ) ,
49
+ pub apply_or_insert : fn ( & mut EntityMut , & dyn Reflect ) ,
50
50
/// Function pointer implementing [`ReflectComponent::remove()`].
51
51
pub remove : fn ( & mut EntityMut ) ,
52
52
/// Function pointer implementing [`ReflectComponent::contains()`].
@@ -56,7 +56,7 @@ pub struct ReflectComponentFns {
56
56
/// Function pointer implementing [`ReflectComponent::reflect_mut()`].
57
57
pub reflect_mut : for <' a > fn ( & ' a mut EntityMut < ' a > ) -> Option < Mut < ' a , dyn Reflect > > ,
58
58
/// Function pointer implementing [`ReflectComponent::reflect_unchecked_mut()`].
59
- pub reflect_unchecked_mut : unsafe fn ( & World , Entity ) -> Option < Mut < dyn Reflect > > ,
59
+ pub reflect_unchecked_mut : unsafe fn ( EntityRef ) -> Option < Mut < dyn Reflect > > ,
60
60
/// Function pointer implementing [`ReflectComponent::copy()`].
61
61
pub copy : fn ( & World , & mut World , Entity , Entity ) ,
62
62
}
@@ -74,12 +74,8 @@ impl ReflectComponentFns {
74
74
75
75
impl ReflectComponent {
76
76
/// Insert a reflected [`Component`] into the entity like [`insert()`](crate::world::EntityMut::insert).
77
- ///
78
- /// # Panics
79
- ///
80
- /// Panics if there is no such entity.
81
- pub fn insert ( & self , world : & mut World , entity : Entity , component : & dyn Reflect ) {
82
- ( self . 0 . insert ) ( world, entity, component) ;
77
+ pub fn insert ( & self , entity : & mut EntityMut , component : & dyn Reflect ) {
78
+ ( self . 0 . insert ) ( entity, component) ;
83
79
}
84
80
85
81
/// Uses reflection to set the value of this [`Component`] type in the entity to the given value.
@@ -92,12 +88,8 @@ impl ReflectComponent {
92
88
}
93
89
94
90
/// Uses reflection to set the value of this [`Component`] type in the entity to the given value or insert a new one if it does not exist.
95
- ///
96
- /// # Panics
97
- ///
98
- /// Panics if the `entity` does not exist.
99
- pub fn apply_or_insert ( & self , world : & mut World , entity : Entity , component : & dyn Reflect ) {
100
- ( self . 0 . apply_or_insert ) ( world, entity, component) ;
91
+ pub fn apply_or_insert ( & self , entity : & mut EntityMut , component : & dyn Reflect ) {
92
+ ( self . 0 . apply_or_insert ) ( entity, component) ;
101
93
}
102
94
103
95
/// Removes this [`Component`] type from the entity. Does nothing if it doesn't exist.
@@ -132,10 +124,9 @@ impl ReflectComponent {
132
124
/// * Don't call this method more than once in the same scope for a given [`Component`].
133
125
pub unsafe fn reflect_unchecked_mut < ' a > (
134
126
& self ,
135
- world : & ' a World ,
136
- entity : Entity ,
127
+ entity : EntityRef < ' a > ,
137
128
) -> Option < Mut < ' a , dyn Reflect > > {
138
- ( self . 0 . reflect_unchecked_mut ) ( world , entity)
129
+ ( self . 0 . reflect_unchecked_mut ) ( entity)
139
130
}
140
131
141
132
/// Gets the value of this [`Component`] type from entity from `source_world` and [applies](Self::apply()) it to the value of this [`Component`] type in entity in `destination_world`.
@@ -177,22 +168,22 @@ impl ReflectComponent {
177
168
impl < C : Component + Reflect + FromWorld > FromType < C > for ReflectComponent {
178
169
fn from_type ( ) -> Self {
179
170
ReflectComponent ( ReflectComponentFns {
180
- insert : |world , entity, reflected_component| {
181
- let mut component = C :: from_world ( world) ;
171
+ insert : |entity, reflected_component| {
172
+ let mut component = entity . world_scope ( |world| C :: from_world ( world) ) ;
182
173
component. apply ( reflected_component) ;
183
- world . entity_mut ( entity) . insert ( component) ;
174
+ entity. insert ( component) ;
184
175
} ,
185
176
apply : |entity, reflected_component| {
186
177
let mut component = entity. get_mut :: < C > ( ) . unwrap ( ) ;
187
178
component. apply ( reflected_component) ;
188
179
} ,
189
- apply_or_insert : |world , entity, reflected_component| {
190
- if let Some ( mut component) = world . get_mut :: < C > ( entity ) {
180
+ apply_or_insert : |entity, reflected_component| {
181
+ if let Some ( mut component) = entity . get_mut :: < C > ( ) {
191
182
component. apply ( reflected_component) ;
192
183
} else {
193
- let mut component = C :: from_world ( world) ;
184
+ let mut component = entity . world_scope ( |world| C :: from_world ( world) ) ;
194
185
component. apply ( reflected_component) ;
195
- world . entity_mut ( entity) . insert ( component) ;
186
+ entity. insert ( component) ;
196
187
}
197
188
} ,
198
189
remove : |entity| {
@@ -214,13 +205,16 @@ impl<C: Component + Reflect + FromWorld> FromType<C> for ReflectComponent {
214
205
ticks : c. ticks ,
215
206
} )
216
207
} ,
217
- reflect_unchecked_mut : |world , entity| {
208
+ reflect_unchecked_mut : |entity| {
218
209
// SAFETY: reflect_mut is an unsafe function pointer used by `reflect_unchecked_mut` which promises to never
219
- // produce aliasing mutable references, and reflect_mut, which has mutable world access
210
+ // produce aliasing mutable references, and reflect_mut, which has mutable world access4
211
+ let world = entity. world ( ) ;
212
+ let last_change_tick = world. last_change_tick ( ) ;
213
+ let read_change_tick = world. read_change_tick ( ) ;
214
+
220
215
unsafe {
221
- world
222
- . get_entity ( entity) ?
223
- . get_unchecked_mut :: < C > ( world. last_change_tick ( ) , world. read_change_tick ( ) )
216
+ entity
217
+ . get_unchecked_mut :: < C > ( last_change_tick, read_change_tick)
224
218
. map ( |c| Mut {
225
219
value : c. value as & mut dyn Reflect ,
226
220
ticks : c. ticks ,
0 commit comments