Skip to content

Commit

Permalink
move shrink back to world_query
Browse files Browse the repository at this point in the history
  • Loading branch information
wainwrightmark committed Oct 20, 2023
1 parent 4feac78 commit 1e2f825
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 79 deletions.
10 changes: 10 additions & 0 deletions crates/bevy_ecs/macros/src/world_query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,16 @@ pub(crate) fn world_query_impl(
type Fetch<'__w> = #fetch_struct_name #user_ty_generics_with_world;
type State = #state_struct_name #user_ty_generics;

fn shrink<'__wlong: '__wshort, '__wshort>(
item: <#struct_name #user_ty_generics as #path::query::WorldQuery>::Item<'__wlong>
) -> <#struct_name #user_ty_generics as #path::query::WorldQuery>::Item<'__wshort> {
#item_struct_name {
#(
#field_idents: <#field_types>::shrink(item.#field_idents),
)*
}
}

unsafe fn init_fetch<'__w>(
_world: #path::world::unsafe_world_cell::UnsafeWorldCell<'__w>,
state: &Self::State,
Expand Down
22 changes: 0 additions & 22 deletions crates/bevy_ecs/macros/src/world_query_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -277,19 +277,7 @@ pub fn derive_world_query_data_impl(input: TokenStream) -> TokenStream {
unsafe impl #user_impl_generics #path::query::WorldQueryData
for #read_only_struct_name #user_ty_generics #user_where_clauses {
type ReadOnly = #read_only_struct_name #user_ty_generics;

fn shrink<'__wlong: '__wshort, '__wshort>(
item: <#read_only_struct_name #user_ty_generics as #path::query::WorldQuery>::Item<'__wlong>
) -> <#read_only_struct_name #user_ty_generics as #path::query::WorldQuery>::Item<'__wshort> {
#read_only_item_struct_name {
#(
#field_idents: <#read_only_field_types>::shrink(item.#field_idents),
)*
}
}
}


}
} else {
quote! {}
Expand All @@ -300,16 +288,6 @@ pub fn derive_world_query_data_impl(input: TokenStream) -> TokenStream {
unsafe impl #user_impl_generics #path::query::WorldQueryData
for #struct_name #user_ty_generics #user_where_clauses {
type ReadOnly = #read_only_struct_name #user_ty_generics;

fn shrink<'__wlong: '__wshort, '__wshort>(
item: <#struct_name #user_ty_generics as #path::query::WorldQuery>::Item<'__wlong>
) -> <#struct_name #user_ty_generics as #path::query::WorldQuery>::Item<'__wshort> {
#item_struct_name {
#(
#field_idents: <#field_types>::shrink(item.#field_idents),
)*
}
}
}

#read_only_data_impl
Expand Down
98 changes: 43 additions & 55 deletions crates/bevy_ecs/src/query/fetch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -260,9 +260,6 @@ use std::{cell::UnsafeCell, marker::PhantomData};
pub unsafe trait WorldQueryData: WorldQuery {
/// The read-only variant of this [`WorldQueryData`], which satisfies the [`ReadOnlyWorldQueryData`] trait.
type ReadOnly: ReadOnlyWorldQueryData<State = <Self as WorldQuery>::State>;

/// This function manually implements subtyping for the query items.
fn shrink<'wlong: 'wshort, 'wshort>(item: Self::Item<'wlong>) -> Self::Item<'wshort>;
}

/// A [`WorldQueryData`] that is read only.
Expand All @@ -285,6 +282,10 @@ unsafe impl WorldQuery for Entity {
type Item<'w> = Entity;
type State = ();

fn shrink<'wlong: 'wshort, 'wshort>(item: Self::Item<'wlong>) -> Self::Item<'wshort> {
item
}

const IS_DENSE: bool = true;

unsafe fn init_fetch<'w>(
Expand Down Expand Up @@ -339,10 +340,6 @@ unsafe impl WorldQuery for Entity {
/// SAFETY: `Self` is the same as `Self::ReadOnly`
unsafe impl WorldQueryData for Entity {
type ReadOnly = Self;

fn shrink<'wlong: 'wshort, 'wshort>(item: Self::Item<'wlong>) -> Self::Item<'wshort> {
item
}
}

