11extern crate proc_macro;
22
33use proc_macro:: TokenStream ;
4- use std:: { env, path:: PathBuf } ;
4+ use std:: { env, path:: PathBuf , sync :: LazyLock } ;
55use toml_edit:: { DocumentMut , Item } ;
66
77/// The path to the `Cargo.toml` file for the Bevy project.
88pub 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)
0 commit comments