@@ -9,7 +9,7 @@ use crate::{
9
9
Access , FilteredAccess , FilteredAccessSet , QueryState , ReadOnlyWorldQuery , WorldQuery ,
10
10
} ,
11
11
system:: { Query , SystemMeta } ,
12
- world:: { FromWorld , World } ,
12
+ world:: { unsafe_world_cell :: UnsafeWorldCell , FromWorld , World } ,
13
13
} ;
14
14
use bevy_ecs_macros:: impl_param_set;
15
15
pub use bevy_ecs_macros:: Resource ;
@@ -127,14 +127,13 @@ pub unsafe trait SystemParam: Sized {
127
127
128
128
/// # Safety
129
129
///
130
- /// This call might use any of the [`World`] accesses that were registered in [`Self::init_state`].
131
- /// - None of those accesses may conflict with any other [`SystemParam`]s
132
- /// that exist at the same time, including those on other threads.
130
+ /// - The passed [`UnsafeWorldCell`] must have access to any world data
131
+ /// registered in [`init_state`](SystemParam::init_state).
133
132
/// - `world` must be the same `World` that was used to initialize [`state`](SystemParam::init_state).
134
133
unsafe fn get_param < ' world , ' state > (
135
134
state : & ' state mut Self :: State ,
136
135
system_meta : & SystemMeta ,
137
- world : & ' world World ,
136
+ world : UnsafeWorldCell < ' world > ,
138
137
change_tick : Tick ,
139
138
) -> Self :: Item < ' world , ' state > ;
140
139
}
@@ -192,10 +191,19 @@ unsafe impl<Q: WorldQuery + 'static, F: ReadOnlyWorldQuery + 'static> SystemPara
192
191
unsafe fn get_param < ' w , ' s > (
193
192
state : & ' s mut Self :: State ,
194
193
system_meta : & SystemMeta ,
195
- world : & ' w World ,
194
+ world : UnsafeWorldCell < ' w > ,
196
195
change_tick : Tick ,
197
196
) -> Self :: Item < ' w , ' s > {
198
- Query :: new ( world, state, system_meta. last_run , change_tick, false )
197
+ Query :: new (
198
+ // SAFETY: We have registered all of the query's world accesses,
199
+ // so the caller ensures that `world` has permission to access any
200
+ // world data that the query needs.
201
+ world. unsafe_world ( ) ,
202
+ state,
203
+ system_meta. last_run ,
204
+ change_tick,
205
+ false ,
206
+ )
199
207
}
200
208
}
201
209
@@ -329,7 +337,7 @@ fn assert_component_access_compatibility(
329
337
/// ```
330
338
pub struct ParamSet < ' w , ' s , T : SystemParam > {
331
339
param_states : & ' s mut T :: State ,
332
- world : & ' w World ,
340
+ world : UnsafeWorldCell < ' w > ,
333
341
system_meta : SystemMeta ,
334
342
change_tick : Tick ,
335
343
}
@@ -435,11 +443,10 @@ unsafe impl<'a, T: Resource> SystemParam for Res<'a, T> {
435
443
unsafe fn get_param < ' w , ' s > (
436
444
& mut component_id: & ' s mut Self :: State ,
437
445
system_meta : & SystemMeta ,
438
- world : & ' w World ,
446
+ world : UnsafeWorldCell < ' w > ,
439
447
change_tick : Tick ,
440
448
) -> Self :: Item < ' w , ' s > {
441
449
let ( ptr, ticks) = world
442
- . as_unsafe_world_cell_migration_internal ( )
443
450
. get_resource_with_ticks ( component_id)
444
451
. unwrap_or_else ( || {
445
452
panic ! (
@@ -476,11 +483,10 @@ unsafe impl<'a, T: Resource> SystemParam for Option<Res<'a, T>> {
476
483
unsafe fn get_param < ' w , ' s > (
477
484
& mut component_id: & ' s mut Self :: State ,
478
485
system_meta : & SystemMeta ,
479
- world : & ' w World ,
486
+ world : UnsafeWorldCell < ' w > ,
480
487
change_tick : Tick ,
481
488
) -> Self :: Item < ' w , ' s > {
482
489
world
483
- . as_unsafe_world_cell_migration_internal ( )
484
490
. get_resource_with_ticks ( component_id)
485
491
. map ( |( ptr, ticks) | Res {
486
492
value : ptr. deref ( ) ,
@@ -530,11 +536,10 @@ unsafe impl<'a, T: Resource> SystemParam for ResMut<'a, T> {
530
536
unsafe fn get_param < ' w , ' s > (
531
537
& mut component_id: & ' s mut Self :: State ,
532
538
system_meta : & SystemMeta ,
533
- world : & ' w World ,
539
+ world : UnsafeWorldCell < ' w > ,
534
540
change_tick : Tick ,
535
541
) -> Self :: Item < ' w , ' s > {
536
542
let value = world
537
- . as_unsafe_world_cell_migration_internal ( )
538
543
. get_resource_mut_by_id ( component_id)
539
544
. unwrap_or_else ( || {
540
545
panic ! (
@@ -568,11 +573,10 @@ unsafe impl<'a, T: Resource> SystemParam for Option<ResMut<'a, T>> {
568
573
unsafe fn get_param < ' w , ' s > (
569
574
& mut component_id: & ' s mut Self :: State ,
570
575
system_meta : & SystemMeta ,
571
- world : & ' w World ,
576
+ world : UnsafeWorldCell < ' w > ,
572
577
change_tick : Tick ,
573
578
) -> Self :: Item < ' w , ' s > {
574
579
world
575
- . as_unsafe_world_cell_migration_internal ( )
576
580
. get_resource_mut_by_id ( component_id)
577
581
. map ( |value| ResMut {
578
582
value : value. value . deref_mut :: < T > ( ) ,
@@ -621,10 +625,11 @@ unsafe impl SystemParam for &'_ World {
621
625
unsafe fn get_param < ' w , ' s > (
622
626
_state : & ' s mut Self :: State ,
623
627
_system_meta : & SystemMeta ,
624
- world : & ' w World ,
628
+ world : UnsafeWorldCell < ' w > ,
625
629
_change_tick : Tick ,
626
630
) -> Self :: Item < ' w , ' s > {
627
- world
631
+ // SAFETY: Read-only access to the entire world was registerd in `init_state`.
632
+ world. world ( )
628
633
}
629
634
}
630
635
@@ -742,7 +747,7 @@ unsafe impl<'a, T: FromWorld + Send + 'static> SystemParam for Local<'a, T> {
742
747
unsafe fn get_param < ' w , ' s > (
743
748
state : & ' s mut Self :: State ,
744
749
_system_meta : & SystemMeta ,
745
- _world : & ' w World ,
750
+ _world : UnsafeWorldCell < ' w > ,
746
751
_change_tick : Tick ,
747
752
) -> Self :: Item < ' w , ' s > {
748
753
Local ( state. get ( ) )
@@ -916,7 +921,7 @@ unsafe impl<T: SystemBuffer> SystemParam for Deferred<'_, T> {
916
921
unsafe fn get_param < ' w , ' s > (
917
922
state : & ' s mut Self :: State ,
918
923
_system_meta : & SystemMeta ,
919
- _world : & ' w World ,
924
+ _world : UnsafeWorldCell < ' w > ,
920
925
_change_tick : Tick ,
921
926
) -> Self :: Item < ' w , ' s > {
922
927
Deferred ( state. get ( ) )
@@ -1022,11 +1027,10 @@ unsafe impl<'a, T: 'static> SystemParam for NonSend<'a, T> {
1022
1027
unsafe fn get_param < ' w , ' s > (
1023
1028
& mut component_id: & ' s mut Self :: State ,
1024
1029
system_meta : & SystemMeta ,
1025
- world : & ' w World ,
1030
+ world : UnsafeWorldCell < ' w > ,
1026
1031
change_tick : Tick ,
1027
1032
) -> Self :: Item < ' w , ' s > {
1028
1033
let ( ptr, ticks) = world
1029
- . as_unsafe_world_cell_migration_internal ( )
1030
1034
. get_non_send_with_ticks ( component_id)
1031
1035
. unwrap_or_else ( || {
1032
1036
panic ! (
@@ -1061,11 +1065,10 @@ unsafe impl<T: 'static> SystemParam for Option<NonSend<'_, T>> {
1061
1065
unsafe fn get_param < ' w , ' s > (
1062
1066
& mut component_id: & ' s mut Self :: State ,
1063
1067
system_meta : & SystemMeta ,
1064
- world : & ' w World ,
1068
+ world : UnsafeWorldCell < ' w > ,
1065
1069
change_tick : Tick ,
1066
1070
) -> Self :: Item < ' w , ' s > {
1067
1071
world
1068
- . as_unsafe_world_cell_migration_internal ( )
1069
1072
. get_non_send_with_ticks ( component_id)
1070
1073
. map ( |( ptr, ticks) | NonSend {
1071
1074
value : ptr. deref ( ) ,
@@ -1114,11 +1117,10 @@ unsafe impl<'a, T: 'static> SystemParam for NonSendMut<'a, T> {
1114
1117
unsafe fn get_param < ' w , ' s > (
1115
1118
& mut component_id: & ' s mut Self :: State ,
1116
1119
system_meta : & SystemMeta ,
1117
- world : & ' w World ,
1120
+ world : UnsafeWorldCell < ' w > ,
1118
1121
change_tick : Tick ,
1119
1122
) -> Self :: Item < ' w , ' s > {
1120
1123
let ( ptr, ticks) = world
1121
- . as_unsafe_world_cell_migration_internal ( )
1122
1124
. get_non_send_with_ticks ( component_id)
1123
1125
. unwrap_or_else ( || {
1124
1126
panic ! (
@@ -1147,11 +1149,10 @@ unsafe impl<'a, T: 'static> SystemParam for Option<NonSendMut<'a, T>> {
1147
1149
unsafe fn get_param < ' w , ' s > (
1148
1150
& mut component_id: & ' s mut Self :: State ,
1149
1151
system_meta : & SystemMeta ,
1150
- world : & ' w World ,
1152
+ world : UnsafeWorldCell < ' w > ,
1151
1153
change_tick : Tick ,
1152
1154
) -> Self :: Item < ' w , ' s > {
1153
1155
world
1154
- . as_unsafe_world_cell_migration_internal ( )
1155
1156
. get_non_send_with_ticks ( component_id)
1156
1157
. map ( |( ptr, ticks) | NonSendMut {
1157
1158
value : ptr. assert_unique ( ) . deref_mut ( ) ,
@@ -1174,7 +1175,7 @@ unsafe impl<'a> SystemParam for &'a Archetypes {
1174
1175
unsafe fn get_param < ' w , ' s > (
1175
1176
_state : & ' s mut Self :: State ,
1176
1177
_system_meta : & SystemMeta ,
1177
- world : & ' w World ,
1178
+ world : UnsafeWorldCell < ' w > ,
1178
1179
_change_tick : Tick ,
1179
1180
) -> Self :: Item < ' w , ' s > {
1180
1181
world. archetypes ( )
@@ -1195,7 +1196,7 @@ unsafe impl<'a> SystemParam for &'a Components {
1195
1196
unsafe fn get_param < ' w , ' s > (
1196
1197
_state : & ' s mut Self :: State ,
1197
1198
_system_meta : & SystemMeta ,
1198
- world : & ' w World ,
1199
+ world : UnsafeWorldCell < ' w > ,
1199
1200
_change_tick : Tick ,
1200
1201
) -> Self :: Item < ' w , ' s > {
1201
1202
world. components ( )
@@ -1216,7 +1217,7 @@ unsafe impl<'a> SystemParam for &'a Entities {
1216
1217
unsafe fn get_param < ' w , ' s > (
1217
1218
_state : & ' s mut Self :: State ,
1218
1219
_system_meta : & SystemMeta ,
1219
- world : & ' w World ,
1220
+ world : UnsafeWorldCell < ' w > ,
1220
1221
_change_tick : Tick ,
1221
1222
) -> Self :: Item < ' w , ' s > {
1222
1223
world. entities ( )
@@ -1237,7 +1238,7 @@ unsafe impl<'a> SystemParam for &'a Bundles {
1237
1238
unsafe fn get_param < ' w , ' s > (
1238
1239
_state : & ' s mut Self :: State ,
1239
1240
_system_meta : & SystemMeta ,
1240
- world : & ' w World ,
1241
+ world : UnsafeWorldCell < ' w > ,
1241
1242
_change_tick : Tick ,
1242
1243
) -> Self :: Item < ' w , ' s > {
1243
1244
world. bundles ( )
@@ -1286,7 +1287,7 @@ unsafe impl SystemParam for SystemChangeTick {
1286
1287
unsafe fn get_param < ' w , ' s > (
1287
1288
_state : & ' s mut Self :: State ,
1288
1289
system_meta : & SystemMeta ,
1289
- _world : & ' w World ,
1290
+ _world : UnsafeWorldCell < ' w > ,
1290
1291
change_tick : Tick ,
1291
1292
) -> Self :: Item < ' w , ' s > {
1292
1293
SystemChangeTick {
@@ -1356,7 +1357,7 @@ unsafe impl SystemParam for SystemName<'_> {
1356
1357
unsafe fn get_param < ' w , ' s > (
1357
1358
name : & ' s mut Self :: State ,
1358
1359
_system_meta : & SystemMeta ,
1359
- _world : & ' w World ,
1360
+ _world : UnsafeWorldCell < ' w > ,
1360
1361
_change_tick : Tick ,
1361
1362
) -> Self :: Item < ' w , ' s > {
1362
1363
SystemName { name }
@@ -1398,7 +1399,7 @@ macro_rules! impl_system_param_tuple {
1398
1399
unsafe fn get_param<' w, ' s>(
1399
1400
state: & ' s mut Self :: State ,
1400
1401
_system_meta: & SystemMeta ,
1401
- _world: & ' w World ,
1402
+ _world: UnsafeWorldCell < ' w> ,
1402
1403
_change_tick: Tick ,
1403
1404
) -> Self :: Item <' w, ' s> {
1404
1405
@@ -1521,7 +1522,7 @@ unsafe impl<P: SystemParam + 'static> SystemParam for StaticSystemParam<'_, '_,
1521
1522
unsafe fn get_param < ' world , ' state > (
1522
1523
state : & ' state mut Self :: State ,
1523
1524
system_meta : & SystemMeta ,
1524
- world : & ' world World ,
1525
+ world : UnsafeWorldCell < ' world > ,
1525
1526
change_tick : Tick ,
1526
1527
) -> Self :: Item < ' world , ' state > {
1527
1528
// SAFETY: Defer to the safety of P::SystemParam
@@ -1539,7 +1540,7 @@ unsafe impl<T: ?Sized> SystemParam for PhantomData<T> {
1539
1540
unsafe fn get_param < ' world , ' state > (
1540
1541
_state : & ' state mut Self :: State ,
1541
1542
_system_meta : & SystemMeta ,
1542
- _world : & ' world World ,
1543
+ _world : UnsafeWorldCell < ' world > ,
1543
1544
_change_tick : Tick ,
1544
1545
) -> Self :: Item < ' world , ' state > {
1545
1546
PhantomData
0 commit comments