Skip to content

Commit b6e22ea

Browse files
committed
Use one BevyManifest instance in the proc macros since CARGO_MANIFEST_DIR won't change during the execution.
1 parent 6178ce9 commit b6e22ea

File tree

12 files changed

+22
-36
lines changed

12 files changed

+22
-36
lines changed

crates/bevy_asset/macros/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use quote::{format_ident, quote};
88
use syn::{parse_macro_input, Data, DeriveInput, Path};
99

1010
pub(crate) fn bevy_asset_path() -> Path {
11-
BevyManifest::default().get_path("bevy_asset")
11+
BevyManifest::shared().get_path("bevy_asset")
1212
}
1313

1414
const DEPENDENCY_ATTRIBUTE: &str = "dependency";

crates/bevy_derive/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ pub fn derive_enum_variant_meta(input: TokenStream) -> TokenStream {
205205
#[proc_macro_derive(AppLabel)]
206206
pub fn derive_app_label(input: TokenStream) -> TokenStream {
207207
let input = syn::parse_macro_input!(input as syn::DeriveInput);
208-
let mut trait_path = BevyManifest::default().get_path("bevy_app");
208+
let mut trait_path = BevyManifest::shared().get_path("bevy_app");
209209
let mut dyn_eq_path = trait_path.clone();
210210
trait_path.segments.push(format_ident!("AppLabel").into());
211211
dyn_eq_path.segments.push(format_ident!("DynEq").into());

crates/bevy_ecs/macros/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -708,7 +708,7 @@ pub fn derive_system_set(input: TokenStream) -> TokenStream {
708708
}
709709

710710
pub(crate) fn bevy_ecs_path() -> syn::Path {
711-
BevyManifest::default().get_path("bevy_ecs")
711+
BevyManifest::shared().get_path("bevy_ecs")
712712
}
713713

714714
#[proc_macro_derive(Event)]

crates/bevy_encase_derive/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use encase_derive_impl::{implement, syn};
1313
const ENCASE: &str = "encase";
1414

1515
fn bevy_encase_path() -> syn::Path {
16-
let bevy_manifest = BevyManifest::default();
16+
let bevy_manifest = BevyManifest::shared();
1717
bevy_manifest
1818
.get_subcrate("render")
1919
.map(|bevy_render_path| {

crates/bevy_gizmos/macros/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ use syn::{parse_macro_input, parse_quote, DeriveInput, Path};
1111
#[proc_macro_derive(GizmoConfigGroup)]
1212
pub fn derive_gizmo_config_group(input: TokenStream) -> TokenStream {
1313
let mut ast = parse_macro_input!(input as DeriveInput);
14-
let bevy_gizmos_path: Path = BevyManifest::default().get_path("bevy_gizmos");
15-
let bevy_reflect_path: Path = BevyManifest::default().get_path("bevy_reflect");
14+
let bevy_gizmos_path: Path = BevyManifest::shared().get_path("bevy_gizmos");
15+
let bevy_reflect_path: Path = BevyManifest::shared().get_path("bevy_reflect");
1616

1717
ast.generics.make_where_clause().predicates.push(
1818
parse_quote! { Self: #bevy_reflect_path::Reflect + #bevy_reflect_path::TypePath + Default},

crates/bevy_macro_utils/src/bevy_manifest.rs

Lines changed: 10 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,21 @@
11
extern crate proc_macro;
22

33
use proc_macro::TokenStream;
4-
use std::{env, path::PathBuf};
4+
use std::{env, path::PathBuf, sync::LazyLock};
55
use toml_edit::{DocumentMut, Item};
66

77
/// The path to the `Cargo.toml` file for the Bevy project.
88
pub struct BevyManifest {
99
manifest: DocumentMut,
1010
}
1111

12-
impl Default for BevyManifest {
13-
fn default() -> Self {
14-
Self {
12+
const BEVY: &str = "bevy";
13+
const BEVY_INTERNAL: &str = "bevy_internal";
14+
15+
impl BevyManifest {
16+
/// Returns a global shared instance of the [`BevyManifest`] struct.
17+
pub fn shared() -> &'static LazyLock<Self> {
18+
static LAZY_SELF: LazyLock<BevyManifest> = LazyLock::new(|| BevyManifest {
1519
manifest: env::var_os("CARGO_MANIFEST_DIR")
1620
.map(PathBuf::from)
1721
.map(|mut path| {
@@ -30,13 +34,10 @@ impl Default for BevyManifest {
3034
})
3135
})
3236
.expect("CARGO_MANIFEST_DIR is not defined."),
33-
}
37+
});
38+
&LAZY_SELF
3439
}
35-
}
36-
const BEVY: &str = "bevy";
37-
const BEVY_INTERNAL: &str = "bevy_internal";
3840

39-
impl BevyManifest {
4041
/// Attempt to retrieve the [path](syn::Path) of a particular package in
4142
/// the [manifest](BevyManifest) by [name](str).
4243
pub fn maybe_get_path(&self, name: &str) -> Option<syn::Path> {
@@ -73,21 +74,6 @@ impl BevyManifest {
7374
.or_else(|| deps_dev.and_then(find_in_deps))
7475
}
7576

76-
/// Returns the path for the crate with the given name.
77-
///
78-
/// This is a convenience method for constructing a [manifest] and
79-
/// calling the [`get_path`] method.
80-
///
81-
/// This method should only be used where you just need the path and can't
82-
/// cache the [manifest]. If caching is possible, it's recommended to create
83-
/// the [manifest] yourself and use the [`get_path`] method.
84-
///
85-
/// [`get_path`]: Self::get_path
86-
/// [manifest]: Self
87-
pub fn get_path_direct(name: &str) -> syn::Path {
88-
Self::default().get_path(name)
89-
}
90-
9177
/// Returns the path for the crate with the given name.
9278
pub fn get_path(&self, name: &str) -> syn::Path {
9379
self.maybe_get_path(name)

crates/bevy_reflect/derive/src/meta.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@ use syn::Path;
33

44
/// Returns the correct path for `bevy_reflect`.
55
pub(crate) fn get_bevy_reflect_path() -> Path {
6-
BevyManifest::get_path_direct("bevy_reflect")
6+
BevyManifest::shared().get_path("bevy_reflect")
77
}

crates/bevy_reflect/derive/src/trait_reflection.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ pub(crate) fn reflect_trait(_args: &TokenStream, input: TokenStream) -> TokenStr
3434
let trait_ident = &item_trait.ident;
3535
let trait_vis = &item_trait.vis;
3636
let reflect_trait_ident = crate::ident::get_reflect_ident(&item_trait.ident.to_string());
37-
let bevy_reflect_path = BevyManifest::default().get_path("bevy_reflect");
37+
let bevy_reflect_path = BevyManifest::shared().get_path("bevy_reflect");
3838

3939
let struct_doc = format!(
4040
" A type generated by the #[reflect_trait] macro for the `{trait_ident}` trait.\n\n This allows casting from `dyn Reflect` to `dyn {trait_ident}`.",

crates/bevy_render/macros/src/as_bind_group.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ enum BindingState<'a> {
4040
}
4141

4242
pub fn derive_as_bind_group(ast: syn::DeriveInput) -> Result<TokenStream> {
43-
let manifest = BevyManifest::default();
43+
let manifest = BevyManifest::shared();
4444
let render_path = manifest.get_path("bevy_render");
4545
let image_path = manifest.get_path("bevy_image");
4646
let asset_path = manifest.get_path("bevy_asset");

crates/bevy_render/macros/src/extract_component.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use syn::{parse_macro_input, parse_quote, DeriveInput, Path};
55
pub fn derive_extract_component(input: TokenStream) -> TokenStream {
66
let mut ast = parse_macro_input!(input as DeriveInput);
77
let bevy_render_path: Path = crate::bevy_render_path();
8-
let bevy_ecs_path: Path = bevy_macro_utils::BevyManifest::default()
8+
let bevy_ecs_path: Path = bevy_macro_utils::BevyManifest::shared()
99
.maybe_get_path("bevy_ecs")
1010
.expect("bevy_ecs should be found in manifest");
1111

0 commit comments

Comments
 (0)