Skip to content

Commit 59e58ee

Browse files
add common safety section
1 parent 05a3c83 commit 59e58ee

File tree

1 file changed

+60
-0
lines changed

1 file changed

+60
-0
lines changed

crates/bevy_ecs/src/world/interior_mutable_world.rs

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,11 @@ impl<'w> InteriorMutableWorld<'w> {
9696
}
9797

9898
/// Gets a reference to the resource of the given type if it exists
99+
///
100+
/// # Safety
101+
/// All [`InteriorMutableWorld`] methods take `&self` and thus do not check that there is only one unique reference or multiple shared ones.
102+
/// It is the callers responsibility to make sure that there will never be a mutable reference to a value that has other references pointing to it,
103+
/// and that no arbitrary safe code can access a `&World` while some value is mutably borrowed.
99104
#[inline]
100105
pub unsafe fn get_resource<R: Resource>(&self) -> Option<&'w R> {
101106
self.0.get_resource::<R>()
@@ -107,12 +112,22 @@ impl<'w> InteriorMutableWorld<'w> {
107112
///
108113
/// **You should prefer to use the typed API [`InteriorMutableWorld::get_resource`] where possible and only
109114
/// use this in cases where the actual types are not known at compile time.**
115+
///
116+
/// # Safety
117+
/// All [`InteriorMutableWorld`] methods take `&self` and thus do not check that there is only one unique reference or multiple shared ones.
118+
/// It is the callers responsibility to make sure that there will never be a mutable reference to a value that has other references pointing to it,
119+
/// and that no arbitrary safe code can access a `&World` while some value is mutably borrowed.
110120
#[inline]
111121
pub unsafe fn get_resource_by_id(&self, component_id: ComponentId) -> Option<Ptr<'w>> {
112122
self.0.get_resource_by_id(component_id)
113123
}
114124

