Skip to content

Commit 2382965

Browse files
add get_non_send_resource and get_non_send_resource_mut to InteriorMutableWorld
1 parent 96663da commit 2382965

File tree

1 file changed

+28
-17
lines changed

1 file changed

+28
-17
lines changed

crates/bevy_ecs/src/world/interior_mutable_world.rs

Lines changed: 28 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,17 @@ impl<'w> InteriorMutableWorld<'w> {
143143
self.0.get_resource_by_id(component_id)
144144
}
145145

146+
/// Gets a reference to the non-send resource of the given type if it exists
147+
///
148+
/// # Safety
149+
/// All [`InteriorMutableWorld`] methods take `&self` and thus do not check that there is only one unique reference or multiple shared ones.
150+
/// 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,
151+
/// and that no arbitrary safe code can access a `&World` while some value is mutably borrowed.
152+
#[inline]
153+
pub unsafe fn get_non_send_resource<R: 'static>(&self) -> Option<&'w R> {
154+
self.0.get_non_send_resource()
155+
}
156+
146157
/// Gets a mutable reference to the resource of the given type if it exists
147158
///
148159
/// # Safety
@@ -195,6 +206,23 @@ impl<'w> InteriorMutableWorld<'w> {
195206
ticks,
196207
})
197208
}
209+
210+
/// Gets a mutable reference to the non-send resource of the given type if it exists
211+
///
212+
/// # Safety
213+
/// All [`InteriorMutableWorld`] methods take `&self` and thus do not check that there is only one unique reference or multiple shared ones.
214+
/// 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,
215+
/// and that no arbitrary safe code can access a `&World` while some value is mutably borrowed.
216+
#[inline]
217+
pub unsafe fn get_non_send_resource_mut<R: 'static>(&self) -> Option<Mut<'w, R>> {
218+
self.0.validate_non_send_access::<R>();
219+
let component_id = self.components().get_resource_id(TypeId::of::<R>())?;
220+
// SAFETY:
221+
// - `component_id` is type `R`
222+
// - we have unique access
223+
// - main thread is validated
224+
unsafe { self.get_resource_mut_with_id(component_id) }
225+
}
198226
}
199227

200228
impl<'w> InteriorMutableWorld<'w> {
@@ -226,21 +254,4 @@ impl<'w> InteriorMutableWorld<'w> {
226254
ticks,
227255
})
228256
}
229-
230-
/// # Safety
231-
/// - `component_id` must be assigned to a component of type `R`.
232-
/// - Caller must ensure this doesn't violate Rust mutability rules for the given resource.
233-
///
234-
/// All [`InteriorMutableWorld`] methods take `&self` and thus do not check that there is only one unique reference or multiple shared ones.
235-
/// 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,
236-
/// and that no arbitrary safe code can access a `&World` while some value is mutably borrowed.
237-
#[inline]
238-
pub(crate) unsafe fn get_non_send_mut_with_id<R: 'static>(
239-
&self,
240-
component_id: ComponentId,
241-
) -> Option<Mut<'w, R>> {
242-
self.0.validate_non_send_access::<R>();
243-
// SAFETY: we validated nonsend access, the rest of the requirements are deferred to caller
244-
unsafe { self.get_resource_mut_with_id(component_id) }
245-
}
246257
}

0 commit comments

Comments
 (0)