From 429143af4299327a816b9d50e13a4cfc3ba247a7 Mon Sep 17 00:00:00 2001 From: Ed Page Date: Tue, 16 Aug 2022 15:03:35 -0500 Subject: [PATCH] fix: Remove once_cell dependency from derive With being able to accept owned types now, `clap_derive` no longer needs `once_cell` to make dynamic data static. This helps towards #1365, #2037 --- Cargo.toml | 2 +- clap_derive/src/attrs.rs | 38 ++++++++------------------------------ src/lib.rs | 2 +- 3 files changed, 10 insertions(+), 32 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 214c230eec6..eed7a7d9b76 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -69,7 +69,7 @@ suggestions = ["dep:strsim"] # Optional deprecated = ["clap_derive?/deprecated"] # Guided experience to prepare for next breaking release (at different stages of development, this may become default) -derive = ["clap_derive", "dep:once_cell"] +derive = ["clap_derive"] cargo = ["dep:once_cell"] # Disable if you're not using Cargo, enables Cargo-env-var-dependent macros wrap_help = ["dep:terminal_size", "textwrap/terminal_size"] env = [] # Use environment variables during arg parsing diff --git a/clap_derive/src/attrs.rs b/clap_derive/src/attrs.rs index 52ce9b46005..79c109d3a2c 100644 --- a/clap_derive/src/attrs.rs +++ b/clap_derive/src/attrs.rs @@ -504,11 +504,10 @@ impl Attrs { }) } else { quote_spanned!(ident.span()=> { - static DEFAULT_VALUE: clap::__macro_refs::once_cell::sync::Lazy = clap::__macro_refs::once_cell::sync::Lazy::new(|| { + { let val: #ty = #val; ::std::string::ToString::to_string(&val) - }); - &*DEFAULT_VALUE + } }) }; @@ -558,10 +557,7 @@ impl Attrs { } - static DEFAULT_VALUES: clap::__macro_refs::once_cell::sync::Lazy> = clap::__macro_refs::once_cell::sync::Lazy::new(|| { - iter_to_vals(#expr) - }); - DEFAULT_VALUES.iter().copied() + iter_to_vals(#expr) } }) } else { @@ -575,14 +571,7 @@ impl Attrs { } - static DEFAULT_STRINGS: clap::__macro_refs::once_cell::sync::Lazy> = clap::__macro_refs::once_cell::sync::Lazy::new(|| { - iter_to_vals(#expr) - }); - - static DEFAULT_VALUES: clap::__macro_refs::once_cell::sync::Lazy> = clap::__macro_refs::once_cell::sync::Lazy::new(|| { - DEFAULT_STRINGS.iter().map(::std::string::String::as_str).collect() - }); - DEFAULT_VALUES.iter().copied() + iter_to_vals(#expr) } }) }; @@ -619,11 +608,10 @@ impl Attrs { }) } else { quote_spanned!(ident.span()=> { - static DEFAULT_VALUE: clap::__macro_refs::once_cell::sync::Lazy<::std::ffi::OsString> = clap::__macro_refs::once_cell::sync::Lazy::new(|| { + { let val: #ty = #val; ::std::ffi::OsString::from(val) - }); - &*DEFAULT_VALUE + } }) }; @@ -674,10 +662,7 @@ impl Attrs { } - static DEFAULT_VALUES: clap::__macro_refs::once_cell::sync::Lazy> = clap::__macro_refs::once_cell::sync::Lazy::new(|| { - iter_to_vals(#expr) - }); - DEFAULT_VALUES.iter().copied() + iter_to_vals(#expr) } }) } else { @@ -691,14 +676,7 @@ impl Attrs { } - static DEFAULT_OS_STRINGS: clap::__macro_refs::once_cell::sync::Lazy> = clap::__macro_refs::once_cell::sync::Lazy::new(|| { - iter_to_vals(#expr) - }); - - static DEFAULT_VALUES: clap::__macro_refs::once_cell::sync::Lazy> = clap::__macro_refs::once_cell::sync::Lazy::new(|| { - DEFAULT_OS_STRINGS.iter().map(::std::ffi::OsString::as_os_str).collect() - }); - DEFAULT_VALUES.iter().copied() + iter_to_vals(#expr) } }) }; diff --git a/src/lib.rs b/src/lib.rs index d15b969814d..37aa246f0ba 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -131,7 +131,7 @@ pub mod _tutorial; #[doc(hidden)] pub mod __macro_refs { - #[cfg(any(feature = "derive", feature = "cargo"))] + #[cfg(feature = "cargo")] #[doc(hidden)] pub use once_cell; }