/// SAFETY: access is read only
Expand All @@ -357,6 +354,10 @@ unsafe impl<'a> WorldQuery for EntityRef<'a> {
type Item<'w> = EntityRef<'w>;
type State = ();

fn shrink<'wlong: 'wshort, 'wshort>(item: Self::Item<'wlong>) -> Self::Item<'wshort> {
item
}

const IS_DENSE: bool = true;

unsafe fn init_fetch<'w>(
Expand Down Expand Up @@ -424,10 +425,6 @@ unsafe impl<'a> WorldQuery for EntityRef<'a> {
/// SAFETY: `Self` is the same as `Self::ReadOnly`
unsafe impl<'a> WorldQueryData for EntityRef<'a> {
type ReadOnly = Self;

fn shrink<'wlong: 'wshort, 'wshort>(item: Self::Item<'wlong>) -> Self::Item<'wshort> {
item
}
}

/// SAFETY: access is read only
Expand All @@ -439,6 +436,10 @@ unsafe impl<'a> WorldQuery for EntityMut<'a> {
type Item<'w> = EntityMut<'w>;
type State = ();

fn shrink<'wlong: 'wshort, 'wshort>(item: Self::Item<'wlong>) -> Self::Item<'wshort> {
item
}

const IS_DENSE: bool = true;

unsafe fn init_fetch<'w>(
Expand Down Expand Up @@ -506,10 +507,6 @@ unsafe impl<'a> WorldQuery for EntityMut<'a> {
/// SAFETY: access of `EntityRef` is a subset of `EntityMut`
unsafe impl<'a> WorldQueryData for EntityMut<'a> {
type ReadOnly = EntityRef<'a>;

fn shrink<'wlong: 'wshort, 'wshort>(item: Self::Item<'wlong>) -> Self::Item<'wshort> {
item
}
}

#[doc(hidden)]
Expand Down Expand Up @@ -537,6 +534,10 @@ unsafe impl<T: Component> WorldQuery for &T {
type Item<'w> = &'w T;
type State = ComponentId;

fn shrink<'wlong: 'wshort, 'wshort>(item: &'wlong T) -> &'wshort T {
item
}

const IS_DENSE: bool = {
match T::Storage::STORAGE_TYPE {
StorageType::Table => true,
Expand Down Expand Up @@ -652,10 +653,6 @@ unsafe impl<T: Component> WorldQuery for &T {
/// SAFETY: `Self` is the same as `Self::ReadOnly`
unsafe impl<T: Component> WorldQueryData for &T {
type ReadOnly = Self;

fn shrink<'wlong: 'wshort, 'wshort>(item: &'wlong T) -> &'wshort T {
item
}
}

/// SAFETY: access is read only
Expand Down Expand Up @@ -693,6 +690,10 @@ unsafe impl<'__w, T: Component> WorldQuery for Ref<'__w, T> {
type Item<'w> = Ref<'w, T>;
type State = ComponentId;

fn shrink<'wlong: 'wshort, 'wshort>(item: Ref<'wlong, T>) -> Ref<'wshort, T> {
item
}

const IS_DENSE: bool = {
match T::Storage::STORAGE_TYPE {
StorageType::Table => true,
Expand Down Expand Up @@ -819,10 +820,6 @@ unsafe impl<'__w, T: Component> WorldQuery for Ref<'__w, T> {
/// SAFETY: `Self` is the same as `Self::ReadOnly`
unsafe impl<'__w, T: Component> WorldQueryData for Ref<'__w, T> {
type ReadOnly = Self;

fn shrink<'wlong: 'wshort, 'wshort>(item: Ref<'wlong, T>) -> Ref<'wshort, T> {
item
}
}

/// SAFETY: access is read only
Expand Down Expand Up @@ -860,6 +857,10 @@ unsafe impl<'__w, T: Component> WorldQuery for &'__w mut T {
type Item<'w> = Mut<'w, T>;
type State = ComponentId;

fn shrink<'wlong: 'wshort, 'wshort>(item: Mut<'wlong, T>) -> Mut<'wshort, T> {
item
}

