@@ -62,17 +62,26 @@ impl<'w> EntityRef<'w> {
62
62
}
63
63
64
64
#[ inline]
65
- pub fn get < T : Component > ( & self ) -> Option < & ' w T > {
65
+ pub fn get < T : Component > ( & self ) -> Option < & ' _ T > {
66
+ // SAFE: lifetimes enforce correct usage of returned borrow
67
+ unsafe { self . get_unchecked :: < T > ( ) }
68
+ }
69
+
70
+ /// This allows aliased mutability use after free bugs.
71
+ /// # Safety
72
+ /// - Must not be a mutable reference to the component.
73
+ /// - Must not use the returned reference after the component is removed
74
+ #[ inline]
75
+ pub unsafe fn get_unchecked < T : Component > ( & self ) -> Option < & ' w T > {
66
76
// SAFE: entity location is valid and returned component is of type T
67
- unsafe {
68
- get_component_with_type ( self . world , TypeId :: of :: < T > ( ) , self . entity , self . location )
69
- . map ( |value| & * value. cast :: < T > ( ) )
70
- }
77
+ get_component_with_type ( self . world , TypeId :: of :: < T > ( ) , self . entity , self . location )
78
+ . map ( |value| & * value. cast :: < T > ( ) )
71
79
}
72
80
81
+ /// This allows aliased mutability and use after free bugs.
73
82
/// # Safety
74
- /// This allows aliased mutability. You must make sure this call does not result in multiple
75
- /// mutable references to the same component
83
+ /// - Must not be any other references to the component.
84
+ /// - Must not use the returned reference after the component is removed.
76
85
#[ inline]
77
86
pub unsafe fn get_unchecked_mut < T : Component > (
78
87
& self ,
@@ -145,39 +154,32 @@ impl<'w> EntityMut<'w> {
145
154
}
146
155
147
156
#[ 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
- }
157
+ pub fn get < T : Component > ( & self ) -> Option < & ' _ T > {
158
+ // SAFE: lifetimes enforce correct usage of returned borrow
159
+ unsafe { self . get_unchecked :: < T > ( ) }
154
160
}
155
161
156
162
#[ 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
- }
163
+ pub fn get_mut < T : Component > ( & mut self ) -> Option < Mut < ' _ , T > > {
164
+ // SAFE: world access is unique, and lifetimes enforce correct usage of returned borrow
165
+ unsafe { self . get_unchecked_mut :: < T > ( ) }
166
+ }
167
+
168
+ /// This allows aliased mutability use after free bugs.
169
+ /// # Safety
170
+ /// - Must not be a mutable reference to the component.
171
+ /// - Must not use the returned reference after the component is removed
172
+ #[ inline]
173
+ pub unsafe fn get_unchecked < T : Component > ( & self ) -> Option < & ' w T > {
174
+ // SAFE: entity location is valid and returned component is of type T
175
+ get_component_with_type ( self . world , TypeId :: of :: < T > ( ) , self . entity , self . location )
176
+ . map ( |value| & * value. cast :: < T > ( ) )
176
177
}
177
178
179
+ /// This allows aliased mutability and use after free bugs.
178
180
/// # Safety
179
- /// This allows aliased mutability. You must make sure this call does not result in multiple
180
- /// mutable references to the same component
181
+ /// - Must not be any other references to the component.
182
+ /// - Must not use the returned reference after the component is removed.
181
183
#[ inline]
182
184
pub unsafe fn get_unchecked_mut < T : Component > ( & self ) -> Option < Mut < ' w , T > > {
183
185
get_component_and_ticks_with_type ( self . world , TypeId :: of :: < T > ( ) , self . entity , self . location )
0 commit comments