@@ -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 , 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 ,
@@ -42,20 +45,25 @@ pub struct ReflectComponent(ReflectComponentFns);
42
45
#[ derive( Clone ) ]
43
46
pub struct ReflectComponentFns {
44
47
/// Function pointer implementing [`ReflectComponent::insert()`].
45
- pub insert : fn ( & mut World , Entity , & dyn Reflect ) ,
48
+ pub insert : fn ( & mut EntityMut , & dyn Reflect ) ,
46
49
/// Function pointer implementing [`ReflectComponent::apply()`].
47
- pub apply : fn ( & mut World , Entity , & dyn Reflect ) ,
50
+ pub apply : fn ( & mut EntityMut , & dyn Reflect ) ,
48
51
/// Function pointer implementing [`ReflectComponent::apply_or_insert()`].
49
- pub apply_or_insert : fn ( & mut World , Entity , & dyn Reflect ) ,
52
+ pub apply_or_insert : fn ( & mut EntityMut , & dyn Reflect ) ,
50
53
/// Function pointer implementing [`ReflectComponent::remove()`].
51
- pub remove : fn ( & mut World , Entity ) ,
54
+ pub remove : fn ( & mut EntityMut ) ,
55
+ /// Function pointer implementing [`ReflectComponent::contains()`].
56
+ pub contains : fn ( EntityRef ) -> bool ,
52
57
/// Function pointer implementing [`ReflectComponent::reflect()`].
53
- pub reflect : fn ( & World , Entity ) -> Option < & dyn Reflect > ,
58
+ pub reflect : fn ( EntityRef ) -> Option < & dyn Reflect > ,
54
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()`].
55
62
///
56
63
/// # Safety
57
- /// The function may only be called with an [`UnsafeWorldCell`] that can be used to mutably access the relevant component on the given entity.
58
- 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 > > ,
59
67
/// Function pointer implementing [`ReflectComponent::copy()`].
60
68
pub copy : fn ( & World , & mut World , Entity , Entity ) ,
61
69
}
@@ -73,68 +81,59 @@ impl ReflectComponentFns {
73
81
74
82
impl ReflectComponent {
75
83
/// Insert a reflected [`Component`] into the entity like [`insert()`](crate::world::EntityMut::insert).
76
- ///
77
- /// # Panics
78
- ///
79
- /// Panics if there is no such entity.
80
- pub fn insert ( & self , world : & mut World , entity : Entity , component : & dyn Reflect ) {
81
- ( self . 0 . insert ) ( world, entity, component) ;
84
+ pub fn insert ( & self , entity : & mut EntityMut , component : & dyn Reflect ) {
85
+ ( self . 0 . insert ) ( entity, component) ;
82
86
}
83
87
84
88
/// Uses reflection to set the value of this [`Component`] type in the entity to the given value.
85
89
///
86
90
/// # Panics
87
91
///
88
- /// Panics if there is no [`Component`] of the given type or the `entity` does not exist .
89
- pub fn apply ( & self , world : & mut World , entity : Entity , component : & dyn Reflect ) {
90
- ( self . 0 . apply ) ( world , entity, component) ;
92
+ /// Panics if there is no [`Component`] of the given type.
93
+ pub fn apply ( & self , entity : & mut EntityMut , component : & dyn Reflect ) {
94
+ ( self . 0 . apply ) ( entity, component) ;
91
95
}
92
96
93
97
/// 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.
94
- ///
95
- /// # Panics
96
- ///
97
- /// Panics if the `entity` does not exist.
98
- pub fn apply_or_insert ( & self , world : & mut World , entity : Entity , component : & dyn Reflect ) {
99
- ( self . 0 . apply_or_insert ) ( world, entity, component) ;
98
+ pub fn apply_or_insert ( & self , entity : & mut EntityMut , component : & dyn Reflect ) {
99
+ ( self . 0 . apply_or_insert ) ( entity, component) ;
100
100
}
101
101
102
102
/// Removes this [`Component`] type from the entity. Does nothing if it doesn't exist.
103
103
///
104
104
/// # Panics
105
105
///
106
- /// Panics if there is no [`Component`] of the given type or the `entity` does not exist.
107
- pub fn remove ( & self , world : & mut World , entity : Entity ) {
108
- ( self . 0 . remove ) ( world, entity) ;
106
+ /// Panics if there is no [`Component`] of the given type.
107
+ pub fn remove ( & self , entity : & mut EntityMut ) {
108
+ ( self . 0 . remove ) ( entity) ;
109
+ }
110
+
111
+ /// Returns whether entity contains this [`Component`]
112
+ pub fn contains ( & self , entity : EntityRef ) -> bool {
113
+ ( self . 0 . contains ) ( entity)
109
114
}
110
115
111
116
/// Gets the value of this [`Component`] type from the entity as a reflected reference.
112
- pub fn reflect < ' a > ( & self , world : & ' a World , entity : Entity ) -> Option < & ' a dyn Reflect > {
113
- ( self . 0 . reflect ) ( world , entity)
117
+ pub fn reflect < ' a > ( & self , entity : EntityRef < ' a > ) -> Option < & ' a dyn Reflect > {
118
+ ( self . 0 . reflect ) ( entity)
114
119
}
115
120
116
121
/// Gets the value of this [`Component`] type from the entity as a mutable reflected reference.
117
- pub fn reflect_mut < ' a > (
118
- & self ,
119
- world : & ' a mut World ,
120
- entity : Entity ,
121
- ) -> Option < Mut < ' a , dyn Reflect > > {
122
- // SAFETY: unique world access
123
- unsafe { ( self . 0 . reflect_mut ) ( world. as_unsafe_world_cell ( ) , entity) }
122
+ pub fn reflect_mut < ' a > ( & self , entity : & ' a mut EntityMut < ' _ > ) -> Option < Mut < ' a , dyn Reflect > > {
123
+ ( self . 0 . reflect_mut ) ( entity)
124
124
}
125
125
126
126
/// # Safety
127
127
/// This method does not prevent you from having two mutable pointers to the same data,
128
128
/// violating Rust's aliasing rules. To avoid this:
129
- /// * Only call this method with a [`UnsafeWorldCell `] that may be used to mutably access the component on the entity `entity`
129
+ /// * Only call this method with a [`UnsafeWorldCellEntityRef `] that may be used to mutably access the component on the entity `entity`
130
130
/// * Don't call this method more than once in the same scope for a given [`Component`].
131
131
pub unsafe fn reflect_unchecked_mut < ' a > (
132
132
& self ,
133
- world : UnsafeWorldCell < ' a > ,
134
- entity : Entity ,
133
+ entity : UnsafeWorldCellEntityRef < ' a > ,
135
134
) -> Option < Mut < ' a , dyn Reflect > > {
136
135
// SAFETY: safety requirements deferred to caller
137
- ( self . 0 . reflect_mut ) ( world , entity)
136
+ ( self . 0 . reflect_unchecked_mut ) ( entity)
138
137
}
139
138
140
139
/// 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`.
@@ -176,27 +175,28 @@ impl ReflectComponent {
176
175
impl < C : Component + Reflect + FromWorld > FromType < C > for ReflectComponent {
177
176
fn from_type ( ) -> Self {
178
177
ReflectComponent ( ReflectComponentFns {
179
- insert : |world , entity, reflected_component| {
180
- let mut component = C :: from_world ( world) ;
178
+ insert : |entity, reflected_component| {
179
+ let mut component = entity . world_scope ( |world| C :: from_world ( world) ) ;
181
180
component. apply ( reflected_component) ;
182
- world . entity_mut ( entity) . insert ( component) ;
181
+ entity. insert ( component) ;
183
182
} ,
184
- apply : |world , entity, reflected_component| {
185
- let mut component = world . get_mut :: < C > ( entity ) . unwrap ( ) ;
183
+ apply : |entity, reflected_component| {
184
+ let mut component = entity . get_mut :: < C > ( ) . unwrap ( ) ;
186
185
component. apply ( reflected_component) ;
187
186
} ,
188
- apply_or_insert : |world , entity, reflected_component| {
189
- if let Some ( mut component) = world . get_mut :: < C > ( entity ) {
187
+ apply_or_insert : |entity, reflected_component| {
188
+ if let Some ( mut component) = entity . get_mut :: < C > ( ) {
190
189
component. apply ( reflected_component) ;
191
190
} else {
192
- let mut component = C :: from_world ( world) ;
191
+ let mut component = entity . world_scope ( |world| C :: from_world ( world) ) ;
193
192
component. apply ( reflected_component) ;
194
- world . entity_mut ( entity) . insert ( component) ;
193
+ entity. insert ( component) ;
195
194
}
196
195
} ,
197
- remove : |world , entity| {
198
- world . entity_mut ( entity) . remove :: < C > ( ) ;
196
+ remove : |entity| {
197
+ entity. remove :: < C > ( ) ;
199
198
} ,
199
+ contains : |entity| entity. contains :: < C > ( ) ,
200
200
copy : |source_world, destination_world, source_entity, destination_entity| {
201
201
let source_component = source_world. get :: < C > ( source_entity) . unwrap ( ) ;
202
202
let mut destination_component = C :: from_world ( destination_world) ;
@@ -205,18 +205,18 @@ impl<C: Component + Reflect + FromWorld> FromType<C> for ReflectComponent {
205
205
. entity_mut ( destination_entity)
206
206
. insert ( destination_component) ;
207
207
} ,
208
- reflect : |world, entity| {
209
- world
210
- . get_entity ( entity) ?
211
- . get :: < C > ( )
212
- . map ( |c| c as & dyn Reflect )
208
+ reflect : |entity| entity. get :: < C > ( ) . map ( |c| c as & dyn Reflect ) ,
209
+ reflect_mut : |entity| {
210
+ entity. get_mut :: < C > ( ) . map ( |c| Mut {
211
+ value : c. value as & mut dyn Reflect ,
212
+ ticks : c. ticks ,
213
+ } )
213
214
} ,
214
- reflect_mut : |world, entity| {
215
- // SAFETY: reflect_mut is an unsafe function pointer used by
216
- // 1. `reflect_unchecked_mut` which must be called with an UnsafeWorldCell with access to the the component `C` on the `entity`, and
217
- // 2. `reflect_mut`, which has mutable world access
215
+ reflect_unchecked_mut : |entity| {
216
+ // SAFETY: reflect_unchecked_mut is an unsafe function pointer used by
217
+ // `reflect_unchecked_mut` which must be called with an UnsafeWorldCellEntityRef with access to the the component `C` on the `entity`
218
218
unsafe {
219
- world . get_entity ( entity) ? . get_mut :: < C > ( ) . map ( |c| Mut {
219
+ entity. get_mut :: < C > ( ) . map ( |c| Mut {
220
220
value : c. value as & mut dyn Reflect ,
221
221
ticks : c. ticks ,
222
222
} )
0 commit comments