const IS_DENSE: bool = {
match T::Storage::STORAGE_TYPE {
StorageType::Table => true,
Expand Down Expand Up @@ -986,10 +987,6 @@ unsafe impl<'__w, T: Component> WorldQuery for &'__w mut T {
/// SAFETY: access of `&T` is a subset of `&mut T`
unsafe impl<'__w, T: Component> WorldQueryData for &'__w mut T {
type ReadOnly = &'__w T;

fn shrink<'wlong: 'wshort, 'wshort>(item: Mut<'wlong, T>) -> Mut<'wshort, T> {
item
}
}

#[doc(hidden)]
Expand All @@ -1016,6 +1013,10 @@ unsafe impl<T: WorldQuery> WorldQuery for Option<T> {
type Item<'w> = Option<T::Item<'w>>;
type State = T::State;

fn shrink<'wlong: 'wshort, 'wshort>(item: Self::Item<'wlong>) -> Self::Item<'wshort> {
item.map(T::shrink)
}

const IS_DENSE: bool = T::IS_DENSE;

#[inline]
Expand Down Expand Up @@ -1103,10 +1104,6 @@ unsafe impl<T: WorldQuery> WorldQuery for Option<T> {
// SAFETY: defers to soundness of `T: WorldQuery` impl
unsafe impl<T: WorldQueryData> WorldQueryData for Option<T> {
type ReadOnly = Option<T::ReadOnly>;

fn shrink<'wlong: 'wshort, 'wshort>(item: Self::Item<'wlong>) -> Self::Item<'wshort> {
item.map(T::shrink)
}
}

/// SAFETY: [`OptionFetch`] is read only because `T` is read only
Expand Down Expand Up @@ -1175,6 +1172,10 @@ unsafe impl<T: Component> WorldQuery for Has<T> {
type Item<'w> = bool;
type State = ComponentId;

fn shrink<'wlong: 'wshort, 'wshort>(item: Self::Item<'wlong>) -> Self::Item<'wshort> {
item
}

const IS_DENSE: bool = {
match T::Storage::STORAGE_TYPE {
StorageType::Table => true,
Expand Down Expand Up @@ -1243,10 +1244,6 @@ unsafe impl<T: Component> WorldQuery for Has<T> {
/// SAFETY: `Self` is the same as `Self::ReadOnly`
unsafe impl<T: Component> WorldQueryData for Has<T> {
type ReadOnly = Self;

fn shrink<'wlong: 'wshort, 'wshort>(item: Self::Item<'wlong>) -> Self::Item<'wshort> {
item
}
}

/// SAFETY: [`Has`] is read only
Expand All @@ -1267,13 +1264,6 @@ macro_rules! impl_tuple_world_query_data {
// SAFETY: defers to soundness `$name: WorldQuery` impl
unsafe impl<$($name: WorldQueryData),*> WorldQueryData for ($($name,)*) {
type ReadOnly = ($($name::ReadOnly,)*);

fn shrink<'wlong: 'wshort, 'wshort>(item: Self::Item<'wlong>) -> Self::Item<'wshort> {
let ($($name,)*) = item;
($(
$name::shrink($name),
)*)
}
}

/// SAFETY: each item in the tuple is read only
Expand All @@ -1297,6 +1287,13 @@ macro_rules! impl_anytuple_fetch {
type Item<'w> = ($(Option<$name::Item<'w>>,)*);
type State = ($($name::State,)*);

fn shrink<'wlong: 'wshort, 'wshort>(item: Self::Item<'wlong>) -> Self::Item<'wshort> {
let ($($name,)*) = item;
($(
$name.map($name::shrink),
)*)
}

#[inline]
#[allow(clippy::unused_unit)]
unsafe fn init_fetch<'w>(_world: UnsafeWorldCell<'w>, state: &Self::State, _last_run: Tick, _this_run: Tick) -> Self::Fetch<'w> {
Expand All @@ -1306,8 +1303,6 @@ macro_rules! impl_anytuple_fetch {

const IS_DENSE: bool = true $(&& $name::IS_DENSE)*;



#[inline]
unsafe fn set_archetype<'w>(
_fetch: &mut Self::Fetch<'w>,
Expand Down Expand Up @@ -1394,13 +1389,6 @@ macro_rules! impl_anytuple_fetch {
// SAFETY: defers to soundness of `$name: WorldQuery` impl
unsafe impl<$($name: WorldQueryData),*> WorldQueryData for AnyOf<($($name,)*)> {
type ReadOnly = AnyOf<($($name::ReadOnly,)*)>;

fn shrink<'wlong: 'wshort, 'wshort>(item: Self::Item<'wlong>) -> Self::Item<'wshort> {
let ($($name,)*) = item;
($(
$name.map($name::shrink),
)*)
}
}

