@@ -70,9 +70,16 @@ impl<'w> EntityRef<'w> {
70
70
}
71
71
}
72
72
73
+ /// Gets a mutable reference to the component of type `T` associated with
74
+ /// this entity without ensuring there are no other borrows active and without
75
+ /// ensuring that the returned reference will stay valid.
76
+ ///
73
77
/// # Safety
74
- /// This allows aliased mutability. You must make sure this call does not result in multiple
75
- /// mutable references to the same component
78
+ ///
79
+ /// - The returned reference must never alias a mutable borrow of this component.
80
+ /// - The returned reference must not be used after this component is moved which
81
+ /// may happen from **any** `insert_component`, `remove_component` or `despawn`
82
+ /// operation on this world (non-exhaustive list).
76
83
#[ inline]
77
84
pub unsafe fn get_unchecked_mut < T : Component > (
78
85
& self ,
@@ -145,39 +152,44 @@ impl<'w> EntityMut<'w> {
145
152
}
146
153
147
154
#[ inline]
148
- pub fn get < T : Component > ( & self ) -> Option < & ' w T > {
149
- // SAFE: entity location is valid and returned component is of type T
150
- unsafe {
151
- get_component_with_type ( self . world , TypeId :: of :: < T > ( ) , self . entity , self . location )
152
- . map ( |value| & * value. cast :: < T > ( ) )
153
- }
155
+ pub fn get < T : Component > ( & self ) -> Option < & ' _ T > {
156
+ // SAFE: lifetimes enforce correct usage of returned borrow
157
+ unsafe { self . get_unchecked :: < T > ( ) }
154
158
}
155
159
156
160
#[ inline]
157
- pub fn get_mut < T : Component > ( & mut self ) -> Option < Mut < ' w , T > > {
158
- // SAFE: world access is unique, entity location is valid, and returned component is of type
159
- // T
160
- unsafe {
161
- get_component_and_ticks_with_type (
162
- self . world ,
163
- TypeId :: of :: < T > ( ) ,
164
- self . entity ,
165
- self . location ,
166
- )
167
- . map ( |( value, ticks) | Mut {
168
- value : & mut * value. cast :: < T > ( ) ,
169
- ticks : Ticks {
170
- component_ticks : & mut * ticks,
171
- last_change_tick : self . world . last_change_tick ( ) ,
172
- change_tick : self . world . change_tick ( ) ,
173
- } ,
174
- } )
175
- }
161
+ pub fn get_mut < T : Component > ( & mut self ) -> Option < Mut < ' _ , T > > {
162
+ // SAFE: world access is unique, and lifetimes enforce correct usage of returned borrow
163
+ unsafe { self . get_unchecked_mut :: < T > ( ) }
176
164
}
177
165
166
+ /// Gets an immutable reference to the component of type `T` associated with
167
+ /// this entity without ensuring there are no unique borrows active and without
168
+ /// ensuring that the returned reference will stay valid.
169
+ ///
178
170
/// # Safety
179
- /// This allows aliased mutability. You must make sure this call does not result in multiple
180
- /// mutable references to the same component
171
+ ///
172
+ /// - The returned reference must never alias a mutable borrow of this component.
173
+ /// - The returned reference must not be used after this component is moved which
174
+ /// may happen from **any** `insert_component`, `remove_component` or `despawn`
175
+ /// operation on this world (non-exhaustive list).
176
+ #[ inline]
177
+ pub unsafe fn get_unchecked < T : Component > ( & self ) -> Option < & ' w T > {
178
+ // SAFE: entity location is valid and returned component is of type T
179
+ get_component_with_type ( self . world , TypeId :: of :: < T > ( ) , self . entity , self . location )
180
+ . map ( |value| & * value. cast :: < T > ( ) )
181
+ }
182
+
183
+ /// Gets a mutable reference to the component of type `T` associated with
184
+ /// this entity without ensuring there are no other borrows active and without
185
+ /// ensuring that the returned reference will stay valid.
186
+ ///
187
+ /// # Safety
188
+ ///
189
+ /// - The returned reference must never alias a mutable borrow of this component.
190
+ /// - The returned reference must not be used after this component is moved which
191
+ /// may happen from **any** `insert_component`, `remove_component` or `despawn`
192
+ /// operation on this world (non-exhaustive list).
181
193
#[ inline]
182
194
pub unsafe fn get_unchecked_mut < T : Component > ( & self ) -> Option < Mut < ' w , T > > {
183
195
get_component_and_ticks_with_type ( self . world , TypeId :: of :: < T > ( ) , self . entity , self . location )
0 commit comments