Skip to content

Commit

Permalink
refactor: Switch to once_cell
Browse files Browse the repository at this point in the history
Though this is changing an API item we export, we do not consider this a
breaking change because
- This was an implementation detail of the macros and people shouldn't be using it directly
- The `macro_rules` macro is coupled to `clap` because they are in the
  same crate
- The derive macro is coupled to `clap` because `clap` declares a
  `=x.y.z` dependency on `clap_derive

Fixes clap-rs#3828
  • Loading branch information
epage committed Jun 14, 2022
1 parent b541f2c commit cb42df6
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 23 deletions.
6 changes: 3 additions & 3 deletions Cargo.toml
Expand Up @@ -73,8 +73,8 @@ color = ["atty", "termcolor"]
suggestions = ["strsim"]

# Optional
derive = ["clap_derive", "lazy_static"]
cargo = ["lazy_static"] # Disable if you're not using Cargo, enables Cargo-env-var-dependent macros
derive = ["clap_derive", "once_cell"]
cargo = ["once_cell"] # Disable if you're not using Cargo, enables Cargo-env-var-dependent macros
wrap_help = ["terminal_size", "textwrap/terminal_size"]
yaml = ["yaml-rust"]
env = [] # Use environment variables during arg parsing
Expand All @@ -101,9 +101,9 @@ yaml-rust = { version = "0.4.1", optional = true }
atty = { version = "0.2", optional = true }
termcolor = { version = "1.1.1", optional = true }
terminal_size = { version = "0.1.12", optional = true }
lazy_static = { version = "1", optional = true }
regex = { version = "1.0", optional = true }
backtrace = { version = "0.3", optional = true }
once_cell = { version = "1.12.0", optional = true }

[dev-dependencies]
regex = "1.0"
Expand Down
26 changes: 10 additions & 16 deletions clap_derive/src/attrs.rs
Expand Up @@ -552,14 +552,11 @@ impl Attrs {
})
} else {
quote_spanned!(ident.span()=> {
clap::lazy_static::lazy_static! {
static ref DEFAULT_VALUE: &'static str = {
let val: #ty = #val;
let s = ::std::string::ToString::to_string(&val);
::std::boxed::Box::leak(s.into_boxed_str())
};
}
*DEFAULT_VALUE
static DEFAULT_VALUE: clap::once_cell::sync::Lazy<String> = clap::once_cell::sync::Lazy::new(|| {
let val: #ty = #val;
::std::string::ToString::to_string(&val)
});
&*DEFAULT_VALUE
})
};

Expand Down Expand Up @@ -595,14 +592,11 @@ impl Attrs {
})
} else {
quote_spanned!(ident.span()=> {
clap::lazy_static::lazy_static! {
static ref DEFAULT_VALUE: &'static ::std::ffi::OsStr = {
let val: #ty = #val;
let s: ::std::ffi::OsString = val.into();
::std::boxed::Box::leak(s.into_boxed_os_str())
};
}
*DEFAULT_VALUE
static DEFAULT_VALUE: clap::once_cell::sync::Lazy<::std::ffi::OsString> = clap::once_cell::sync::Lazy::new(|| {
let val: #ty = #val;
::std::ffi::OsString = val.into()
});
&*DEFAULT_VALUE
})
};

Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Expand Up @@ -72,7 +72,7 @@ pub use ValueEnum as ArgEnum;

#[cfg(any(feature = "derive", feature = "cargo"))]
#[doc(hidden)]
pub use lazy_static;
pub use once_cell;

#[macro_use]
#[allow(missing_docs)]
Expand Down
5 changes: 2 additions & 3 deletions src/macros.rs
Expand Up @@ -235,9 +235,8 @@ macro_rules! crate_version {
#[macro_export]
macro_rules! crate_authors {
($sep:expr) => {{
clap::lazy_static::lazy_static! {
static ref CACHED: String = env!("CARGO_PKG_AUTHORS").replace(':', $sep);
}
static CACHED: clap::once_cell::sync::Lazy<String> =
clap::once_cell::sync::Lazy::new(|| env!("CARGO_PKG_AUTHORS").replace(':', $sep));

let s: &'static str = &*CACHED;
s
Expand Down

0 comments on commit cb42df6

Please sign in to comment.