Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion crates/bevy_asset/macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ fn derive_dependency_visitor_internal(

Ok(quote! {
impl #impl_generics #bevy_asset_path::VisitAssetDependencies for #struct_name #type_generics #where_clause {
fn visit_dependencies(&self, #visit: &mut impl FnMut(#bevy_asset_path::UntypedAssetId)) {
fn visit_dependencies(&self, #visit: &mut impl ::core::ops::FnMut(#bevy_asset_path::UntypedAssetId)) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it make sense to introduce an  FQFnMut  here, similar to other FQ types used elsewhere?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure what the policy should be for adding new FQ types; adding every type in core/std would be excessive, so just the ones we use? I added FQIterator and FQInto because they appeared a few times (Iterator 6 times, Into 9 times). I could add FQFnMut, since FnMut appears 3 times, but I would also need to add FQFn (appears once) and FQFnOnce (doesn't appear) for parity.

I'd like to keep changes to the fq_std module small in this PR, as the focus is fixing hygiene issues. I think the module could be improved (FQBox in particular) in a later PR focused on code quality.

#body
}
}
Expand Down
24 changes: 14 additions & 10 deletions crates/bevy_ecs/macro_logic/src/component.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use bevy_macro_utils::fq_std::{FQDefault, FQOption, FQSend, FQSync};
use proc_macro2::{Span, TokenStream};
use quote::{quote, ToTokens};
use std::collections::HashSet;
Expand Down Expand Up @@ -242,7 +243,7 @@ impl DeriveComponent {
let ident = &require.path;
let constructor = match &require.func {
Some(func) => quote! { || { let x: #ident = (#func)().into(); x } },
None => quote! { <#ident as ::core::default::Default>::default },
None => quote! { <#ident as #FQDefault>::default },
};
register_required.push(quote! {
required_components.register_required::<#ident>(#constructor);
Expand All @@ -254,7 +255,7 @@ impl DeriveComponent {
ast.generics
.make_where_clause()
.predicates
.push(parse_quote! { Self: ::core::marker::Send + ::core::marker::Sync + 'static });
.push(parse_quote! { Self: #FQSend + #FQSync + 'static });
let (impl_generics, type_generics, where_clause) = &ast.generics.split_for_impl();

let required_component_docs = self.requires.map(|r| {
Expand Down Expand Up @@ -301,7 +302,7 @@ impl DeriveComponent {
let relationship_member = field.ident.clone().map_or(Member::from(0), Member::Named);
if relationship.is_some() {
quote! {
::core::option::Option::Some(
#FQOption::Some(
// Safety: we pass valid offset of a field containing Entity (obtained via offset_off!)
unsafe {
#bevy_ecs::relationship::ComponentRelationshipAccessor::<Self>::relationship(
Expand All @@ -312,11 +313,11 @@ impl DeriveComponent {
}
} else {
quote! {
::core::option::Option::Some(#bevy_ecs::relationship::ComponentRelationshipAccessor::<Self>::relationship_target())
#FQOption::Some(#bevy_ecs::relationship::ComponentRelationshipAccessor::<Self>::relationship_target())
}
}
} else {
quote! {::core::option::Option::None}
quote! {#FQOption::None}
};
Ok(quote! {
#required_component_docs
Expand All @@ -343,7 +344,7 @@ impl DeriveComponent {

#map_entities

fn relationship_accessor() -> ::core::option::Option<#bevy_ecs::relationship::ComponentRelationshipAccessor<Self>> {
fn relationship_accessor() -> #FQOption<#bevy_ecs::relationship::ComponentRelationshipAccessor<Self>> {
#relationship_accessor
}
}
Expand Down Expand Up @@ -385,6 +386,8 @@ impl DeriveComponent {
let relationship_target = &relationship.relationship_target;
let allow_self_referential = relationship.allow_self_referential;

let fqdefault = FQDefault.into_token_stream();

Ok(Some(quote! {
impl #impl_generics #bevy_ecs::relationship::Relationship for #struct_name #type_generics #where_clause {
type RelationshipTarget = #relationship_target;
Expand All @@ -398,7 +401,7 @@ impl DeriveComponent {
#[inline]
fn from(entity: #bevy_ecs::entity::Entity) -> Self {
Self {
#(#members: ::core::default::Default::default(),)*
#(#members: #fqdefault::default(),)*
#relationship_member: entity
}
}
Expand Down Expand Up @@ -447,6 +450,7 @@ impl DeriveComponent {
let struct_name = &ast.ident;
let (impl_generics, type_generics, where_clause) = &ast.generics.split_for_impl();
let linked_spawn = relationship_target.linked_spawn;
let fqdefault = FQDefault.into_token_stream();
Ok(Some(quote! {
impl #impl_generics #bevy_ecs::relationship::RelationshipTarget for #struct_name #type_generics #where_clause {
const LINKED_SPAWN: bool = #linked_spawn;
Expand All @@ -466,7 +470,7 @@ impl DeriveComponent {
#[inline]
fn from_collection_risky(collection: Self::Collection) -> Self {
Self {
#(#members: ::core::default::Default::default(),)*
#(#members: #fqdefault::default(),)*
#relationship_member: collection
}
}
Expand Down Expand Up @@ -672,8 +676,8 @@ fn hook_register_function_call(
}
};
quote! {
fn #hook() -> ::core::option::Option<#bevy_ecs_path::lifecycle::ComponentHook> {
::core::option::Option::Some(#hook_function)
fn #hook() -> #FQOption<#bevy_ecs_path::lifecycle::ComponentHook> {
#FQOption::Some(#hook_function)
}
}
}
Expand Down
7 changes: 4 additions & 3 deletions crates/bevy_ecs/macros/src/event.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use bevy_macro_utils::fq_std::{FQInto, FQSend, FQSync};
use proc_macro::TokenStream;
use quote::quote;
use syn::{
Expand All @@ -19,7 +20,7 @@ pub fn derive_event(input: TokenStream) -> TokenStream {
ast.generics
.make_where_clause()
.predicates
.push(parse_quote! { Self: ::core::marker::Send + ::core::marker::Sync + 'static });
.push(parse_quote! { Self: #FQSend + #FQSync + 'static });

let mut processed_attrs = Vec::new();
let mut trigger: Option<Type> = None;
Expand Down Expand Up @@ -63,7 +64,7 @@ pub fn derive_entity_event(input: TokenStream) -> TokenStream {
ast.generics
.make_where_clause()
.predicates
.push(parse_quote! { Self: ::core::marker::Send + ::core::marker::Sync + 'static });
.push(parse_quote! { Self: #FQSend + #FQSync + 'static });

let mut auto_propagate = false;
let mut propagate = false;
Expand Down Expand Up @@ -139,7 +140,7 @@ pub fn derive_entity_event(input: TokenStream) -> TokenStream {
quote! {
impl #impl_generics #bevy_ecs_path::event::SetEntityEventTarget for #struct_name #type_generics #where_clause {
fn set_event_target(&mut self, entity: #bevy_ecs_path::entity::Entity) {
self.#entity_field = ::core::convert::Into::into(entity);
self.#entity_field = #FQInto::into(entity);
}
}
}
Expand Down
31 changes: 17 additions & 14 deletions crates/bevy_ecs/macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ mod world_query;
use crate::{query_data::derive_query_data_impl, query_filter::derive_query_filter_impl};
use bevy_ecs_macro_logic::{component::DeriveComponent, map_entities::map_entities};
use bevy_macro_utils::{
derive_label, ensure_no_collision, get_struct_fields, pascal_to_snake_case, BevyManifest,
derive_label, ensure_no_collision,
fq_std::{FQDefault, FQIterator, FQOption, FQResult},
get_struct_fields, pascal_to_snake_case, BevyManifest,
};
use proc_macro::TokenStream;
use proc_macro2::{Ident, Span};
Expand Down Expand Up @@ -138,13 +140,13 @@ pub fn derive_bundle(input: TokenStream) -> TokenStream {
unsafe impl #impl_generics #ecs_path::bundle::Bundle for #struct_name #ty_generics #where_clause {
fn component_ids(
components: &mut #ecs_path::component::ComponentsRegistrator,
) -> impl ::core::iter::Iterator<Item = #ecs_path::component::ComponentId> + use<#(#generics_ty_list,)*> {
) -> impl #FQIterator<Item = #ecs_path::component::ComponentId> + use<#(#generics_ty_list,)*> {
::core::iter::empty()#(.chain(<#active_field_types as #ecs_path::bundle::Bundle>::component_ids(components)))*
}

fn get_component_ids(
components: &#ecs_path::component::Components,
) -> impl ::core::iter::Iterator<Item = ::core::option::Option<#ecs_path::component::ComponentId>> {
) -> impl #FQIterator<Item = #FQOption<#ecs_path::component::ComponentId>> {
::core::iter::empty()#(.chain(<#active_field_types as #ecs_path::bundle::Bundle>::get_component_ids(components)))*
}
}
Expand All @@ -157,7 +159,7 @@ pub fn derive_bundle(input: TokenStream) -> TokenStream {
#[inline]
unsafe fn get_components(
ptr: #ecs_path::ptr::MovingPtr<'_, Self>,
func: &mut impl FnMut(#ecs_path::component::StorageType, #ecs_path::ptr::OwningPtr<'_>)
func: &mut impl ::core::ops::FnMut(#ecs_path::component::StorageType, #ecs_path::ptr::OwningPtr<'_>)
) {
use #ecs_path::__macro_exports::DebugCheckedUnwrap;

Expand All @@ -182,18 +184,19 @@ pub fn derive_bundle(input: TokenStream) -> TokenStream {
}
};

let fqdefault = FQDefault.into_token_stream();
let from_components_impl = attributes.impl_from_components.then(|| quote! {
// SAFETY:
// - ComponentId is returned in field-definition-order. [from_components] uses field-definition-order
unsafe impl #impl_generics #ecs_path::bundle::BundleFromComponents for #struct_name #ty_generics #where_clause {
#[allow(unused_variables, non_snake_case)]
unsafe fn from_components<__T, __F>(ctx: &mut __T, func: &mut __F) -> Self
where
__F: FnMut(&mut __T) -> #ecs_path::ptr::OwningPtr<'_>
__F: ::core::ops::FnMut(&mut __T) -> #ecs_path::ptr::OwningPtr<'_>
{
Self {
#(#active_field_members: <#active_field_types as #ecs_path::bundle::BundleFromComponents>::from_components(ctx, &mut *func),)*
#(#inactive_field_members: ::core::default::Default::default(),)*
#(#inactive_field_members: #fqdefault::default(),)*
}
}
}
Expand Down Expand Up @@ -457,14 +460,14 @@ fn derive_system_param_impl(
system_meta: &#path::system::SystemMeta,
world: #path::world::unsafe_world_cell::UnsafeWorldCell<'w>,
change_tick: #path::change_detection::Tick,
) -> ::core::result::Result<Self::Item<'w, 's>, #path::system::SystemParamValidationError> {
) -> #FQResult<Self::Item<'w, 's>, #path::system::SystemParamValidationError> {
let (#(#tuple_patterns,)*) = &mut state.state;
#(
let #field_locals = unsafe {
<#field_types as #path::system::SystemParam>::get_param(#field_locals, system_meta, world, change_tick)
}.map_err(|err| #path::system::SystemParamValidationError::new::<Self>(err.skipped, #field_validation_messages, #field_validation_names))?;
)*
::core::result::Result::Ok(#struct_name {
#FQResult::Ok(#struct_name {
#(#field_members: #field_locals,)*
})
}
Expand Down Expand Up @@ -676,23 +679,23 @@ pub fn derive_settings_group(input: TokenStream) -> TokenStream {

let group_name = override_group_name.unwrap_or(pascal_to_snake_case(&name.to_string()));
let key_name = key_name
.map(|f| quote! { ::core::option::Option::Some(#f) })
.unwrap_or(quote! { ::core::option::Option::None });
.map(|f| quote! { #FQOption::Some(#f) })
.unwrap_or(quote! { #FQOption::None });
let file_name = override_file
.map(|f| quote! { ::core::option::Option::Some(#f) })
.unwrap_or(quote! { ::core::option::Option::None });
.map(|f| quote! { #FQOption::Some(#f) })
.unwrap_or(quote! { #FQOption::None });

let expanded = quote! {
impl #path::SettingsGroup for #name {
fn settings_group_name() -> &'static str {
#group_name
}

fn settings_key_name() -> ::core::option::Option<&'static str> {
fn settings_key_name() -> #FQOption<&'static str> {
#key_name
}

fn settings_source() -> ::core::option::Option<&'static str> {
fn settings_source() -> #FQOption<&'static str> {
#file_name
}
}
Expand Down
3 changes: 2 additions & 1 deletion crates/bevy_ecs/macros/src/message.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use bevy_macro_utils::fq_std::{FQSend, FQSync};
use proc_macro::TokenStream;
use quote::quote;
use syn::{parse_macro_input, parse_quote, DeriveInput, Path};
Expand All @@ -9,7 +10,7 @@ pub fn derive_message(input: TokenStream) -> TokenStream {
ast.generics
.make_where_clause()
.predicates
.push(parse_quote! { Self: ::core::marker::Send + ::core::marker::Sync + 'static });
.push(parse_quote! { Self: #FQSend + #FQSync + 'static });

let struct_name = &ast.ident;
let (impl_generics, type_generics, where_clause) = &ast.generics.split_for_impl();
Expand Down
18 changes: 11 additions & 7 deletions crates/bevy_ecs/macros/src/query_data.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
use bevy_macro_utils::{ensure_no_collision, get_struct_fields};
use bevy_macro_utils::{
ensure_no_collision,
fq_std::{FQIterator, FQOption},
get_struct_fields,
};
use proc_macro::TokenStream;
use proc_macro2::{Ident, Span};
use quote::{format_ident, quote};
Expand Down Expand Up @@ -456,15 +460,15 @@ pub fn derive_query_data_impl(input: TokenStream) -> TokenStream {
_fetch: &mut <Self as #path::query::WorldQuery>::Fetch<'__w>,
_entity: #path::entity::Entity,
_table_row: #path::storage::TableRow,
) -> ::core::option::Option<Self::Item<'__w, '__s>> {
::core::option::Option::Some(Self::Item {
) -> #FQOption<Self::Item<'__w, '__s>> {
#FQOption::Some(Self::Item {
#(#field_members: <#read_only_field_types>::fetch(&_state.#field_aliases, &mut _fetch.#field_aliases, _entity, _table_row)?,)*
})
}

fn iter_access(
_state: &Self::State,
) -> impl ::core::iter::Iterator<Item = #path::query::EcsAccessType<'_>> {
) -> impl #FQIterator<Item = #path::query::EcsAccessType<'_>> {
::core::iter::empty() #(.chain(<#field_types>::iter_access(&_state.#field_aliases)))*
}
}
Expand Down Expand Up @@ -538,15 +542,15 @@ pub fn derive_query_data_impl(input: TokenStream) -> TokenStream {
_fetch: &mut <Self as #path::query::WorldQuery>::Fetch<'__w>,
_entity: #path::entity::Entity,
_table_row: #path::storage::TableRow,
) -> ::core::option::Option<Self::Item<'__w, '__s>> {
::core::option::Option::Some(Self::Item {
) -> #FQOption<Self::Item<'__w, '__s>> {
#FQOption::Some(Self::Item {
#(#field_members: <#field_types>::fetch(&_state.#field_aliases, &mut _fetch.#field_aliases, _entity, _table_row)?,)*
})
}

fn iter_access(
_state: &Self::State,
) -> impl ::core::iter::Iterator<Item = #path::query::EcsAccessType<'_>> {
) -> impl #FQIterator<Item = #path::query::EcsAccessType<'_>> {
::core::iter::empty() #(.chain(<#field_types>::iter_access(&_state.#field_aliases)))*
}
}
Expand Down
3 changes: 2 additions & 1 deletion crates/bevy_ecs/macros/src/resource.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use bevy_ecs_macro_logic::component::DeriveComponent;
use bevy_macro_utils::fq_std::FQOption;
use proc_macro2::TokenStream;
use quote::quote;
use syn::{DeriveInput, Path};
Expand All @@ -15,7 +16,7 @@ pub fn derive_resource(ast: &mut DeriveInput) -> TokenStream {

// We add the component_id existence check here to avoid recursive init during required components initialization.
derive_component.additional_requires.push(quote! {
let resource_component_id = if let ::core::option::Option::Some(id) = required_components.components_registrator().component_id::<#struct_name #type_generics>() {
let resource_component_id = if let #FQOption::Some(id) = required_components.components_registrator().component_id::<#struct_name #type_generics>() {
id
} else {
required_components.components_registrator().register_component::<#struct_name #type_generics>()
Expand Down
Loading