@@ -292,7 +292,13 @@ use std::{cell::UnsafeCell, marker::PhantomData};
292
292
///
293
293
/// # bevy_ecs::system::assert_is_system(my_system);
294
294
/// ```
295
- pub trait WorldQuery : for < ' w > WorldQueryGats < ' w , _State = Self :: State > {
295
+ /// # Safety
296
+ ///
297
+ /// Implementor must ensure that [`WorldQuery::update_component_access`] and
298
+ /// [`WorldQuery::update_archetype_component_access`] exactly reflects the results of
299
+ /// [`FetchState::matches_archetype`], [`FetchState::matches_table`], [`Fetch::archetype_fetch`], and
300
+ /// [`Fetch::table_fetch`].
301
+ pub unsafe trait WorldQuery : for < ' w > WorldQueryGats < ' w , _State = Self :: State > {
296
302
type ReadOnly : ReadOnlyWorldQuery < State = Self :: State > ;
297
303
type State : FetchState ;
298
304
@@ -429,20 +435,14 @@ pub trait Fetch<'world>: Sized {
429
435
/// State used to construct a Fetch. This will be cached inside [`QueryState`](crate::query::QueryState),
430
436
/// so it is best to move as much data / computation here as possible to reduce the cost of
431
437
/// constructing Fetch.
432
- ///
433
- /// # Safety
434
- ///
435
- /// Implementor must ensure that [`FetchState::update_component_access`] and
436
- /// [`FetchState::update_archetype_component_access`] exactly reflects the results of
437
- /// [`FetchState::matches_archetype`], [`FetchState::matches_table`], [`Fetch::archetype_fetch`], and
438
- /// [`Fetch::table_fetch`].
439
- pub unsafe trait FetchState : Send + Sync + Sized {
438
+ pub trait FetchState : Send + Sync + Sized {
440
439
fn init ( world : & mut World ) -> Self ;
441
440
fn matches_archetype ( & self , archetype : & Archetype ) -> bool ;
442
441
fn matches_table ( & self , table : & Table ) -> bool ;
443
442
}
444
443
445
- impl WorldQuery for Entity {
444
+ /// SAFETY: no component or archetype access
445
+ unsafe impl WorldQuery for Entity {
446
446
type ReadOnly = Self ;
447
447
type State = EntityState ;
448
448
@@ -474,8 +474,7 @@ unsafe impl ReadOnlyWorldQuery for Entity {}
474
474
#[ doc( hidden) ]
475
475
pub struct EntityState ;
476
476
477
- // SAFETY: no component or archetype access
478
- unsafe impl FetchState for EntityState {
477
+ impl FetchState for EntityState {
479
478
fn init ( _world : & mut World ) -> Self {
480
479
Self
481
480
}
@@ -541,7 +540,9 @@ impl<'w> Fetch<'w> for EntityFetch<'w> {
541
540
}
542
541
}
543
542
544
- impl < T : Component > WorldQuery for & T {
543
+ // SAFETY: component access and archetype component access are properly updated to reflect that T is
544
+ // read
545
+ unsafe impl < T : Component > WorldQuery for & T {
545
546
type ReadOnly = Self ;
546
547
type State = ReadState < T > ;
547
548
@@ -578,9 +579,7 @@ pub struct ReadState<T> {
578
579
marker : PhantomData < T > ,
579
580
}
580
581
581
- // SAFETY: component access and archetype component access are properly updated to reflect that T is
582
- // read
583
- unsafe impl < T : Component > FetchState for ReadState < T > {
582
+ impl < T : Component > FetchState for ReadState < T > {
584
583
fn init ( world : & mut World ) -> Self {
585
584
let component_id = world. init_component :: < T > ( ) ;
586
585
ReadState {
@@ -725,7 +724,9 @@ impl<'w, T: Component> Fetch<'w> for ReadFetch<'w, T> {
725
724
}
726
725
}
727
726
728
- impl < ' w , T : Component > WorldQuery for & ' w mut T {
727
+ /// SAFETY: // SAFETYcomponent access and archetype component access are properly updated to reflect that T is
728
+ /// read and write
729
+ unsafe impl < ' w , T : Component > WorldQuery for & ' w mut T {
729
730
type ReadOnly = & ' w T ;
730
731
type State = ReadState < T > ;
731
732
@@ -908,7 +909,9 @@ impl<'w, 's, T: Component> Fetch<'w> for WriteFetch<'w, T> {
908
909
}
909
910
}
910
911
911
- impl < T : WorldQuery > WorldQuery for Option < T > {
912
+ // SAFETY: component access and archetype component access are properly updated according to the
913
+ // internal Fetch
914
+ unsafe impl < T : WorldQuery > WorldQuery for Option < T > {
912
915
type ReadOnly = Option < T :: ReadOnly > ;
913
916
type State = OptionState < T :: State > ;
914
917
@@ -948,9 +951,7 @@ pub struct OptionState<T: FetchState> {
948
951
state : T ,
949
952
}
950
953
951
- // SAFETY: component access and archetype component access are properly updated according to the
952
- // internal Fetch
953
- unsafe impl < T : FetchState > FetchState for OptionState < T > {
954
+ impl < T : FetchState > FetchState for OptionState < T > {
954
955
fn init ( world : & mut World ) -> Self {
955
956
Self {
956
957
state : T :: init ( world) ,
@@ -1095,7 +1096,9 @@ impl<T: Component> ChangeTrackers<T> {
1095
1096
}
1096
1097
}
1097
1098
1098
- impl < T : Component > WorldQuery for ChangeTrackers < T > {
1099
+ // SAFETY: component access and archetype component access are properly updated to reflect that T is
1100
+ // read
1101
+ unsafe impl < T : Component > WorldQuery for ChangeTrackers < T > {
1099
1102
type ReadOnly = Self ;
1100
1103
type State = ChangeTrackersState < T > ;
1101
1104
@@ -1132,9 +1135,7 @@ pub struct ChangeTrackersState<T> {
1132
1135
marker : PhantomData < T > ,
1133
1136
}
1134
1137
1135
- // SAFETY: component access and archetype component access are properly updated to reflect that T is
1136
- // read
1137
- unsafe impl < T : Component > FetchState for ChangeTrackersState < T > {
1138
+ impl < T : Component > FetchState for ChangeTrackersState < T > {
1138
1139
fn init ( world : & mut World ) -> Self {
1139
1140
let component_id = world. init_component :: < T > ( ) ;
1140
1141
Self {
@@ -1377,10 +1378,9 @@ macro_rules! impl_tuple_fetch {
1377
1378
}
1378
1379
}
1379
1380
1380
- // SAFETY: update_component_access and update_archetype_component_access are called for each item in the tuple
1381
1381
#[ allow( non_snake_case) ]
1382
1382
#[ allow( clippy:: unused_unit) ]
1383
- unsafe impl <$( $name: FetchState ) ,* > FetchState for ( $( $name, ) * ) {
1383
+ impl <$( $name: FetchState ) ,* > FetchState for ( $( $name, ) * ) {
1384
1384
fn init( _world: & mut World ) -> Self {
1385
1385
( $( $name:: init( _world) , ) * )
1386
1386
}
@@ -1398,7 +1398,8 @@ macro_rules! impl_tuple_fetch {
1398
1398
1399
1399
#[ allow( non_snake_case) ]
1400
1400
#[ allow( clippy:: unused_unit) ]
1401
- impl <$( $name: WorldQuery ) ,* > WorldQuery for ( $( $name, ) * ) {
1401
+ // SAFETY: update_component_access and update_archetype_component_access are called for each item in the tuple
1402
+ unsafe impl <$( $name: WorldQuery ) ,* > WorldQuery for ( $( $name, ) * ) {
1402
1403
type ReadOnly = ( $( $name:: ReadOnly , ) * ) ;
1403
1404
type State = ( $( $name:: State , ) * ) ;
1404
1405
@@ -1500,10 +1501,9 @@ macro_rules! impl_anytuple_fetch {
1500
1501
}
1501
1502
}
1502
1503
1503
- // SAFETY: update_component_access and update_archetype_component_access are called for each item in the tuple
1504
1504
#[ allow( non_snake_case) ]
1505
1505
#[ allow( clippy:: unused_unit) ]
1506
- unsafe impl <$( $name: FetchState ) ,* > FetchState for AnyOf <( $( $name, ) * ) > {
1506
+ impl <$( $name: FetchState ) ,* > FetchState for AnyOf <( $( $name, ) * ) > {
1507
1507
fn init( _world: & mut World ) -> Self {
1508
1508
AnyOf ( ( $( $name:: init( _world) , ) * ) )
1509
1509
}
@@ -1521,7 +1521,8 @@ macro_rules! impl_anytuple_fetch {
1521
1521
1522
1522
#[ allow( non_snake_case) ]
1523
1523
#[ allow( clippy:: unused_unit) ]
1524
- impl <$( $name: WorldQuery ) ,* > WorldQuery for AnyOf <( $( $name, ) * ) > {
1524
+ // SAFETY: update_component_access and update_archetype_component_access are called for each item in the tuple
1525
+ unsafe impl <$( $name: WorldQuery ) ,* > WorldQuery for AnyOf <( $( $name, ) * ) > {
1525
1526
type ReadOnly = AnyOf <( $( $name:: ReadOnly , ) * ) >;
1526
1527
type State = AnyOf <( $( $name:: State , ) * ) >;
1527
1528
0 commit comments