/// SAFETY: each item in the tuple is read only
Expand All @@ -1424,6 +1412,8 @@ unsafe impl<Q: WorldQueryData> WorldQuery for NopWorldQuery<Q> {
type Item<'w> = ();
type State = Q::State;

fn shrink<'wlong: 'wshort, 'wshort>(_: ()) {}

const IS_DENSE: bool = Q::IS_DENSE;

#[inline(always)]
Expand Down Expand Up @@ -1479,8 +1469,6 @@ unsafe impl<Q: WorldQueryData> WorldQuery for NopWorldQuery<Q> {
/// SAFETY: `Self::ReadOnly` is `Self`
unsafe impl<Q: WorldQueryData> WorldQueryData for NopWorldQuery<Q> {
type ReadOnly = Self;

fn shrink<'wlong: 'wshort, 'wshort>(_: ()) {}
}

/// SAFETY: `NopFetch` never accesses any data
Expand All @@ -1495,6 +1483,8 @@ unsafe impl<T: ?Sized> WorldQuery for PhantomData<T> {

type State = ();

fn shrink<'wlong: 'wshort, 'wshort>(_item: Self::Item<'wlong>) -> Self::Item<'wshort> {}

unsafe fn init_fetch<'w>(
_world: UnsafeWorldCell<'w>,
_state: &Self::State,
Expand Down Expand Up @@ -1547,8 +1537,6 @@ unsafe impl<T: ?Sized> WorldQuery for PhantomData<T> {
/// SAFETY: `Self::ReadOnly` is `Self`
unsafe impl<T: ?Sized> WorldQueryData for PhantomData<T> {
type ReadOnly = Self;

fn shrink<'wlong: 'wshort, 'wshort>(_item: Self::Item<'wlong>) -> Self::Item<'wshort> {}
}

/// SAFETY: `PhantomData` never accesses any world data.
Expand Down
16 changes: 16 additions & 0 deletions crates/bevy_ecs/src/query/filter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,8 @@ unsafe impl<T: Component> WorldQuery for With<T> {
type Item<'w> = ();
type State = ComponentId;

fn shrink<'wlong: 'wshort, 'wshort>(_: Self::Item<'wlong>) -> Self::Item<'wshort> {}

#[inline]
unsafe fn init_fetch(
_world: UnsafeWorldCell,
Expand Down Expand Up @@ -246,6 +248,8 @@ unsafe impl<T: Component> WorldQuery for Without<T> {
type Item<'w> = ();
type State = ComponentId;

fn shrink<'wlong: 'wshort, 'wshort>(_: Self::Item<'wlong>) -> Self::Item<'wshort> {}

#[inline]
unsafe fn init_fetch(
_world: UnsafeWorldCell,
Expand Down Expand Up @@ -382,6 +386,10 @@ macro_rules! impl_query_filter_tuple {
type Item<'w> = bool;
type State = ($($filter::State,)*);

fn shrink<'wlong: 'wshort, 'wshort>(item: Self::Item<'wlong>) -> Self::Item<'wshort> {
item
}

const IS_DENSE: bool = true $(&& $filter::IS_DENSE)*;

#[inline]
Expand Down Expand Up @@ -555,6 +563,10 @@ unsafe impl<T: Component> WorldQuery for Added<T> {
type Item<'w> = bool;
type State = ComponentId;

fn shrink<'wlong: 'wshort, 'wshort>(item: Self::Item<'wlong>) -> Self::Item<'wshort> {
item
}

#[inline]
unsafe fn init_fetch<'w>(
world: UnsafeWorldCell<'w>,
Expand Down Expand Up @@ -720,6 +732,10 @@ unsafe impl<T: Component> WorldQuery for Changed<T> {
type Item<'w> = bool;
type State = ComponentId;

fn shrink<'wlong: 'wshort, 'wshort>(item: Self::Item<'wlong>) -> Self::Item<'wshort> {
item
}

#[inline]
unsafe fn init_fetch<'w>(
world: UnsafeWorldCell<'w>,
Expand Down
Loading

0 comments on commit 1e2f825

Please sign in to comment.