@@ -5,7 +5,10 @@ use crate::{
5
5
component:: Component ,
6
6
entity:: { Entity , EntityMap , MapEntities , MapEntitiesError } ,
7
7
system:: Resource ,
8
- world:: { unsafe_world_cell:: UnsafeWorldCell , EntityMut , EntityRef , FromWorld , World } ,
8
+ world:: {
9
+ unsafe_world_cell:: { UnsafeWorldCell , UnsafeWorldCellEntityRef } ,
10
+ EntityMut , EntityRef , FromWorld , World ,
11
+ } ,
9
12
} ;
10
13
use bevy_reflect:: {
11
14
impl_from_reflect_value, impl_reflect_value, FromType , Reflect , ReflectDeserialize ,
@@ -44,20 +47,23 @@ pub struct ReflectComponentFns {
44
47
/// Function pointer implementing [`ReflectComponent::insert()`].
45
48
pub insert : fn ( & mut World , Entity , & dyn Reflect ) ,
46
49
/// Function pointer implementing [`ReflectComponent::apply()`].
47
- pub apply : fn ( EntityMut , & dyn Reflect ) ,
50
+ pub apply : fn ( & mut EntityMut , & dyn Reflect ) ,
48
51
/// Function pointer implementing [`ReflectComponent::apply_or_insert()`].
49
52
pub apply_or_insert : fn ( & mut World , Entity , & dyn Reflect ) ,
50
53
/// Function pointer implementing [`ReflectComponent::remove()`].
51
- pub remove : fn ( EntityMut ) ,
54
+ pub remove : fn ( & mut EntityMut ) ,
52
55
/// Function pointer implementing [`ReflectComponent::contains()`].
53
- pub contains : fn ( EntityRef ) -> bool ,
56
+ pub contains : fn ( & EntityRef ) -> bool ,
54
57
/// Function pointer implementing [`ReflectComponent::reflect()`].
55
- pub reflect : fn ( EntityRef ) -> Option < & dyn Reflect > ,
58
+ pub reflect : for < ' a > fn ( & ' a EntityRef ) -> Option < & ' a dyn Reflect > ,
56
59
/// Function pointer implementing [`ReflectComponent::reflect_mut()`].
60
+ pub reflect_mut : for <' a > fn ( & ' a mut EntityMut ) -> Option < Mut < ' a , dyn Reflect > > ,
61
+ /// Function pointer implementing [`ReflectComponent::reflect_unchecked_mut()`].
57
62
///
58
63
/// # Safety
59
- /// The function may only be called with an [`UnsafeWorldCell`] that can be used to mutably access the relevant component on the given entity.
60
- pub reflect_mut : unsafe fn ( UnsafeWorldCell < ' _ > , Entity ) -> Option < Mut < ' _ , dyn Reflect > > ,
64
+ /// The function may only be called with an [`UnsafeWorldCellEntityRef`] that can be used to mutably access the relevant component on the given entity.
65
+ pub reflect_unchecked_mut :
66
+ unsafe fn ( UnsafeWorldCellEntityRef < ' _ > ) -> Option < Mut < ' _ , dyn Reflect > > ,
61
67
/// Function pointer implementing [`ReflectComponent::copy()`].
62
68
pub copy : fn ( & World , & mut World , Entity , Entity ) ,
63
69
}
@@ -88,7 +94,7 @@ impl ReflectComponent {
88
94
/// # Panics
89
95
///
90
96
/// Panics if there is no [`Component`] of the given type.
91
- pub fn apply ( & self , entity : EntityMut , component : & dyn Reflect ) {
97
+ pub fn apply ( & self , entity : & mut EntityMut , component : & dyn Reflect ) {
92
98
( self . 0 . apply ) ( entity, component) ;
93
99
}
94
100
@@ -106,42 +112,37 @@ impl ReflectComponent {
106
112
/// # Panics
107
113
///
108
114
/// Panics if there is no [`Component`] of the given type.
109
- pub fn remove ( & self , entity : EntityMut ) {
115
+ pub fn remove ( & self , entity : & mut EntityMut ) {
110
116
( self . 0 . remove ) ( entity) ;
111
117
}
112
118
113
119
/// Returns whether entity contains this [`Component`]
114
- pub fn contains ( & self , entity : EntityRef ) -> bool {
120
+ pub fn contains ( & self , entity : & EntityRef ) -> bool {
115
121
( self . 0 . contains ) ( entity)
116
122
}
117
123
118
124
/// Gets the value of this [`Component`] type from the entity as a reflected reference.
119
- pub fn reflect < ' a > ( & self , entity : EntityRef < ' a > ) -> Option < & ' a dyn Reflect > {
125
+ pub fn reflect < ' a > ( & self , entity : & ' a EntityRef < ' a > ) -> Option < & ' a dyn Reflect > {
120
126
( self . 0 . reflect ) ( entity)
121
127
}
122
128
123
129
/// Gets the value of this [`Component`] type from the entity as a mutable reflected reference.
124
- pub fn reflect_mut < ' a > (
125
- & self ,
126
- world : & ' a mut World ,
127
- entity : Entity ,
128
- ) -> Option < Mut < ' a , dyn Reflect > > {
130
+ pub fn reflect_mut < ' a > ( & self , entity : & ' a mut EntityMut < ' a > ) -> Option < Mut < ' a , dyn Reflect > > {
129
131
// SAFETY: unique world access
130
- unsafe { ( self . 0 . reflect_mut ) ( world . as_unsafe_world_cell ( ) , entity) }
132
+ ( self . 0 . reflect_mut ) ( entity)
131
133
}
132
134
133
135
/// # Safety
134
136
/// This method does not prevent you from having two mutable pointers to the same data,
135
137
/// violating Rust's aliasing rules. To avoid this:
136
- /// * Only call this method with a [`UnsafeWorldCell `] that may be used to mutably access the component on the entity `entity`
138
+ /// * Only call this method with a [`UnsafeWorldCellEntityRef `] that may be used to mutably access the component on the entity `entity`
137
139
/// * Don't call this method more than once in the same scope for a given [`Component`].
138
140
pub unsafe fn reflect_unchecked_mut < ' a > (
139
141
& self ,
140
- world : UnsafeWorldCell < ' a > ,
141
- entity : Entity ,
142
+ entity : UnsafeWorldCellEntityRef < ' a > ,
142
143
) -> Option < Mut < ' a , dyn Reflect > > {
143
144
// SAFETY: safety requirements deferred to caller
144
- ( self . 0 . reflect_mut ) ( world , entity)
145
+ ( self . 0 . reflect_unchecked_mut ) ( entity)
145
146
}
146
147
147
148
/// 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`.
@@ -188,7 +189,7 @@ impl<C: Component + Reflect + FromWorld> FromType<C> for ReflectComponent {
188
189
component. apply ( reflected_component) ;
189
190
world. entity_mut ( entity) . insert ( component) ;
190
191
} ,
191
- apply : |mut entity, reflected_component| {
192
+ apply : |entity, reflected_component| {
192
193
let mut component = entity. get_mut :: < C > ( ) . unwrap ( ) ;
193
194
component. apply ( reflected_component) ;
194
195
} ,
@@ -201,7 +202,7 @@ impl<C: Component + Reflect + FromWorld> FromType<C> for ReflectComponent {
201
202
world. entity_mut ( entity) . insert ( component) ;
202
203
}
203
204
} ,
204
- remove : |mut entity| {
205
+ remove : |entity| {
205
206
entity. remove :: < C > ( ) ;
206
207
} ,
207
208
contains : |entity| entity. contains :: < C > ( ) ,
@@ -214,12 +215,17 @@ impl<C: Component + Reflect + FromWorld> FromType<C> for ReflectComponent {
214
215
. insert ( destination_component) ;
215
216
} ,
216
217
reflect : |entity| entity. get :: < C > ( ) . map ( |c| c as & dyn Reflect ) ,
217
- reflect_mut : |world, entity| {
218
- // SAFETY: reflect_mut is an unsafe function pointer used by
219
- // 1. `reflect_unchecked_mut` which must be called with an UnsafeWorldCell with access to the the component `C` on the `entity`, and
220
- // 2. `reflect_mut`, which has mutable world access
218
+ reflect_mut : |entity| {
219
+ entity. get_mut :: < C > ( ) . map ( |c| Mut {
220
+ value : c. value as & mut dyn Reflect ,
221
+ ticks : c. ticks ,
222
+ } )
223
+ } ,
224
+ reflect_unchecked_mut : |entity| {
225
+ // SAFETY: reflect_unchecked_mut is an unsafe function pointer used by
226
+ // `reflect_unchecked_mut` which must be called with an UnsafeWorldCellEntityReff with access to the the component `C` on the `entity`
221
227
unsafe {
222
- world . get_entity ( entity) ? . get_mut :: < C > ( ) . map ( |c| Mut {
228
+ entity. get_mut :: < C > ( ) . map ( |c| Mut {
223
229
value : c. value as & mut dyn Reflect ,
224
230
ticks : c. ticks ,
225
231
} )
0 commit comments