Skip to content

Commit

Permalink
Make public macros more robust with $crate (bevyengine#4655)
Browse files Browse the repository at this point in the history
# 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.
  • Loading branch information
infmagic2047 authored and exjam committed May 22, 2022
1 parent 2e40b58 commit 93a28d3
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 5 deletions.
8 changes: 4 additions & 4 deletions crates/bevy_asset/src/assets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -356,16 +356,16 @@ macro_rules! load_internal_asset {
{
let mut debug_app = $app
.world
.non_send_resource_mut::<bevy_asset::debug_asset_server::DebugAssetApp>();
bevy_asset::debug_asset_server::register_handle_with_loader(
.non_send_resource_mut::<$crate::debug_asset_server::DebugAssetApp>();
$crate::debug_asset_server::register_handle_with_loader(
$loader,
&mut debug_app,
$handle,
file!(),
$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)));
}};
}
Expand All @@ -374,7 +374,7 @@ macro_rules! load_internal_asset {
#[macro_export]
macro_rules! load_internal_asset {
($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)));
}};
}
Expand Down
2 changes: 1 addition & 1 deletion crates/bevy_utils/src/label.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ macro_rules! define_label {
($label_trait_name:ident) => {
/// Defines a set of strongly-typed labels for a class of objects
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)]
fn dyn_clone(&self) -> Box<dyn $label_trait_name>;
Expand Down

0 comments on commit 93a28d3

Please sign in to comment.