Make public macros more robust with $crate (#4655)

# Objective

We have some macros that are public but only used internally for now. They fail on user's code due to the use of crate names like `bevy_utils`, while the user only has `bevy::utils`. There are two affected macros.

- `bevy_utils::define_label`: it may be useful in user's code for defining custom kinds of label traits (this is why I made this PR).
- `bevy_asset::load_internal_asset`: not useful currently due to limitations of the debug asset server, but this may change in the future.

## Solution

We can make them work by using `$crate` instead of names of their own crates, which can refer to the macro's defining crate regardless of the user's setup. Even though our objective is rather low-priority here, the solution adds no maintenance cost so it is still worthwhile.
This commit is contained in:
Yutao Yuan 2022-05-06 19:29:45 +00:00
parent 068e9eaae8
commit aabc47f290
2 changed files with 5 additions and 5 deletions

View file

@ -356,8 +356,8 @@ macro_rules! load_internal_asset {
{ {
let mut debug_app = $app let mut debug_app = $app
.world .world
.non_send_resource_mut::<bevy_asset::debug_asset_server::DebugAssetApp>(); .non_send_resource_mut::<$crate::debug_asset_server::DebugAssetApp>();
bevy_asset::debug_asset_server::register_handle_with_loader( $crate::debug_asset_server::register_handle_with_loader(
$loader, $loader,
&mut debug_app, &mut debug_app,
$handle, $handle,
@ -365,7 +365,7 @@ macro_rules! load_internal_asset {
$path_str, $path_str,
); );
} }
let mut assets = $app.world.resource_mut::<bevy_asset::Assets<_>>(); let mut assets = $app.world.resource_mut::<$crate::Assets<_>>();
assets.set_untracked($handle, ($loader)(include_str!($path_str))); assets.set_untracked($handle, ($loader)(include_str!($path_str)));
}}; }};
} }
@ -374,7 +374,7 @@ macro_rules! load_internal_asset {
#[macro_export] #[macro_export]
macro_rules! load_internal_asset { macro_rules! load_internal_asset {
($app: ident, $handle: ident, $path_str: expr, $loader: expr) => {{ ($app: ident, $handle: ident, $path_str: expr, $loader: expr) => {{
let mut assets = $app.world.resource_mut::<bevy_asset::Assets<_>>(); let mut assets = $app.world.resource_mut::<$crate::Assets<_>>();
assets.set_untracked($handle, ($loader)(include_str!($path_str))); assets.set_untracked($handle, ($loader)(include_str!($path_str)));
}}; }};
} }

View file

@ -60,7 +60,7 @@ macro_rules! define_label {
($label_trait_name:ident) => { ($label_trait_name:ident) => {
/// Defines a set of strongly-typed labels for a class of objects /// Defines a set of strongly-typed labels for a class of objects
pub trait $label_trait_name: pub trait $label_trait_name:
::bevy_utils::label::DynHash + ::std::fmt::Debug + Send + Sync + 'static $crate::label::DynHash + ::std::fmt::Debug + Send + Sync + 'static
{ {
#[doc(hidden)] #[doc(hidden)]
fn dyn_clone(&self) -> Box<dyn $label_trait_name>; fn dyn_clone(&self) -> Box<dyn $label_trait_name>;