Skip to content

Commit 5d5a504

Browse files
committed
Revise SystemParam docs (#7274)
# Objective Increase clarity in a few places for the `SystemParam` docs.
1 parent f024bce commit 5d5a504

File tree

2 files changed

+13
-5
lines changed

2 files changed

+13
-5
lines changed

crates/bevy_ecs/src/system/system_param.rs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -124,8 +124,11 @@ use std::{
124124
///
125125
/// # Safety
126126
///
127-
/// The implementor must ensure that [`SystemParam::init_state`] correctly registers all
128-
/// [`World`] accesses used by this [`SystemParam`] with the provided [`system_meta`](SystemMeta).
127+
/// The implementor must ensure the following is true.
128+
/// - [`SystemParam::init_state`] correctly registers all [`World`] accesses used
129+
/// by [`SystemParam::get_param`] with the provided [`system_meta`](SystemMeta).
130+
/// - None of the world accesses may conflict with any prior accesses registered
131+
/// on `system_meta`.
129132
pub unsafe trait SystemParam: Sized {
130133
/// Used to store data which persists across invocations of a system.
131134
type State: Send + Sync + 'static;
@@ -158,7 +161,9 @@ pub unsafe trait SystemParam: Sized {
158161
/// # Safety
159162
///
160163
/// This call might use any of the [`World`] accesses that were registered in [`Self::init_state`].
161-
/// You must ensure that none of those accesses conflict with any other [`SystemParam`]s running in parallel with this one.
164+
/// - None of those accesses may conflict with any other [`SystemParam`]s
165+
/// that exist at the same time, including those on other threads.
166+
/// - `world` must be the same `World` that was used to initialize [`state`](SystemParam::init_state).
162167
unsafe fn get_param<'world, 'state>(
163168
state: &'state mut Self::State,
164169
system_meta: &SystemMeta,
@@ -589,7 +594,7 @@ unsafe impl<'a, T: Resource> SystemParam for Option<ResMut<'a, T>> {
589594
// SAFETY: Commands only accesses internal state
590595
unsafe impl<'w, 's> ReadOnlySystemParam for Commands<'w, 's> {}
591596

592-
// SAFETY: only local state is accessed
597+
// SAFETY: `Commands::get_param` does not access the world.
593598
unsafe impl SystemParam for Commands<'_, '_> {
594599
type State = CommandQueue;
595600
type Item<'w, 's> = Commands<'w, 's>;
@@ -1305,7 +1310,6 @@ unsafe impl<'s> ReadOnlySystemParam for SystemName<'s> {}
13051310

13061311
macro_rules! impl_system_param_tuple {
13071312
($($param: ident),*) => {
1308-
13091313
// SAFETY: tuple consists only of ReadOnlySystemParams
13101314
unsafe impl<$($param: ReadOnlySystemParam),*> ReadOnlySystemParam for ($($param,)*) {}
13111315

crates/bevy_render/src/extract_param.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ pub struct ExtractState<P: SystemParam + 'static> {
5656
unsafe impl<P> ReadOnlySystemParam for Extract<'_, '_, P> where P: ReadOnlySystemParam {}
5757

5858
// SAFETY: The only `World` access is properly registered by `Res<MainWorld>::init_state`.
59+
// This call will also ensure that there are no conflicts with prior params.
5960
unsafe impl<P> SystemParam for Extract<'_, '_, P>
6061
where
6162
P: ReadOnlySystemParam,
@@ -77,6 +78,9 @@ where
7778
world: &'w World,
7879
change_tick: u32,
7980
) -> Self::Item<'w, 's> {
81+
// SAFETY:
82+
// - The caller ensures that `world` is the same one that `init_state` was called with.
83+
// - The caller ensures that no other `SystemParam`s will conflict with the accesses we have registered.
8084
let main_world = Res::<MainWorld>::get_param(
8185
&mut state.main_world_state,
8286
system_meta,

0 commit comments

Comments
 (0)