115125
/// Gets a mutable reference to the resource of the given type if it exists
126+
///
127+
/// # Safety
128+
/// All [`InteriorMutableWorld`] methods take `&self` and thus do not check that there is only one unique reference or multiple shared ones.
129+
/// It is the callers responsibility to make sure that there will never be a mutable reference to a value that has other references pointing to it,
130+
/// and that no arbitrary safe code can access a `&World` while some value is mutably borrowed.
116131
#[inline]
117132
pub unsafe fn get_resource_mut<R: Resource>(&self) -> Option<Mut<'w, R>> {
118133
let component_id = self.0.components.get_resource_id(TypeId::of::<R>())?;
@@ -125,6 +140,11 @@ impl<'w> InteriorMutableWorld<'w> {
125140
///
126141
/// **You should prefer to use the typed API [`InteriorMutableWorld::get_resource_mut`] where possible and only
127142
/// use this in cases where the actual types are not known at compile time.**
143+
///
144+
/// # Safety
145+
/// All [`InteriorMutableWorld`] methods take `&self` and thus do not check that there is only one unique reference or multiple shared ones.
146+
/// It is the callers responsibility to make sure that there will never be a mutable reference to a value that has other references pointing to it,
147+
/// and that no arbitrary safe code can access a `&World` while some value is mutably borrowed.
128148
#[inline]
129149
pub unsafe fn get_resource_mut_by_id(
130150
&self,
@@ -156,6 +176,11 @@ impl<'w> InteriorMutableWorld<'w> {
156176

157177
/// Gets a reference to the non-send resource of the given type, if it exists.
158178
/// Otherwise returns [None]
179+
///
180+
/// # Safety
181+
/// All [`InteriorMutableWorld`] methods take `&self` and thus do not check that there is only one unique reference or multiple shared ones.
182+
/// It is the callers responsibility to make sure that there will never be a mutable reference to a value that has other references pointing to it,
183+
/// and that no arbitrary safe code can access a `&World` while some value is mutably borrowed.
159184
#[inline]
160185
pub unsafe fn get_non_send_resource<R: 'static>(&self) -> Option<&R> {
161186
self.0.get_non_send_resource::<R>()
@@ -175,6 +200,10 @@ impl<'w> InteriorMutableWorld<'w> {
175200
/// - `component_id` must be assigned to a component of type `R`
176201
/// - Caller must ensure this doesn't violate Rust mutability rules for the given resource.
177202
/// - resource is either Send+Sync or the main thread is validated
203+
///
204+
/// All [`InteriorMutableWorld`] methods take `&self` and thus do not check that there is only one unique reference or multiple shared ones.
205+
/// It is the callers responsibility to make sure that there will never be a mutable reference to a value that has other references pointing to it,
206+
/// and that no arbitrary safe code can access a `&World` while some value is mutably borrowed.
178207
#[inline]
179208
pub(crate) unsafe fn get_resource_mut_with_id<R>(
180209
&self,
@@ -196,6 +225,10 @@ impl<'w> InteriorMutableWorld<'w> {
196225
/// # Safety
197226
/// - `component_id` must be assigned to a component of type `R`.
198227
/// - Caller must ensure this doesn't violate Rust mutability rules for the given resource.
228+
///
229+
/// All [`InteriorMutableWorld`] methods take `&self` and thus do not check that there is only one unique reference or multiple shared ones.
230+
/// It is the callers responsibility to make sure that there will never be a mutable reference to a value that has other references pointing to it,
231+
/// and that no arbitrary safe code can access a `&World` while some value is mutably borrowed.
199232
#[inline]
200233
pub(crate) unsafe fn get_non_send_mut_with_id<R: 'static>(
201234
&self,
@@ -252,6 +285,10 @@ impl<'w> InteriorMutableEntityRef<'w> {
252285
entity_ref::contains_component_with_type(self.world.0, type_id, self.location)
253286
}
254287

288+
/// # Safety
289+
/// All [`InteriorMutableEntityRef`] methods take `&self` and thus do not check that there is only one unique reference or multiple shared ones.
290+
/// It is the callers responsibility to make sure that there will never be a mutable reference to a value that has other references pointing to it,
291+
/// and that no arbitrary safe code can access a `&World` while some value is mutably borrowed.
255292
#[inline]
256293
pub unsafe fn get<T: Component>(&self) -> Option<&'w T> {
257294
// SAFETY:
@@ -276,6 +313,11 @@ impl<'w> InteriorMutableEntityRef<'w> {
276313

277314
/// Retrieves the change ticks for the given component. This can be useful for implementing change
278315
/// detection in custom runtimes.
316+
///
317+
/// # Safety
318+
/// All [`InteriorMutableEntityRef`] methods take `&self` and thus do not check that there is only one unique reference or multiple shared ones.
319+
/// It is the callers responsibility to make sure that there will never be a mutable reference to a value that has other references pointing to it,
320+
/// and that no arbitrary safe code can access a `&World` while some value is mutably borrowed.
279321
#[inline]
280322
pub unsafe fn get_change_ticks<T: Component>(&self) -> Option<&'w ComponentTicks> {
281323
// SAFETY:
@@ -298,6 +340,10 @@ impl<'w> InteriorMutableEntityRef<'w> {
298340
}
299341
}
300342

343+
/// # Safety
344+
/// All [`InteriorMutableEntityRef`] methods take `&self` and thus do not check that there is only one unique reference or multiple shared ones.
345+
/// It is the callers responsibility to make sure that there will never be a mutable reference to a value that has other references pointing to it,
346+
/// and that no arbitrary safe code can access a `&World` while some value is mutably borrowed.
301347
#[inline]
302348
pub unsafe fn get_mut<T: Component>(&self) -> Option<Mut<'w, T>> {
303349
// SAFETY: same safety requirements
@@ -309,6 +355,10 @@ impl<'w> InteriorMutableEntityRef<'w> {
309355
}
310356
}
311357

358+
/// # Safety
359+
/// All [`InteriorMutableEntityRef`] methods take `&self` and thus do not check that there is only one unique reference or multiple shared ones.
360+
/// It is the callers responsibility to make sure that there will never be a mutable reference to a value that has other references pointing to it,
361+
/// and that no arbitrary safe code can access a `&World` while some value is mutably borrowed.
312362
#[inline]
313363
pub unsafe fn get_mut_using_ticks<T: Component>(
314364
&self,
@@ -347,6 +397,11 @@ impl<'w> InteriorMutableEntityRef<'w> {
347397
///
348398
/// Unlike [`InteriorMutableEntityRef::get`], this returns a raw pointer to the component,
349399
/// which is only valid while the `'w` borrow of the lifetime is active.
400+
///
401+
/// # Safety
402+
/// All [`InteriorMutableEntityRef`] methods take `&self` and thus do not check that there is only one unique reference or multiple shared ones.
403+
/// It is the callers responsibility to make sure that there will never be a mutable reference to a value that has other references pointing to it,
404+
/// and that no arbitrary safe code can access a `&World` while some value is mutably borrowed.
350405
#[inline]
351406
pub unsafe fn get_by_id(&self, component_id: ComponentId) -> Option<Ptr<'w>> {
352407
self.world.0.components.get_info(component_id)?;
@@ -367,6 +422,11 @@ impl<'w> InteriorMutableEntityRef<'w> {
367422
///
368423
/// **You should prefer to use the typed API [`InteriorMutableEntityRef::get_mut`] where possible and only
369424
/// use this in cases where the actual types are not known at compile time.**
425+
///
426+
/// # Safety
427+
/// All [`InteriorMutableEntityRef`] methods take `&self` and thus do not check that there is only one unique reference or multiple shared ones.
428+
/// It is the callers responsibility to make sure that there will never be a mutable reference to a value that has other references pointing to it,
429+
/// and that no arbitrary safe code can access a `&World` while some value is mutably borrowed.
370430
#[inline]
371431
pub unsafe fn get_mut_by_id(&self, component_id: ComponentId) -> Option<MutUntyped<'w>> {
372432
self.world.0.components.get_info(component_id)?;

0 commit comments

Comments
 (0)