@@ -75,10 +75,21 @@ impl<'w> EntityRef<'w> {
75
75
76
76
#[ inline]
77
77
pub fn get < T : Component > ( & self ) -> Option < & ' w T > {
78
+ self . get_relation :: < T > ( None )
79
+ }
80
+
81
+ #[ inline]
82
+ pub fn get_relation < T : Component > ( & self , target : Option < Entity > ) -> Option < & ' w T > {
78
83
// SAFE: entity location is valid and returned component is of type T
79
84
unsafe {
80
- get_component_with_type ( self . world , TypeId :: of :: < T > ( ) , self . entity , self . location )
81
- . map ( |value| & * value. cast :: < T > ( ) )
85
+ get_component_with_type (
86
+ self . world ,
87
+ TypeId :: of :: < T > ( ) ,
88
+ target,
89
+ self . entity ,
90
+ self . location ,
91
+ )
92
+ . map ( |value| & * value. cast :: < T > ( ) )
82
93
}
83
94
}
84
95
@@ -91,13 +102,33 @@ impl<'w> EntityRef<'w> {
91
102
last_change_tick : u32 ,
92
103
change_tick : u32 ,
93
104
) -> Option < Mut < ' w , T > > {
94
- get_component_and_ticks_with_type ( self . world , TypeId :: of :: < T > ( ) , self . entity , self . location )
95
- . map ( |( value, ticks) | Mut {
96
- value : & mut * value. cast :: < T > ( ) ,
97
- component_ticks : & mut * ticks,
98
- last_change_tick,
99
- change_tick,
100
- } )
105
+ // SAFETY: Caller
106
+ self . get_relation_unchecked_mut ( None , last_change_tick, change_tick)
107
+ }
108
+
109
+ /// # Safety
110
+ /// This allows aliased mutability. You must make sure this call does not result in multiple
111
+ /// mutable references to the same component
112
+ #[ inline]
113
+ pub unsafe fn get_relation_unchecked_mut < T : Component > (
114
+ & self ,
115
+ target : Option < Entity > ,
116
+ last_change_tick : u32 ,
117
+ change_tick : u32 ,
118
+ ) -> Option < Mut < ' w , T > > {
119
+ get_component_and_ticks_with_type (
120
+ self . world ,
121
+ TypeId :: of :: < T > ( ) ,
122
+ target,
123
+ self . entity ,
124
+ self . location ,
125
+ )
126
+ . map ( |( value, ticks) | Mut {
127
+ value : & mut * value. cast :: < T > ( ) ,
128
+ component_ticks : & mut * ticks,
129
+ last_change_tick,
130
+ change_tick,
131
+ } )
101
132
}
102
133
}
103
134
@@ -165,21 +196,38 @@ impl<'w> EntityMut<'w> {
165
196
166
197
#[ inline]
167
198
pub fn get < T : Component > ( & self ) -> Option < & ' w T > {
199
+ self . get_relation :: < T > ( None )
200
+ }
201
+
202
+ #[ inline]
203
+ pub fn get_relation < T : Component > ( & self , target : Option < Entity > ) -> Option < & ' w T > {
168
204
// SAFE: entity location is valid and returned component is of type T
169
205
unsafe {
170
- get_component_with_type ( self . world , TypeId :: of :: < T > ( ) , self . entity , self . location )
171
- . map ( |value| & * value. cast :: < T > ( ) )
206
+ get_component_with_type (
207
+ self . world ,
208
+ TypeId :: of :: < T > ( ) ,
209
+ target,
210
+ self . entity ,
211
+ self . location ,
212
+ )
213
+ . map ( |value| & * value. cast :: < T > ( ) )
172
214
}
173
215
}
174
216
175
217
#[ inline]
176
218
pub fn get_mut < T : Component > ( & mut self ) -> Option < Mut < ' w , T > > {
219
+ self . get_relation_mut :: < T > ( None )
220
+ }
221
+
222
+ #[ inline]
223
+ pub fn get_relation_mut < T : Component > ( & mut self , target : Option < Entity > ) -> Option < Mut < ' w , T > > {
177
224
// SAFE: world access is unique, entity location is valid, and returned component is of type
178
225
// T
179
226
unsafe {
180
227
get_component_and_ticks_with_type (
181
228
self . world ,
182
229
TypeId :: of :: < T > ( ) ,
230
+ target,
183
231
self . entity ,
184
232
self . location ,
185
233
)
@@ -197,13 +245,30 @@ impl<'w> EntityMut<'w> {
197
245
/// mutable references to the same component
198
246
#[ inline]
199
247
pub unsafe fn get_unchecked_mut < T : Component > ( & self ) -> Option < Mut < ' w , T > > {
200
- get_component_and_ticks_with_type ( self . world , TypeId :: of :: < T > ( ) , self . entity , self . location )
201
- . map ( |( value, ticks) | Mut {
202
- value : & mut * value. cast :: < T > ( ) ,
203
- component_ticks : & mut * ticks,
204
- last_change_tick : self . world . last_change_tick ( ) ,
205
- change_tick : self . world . read_change_tick ( ) ,
206
- } )
248
+ self . get_relation_unchecked_mut :: < T > ( None )
249
+ }
250
+
251
+ /// # Safety
252
+ /// This allows aliased mutability. You must make sure this call does not result in multiple
253
+ /// mutable references to the same component
254
+ #[ inline]
255
+ pub unsafe fn get_relation_unchecked_mut < T : Component > (
256
+ & self ,
257
+ target : Option < Entity > ,
258
+ ) -> Option < Mut < ' w , T > > {
259
+ get_component_and_ticks_with_type (
260
+ self . world ,
261
+ TypeId :: of :: < T > ( ) ,
262
+ target,
263
+ self . entity ,
264
+ self . location ,
265
+ )
266
+ . map ( |( value, ticks) | Mut {
267
+ value : & mut * value. cast :: < T > ( ) ,
268
+ component_ticks : & mut * ticks,
269
+ last_change_tick : self . world . last_change_tick ( ) ,
270
+ change_tick : self . world . read_change_tick ( ) ,
271
+ } )
207
272
}
208
273
209
274
// TODO: factor out non-generic part to cut down on monomorphization (just check perf)
@@ -796,28 +861,32 @@ unsafe fn remove_component(
796
861
}
797
862
}
798
863
864
+ /// Set `target` to None if you just want normal components
799
865
/// # Safety
800
866
/// `entity_location` must be within bounds of an archetype that exists.
801
867
unsafe fn get_component_with_type (
802
868
world : & World ,
803
869
type_id : TypeId ,
870
+ target : Option < Entity > ,
804
871
entity : Entity ,
805
872
location : EntityLocation ,
806
873
) -> Option < * mut u8 > {
807
874
let kind = world. components . get_component_kind ( type_id) ?;
808
- get_component ( world, kind. id ( ) , None , entity, location)
875
+ get_component ( world, kind. id ( ) , target , entity, location)
809
876
}
810
877
878
+ /// Set `target` to None if you just want normal components
811
879
/// # Safety
812
880
/// `entity_location` must be within bounds of an archetype that exists.
813
881
pub ( crate ) unsafe fn get_component_and_ticks_with_type (
814
882
world : & World ,
815
883
type_id : TypeId ,
884
+ target : Option < Entity > ,
816
885
entity : Entity ,
817
886
location : EntityLocation ,
818
887
) -> Option < ( * mut u8 , * mut ComponentTicks ) > {
819
888
let kind_info = world. components . get_component_kind ( type_id) ?;
820
- get_component_and_ticks ( world, kind_info. id ( ) , None , entity, location)
889
+ get_component_and_ticks ( world, kind_info. id ( ) , target , entity, location)
821
890
}
822
891
823
892
fn contains_component_with_type ( world : & World , type_id : TypeId , location : EntityLocation ) -> bool {
0 commit comments