diff --git a/crates/ruma-macros/Cargo.toml b/crates/ruma-macros/Cargo.toml index 3fdb6dab..01a4f157 100644 --- a/crates/ruma-macros/Cargo.toml +++ b/crates/ruma-macros/Cargo.toml @@ -22,7 +22,6 @@ __internal_macro_expand = ["syn/visit-mut"] [dependencies] cfg-if = "1.0.0" -once_cell = "1.13.0" proc-macro-crate = "3.1.0" proc-macro2 = "1.0.24" quote = "1.0.8" diff --git a/crates/ruma-macros/src/api.rs b/crates/ruma-macros/src/api.rs index d720577c..fdcfa80d 100644 --- a/crates/ruma-macros/src/api.rs +++ b/crates/ruma-macros/src/api.rs @@ -1,8 +1,7 @@ //! Methods and types for generating API endpoints. -use std::{env, fs, path::Path}; +use std::{env, fs, path::Path, sync::OnceLock}; -use once_cell::sync::Lazy; use proc_macro2::Span; use serde::{de::IgnoredAny, Deserialize}; @@ -29,37 +28,41 @@ fn ensure_feature_presence() -> Option<&'static syn::Error> { server: Option, } - static RESULT: Lazy> = Lazy::new(|| { - let manifest_dir = env::var("CARGO_MANIFEST_DIR") - .map_err(|_| syn::Error::new(Span::call_site(), "Failed to read CARGO_MANIFEST_DIR"))?; + static RESULT: OnceLock> = OnceLock::new(); - let manifest_file = Path::new(&manifest_dir).join("Cargo.toml"); - let manifest_bytes = fs::read_to_string(manifest_file) - .map_err(|_| syn::Error::new(Span::call_site(), "Failed to read Cargo.toml"))?; + RESULT + .get_or_init(|| { + let manifest_dir = env::var("CARGO_MANIFEST_DIR").map_err(|_| { + syn::Error::new(Span::call_site(), "Failed to read CARGO_MANIFEST_DIR") + })?; - let manifest_parsed: CargoToml = toml::from_str(&manifest_bytes) - .map_err(|_| syn::Error::new(Span::call_site(), "Failed to parse Cargo.toml"))?; + let manifest_file = Path::new(&manifest_dir).join("Cargo.toml"); + let manifest_bytes = fs::read_to_string(manifest_file) + .map_err(|_| syn::Error::new(Span::call_site(), "Failed to read Cargo.toml"))?; - if manifest_parsed.features.client.is_none() { - return Err(syn::Error::new( - Span::call_site(), - "This crate doesn't define a `client` feature in its `Cargo.toml`.\n\ + let manifest_parsed: CargoToml = toml::from_str(&manifest_bytes) + .map_err(|_| syn::Error::new(Span::call_site(), "Failed to parse Cargo.toml"))?; + + if manifest_parsed.features.client.is_none() { + return Err(syn::Error::new( + Span::call_site(), + "This crate doesn't define a `client` feature in its `Cargo.toml`.\n\ Please add a `client` feature such that generated `OutgoingRequest` and \ `IncomingResponse` implementations can be enabled.", - )); - } + )); + } - if manifest_parsed.features.server.is_none() { - return Err(syn::Error::new( - Span::call_site(), - "This crate doesn't define a `server` feature in its `Cargo.toml`.\n\ + if manifest_parsed.features.server.is_none() { + return Err(syn::Error::new( + Span::call_site(), + "This crate doesn't define a `server` feature in its `Cargo.toml`.\n\ Please add a `server` feature such that generated `IncomingRequest` and \ `OutgoingResponse` implementations can be enabled.", - )); - } + )); + } - Ok(()) - }); - - RESULT.as_ref().err() + Ok(()) + }) + .as_ref() + .err() }