Skip to content

Commit

Permalink
Merge clap-rs#2092
Browse files Browse the repository at this point in the history
2092: Make caching in crate_authors! actually work r=pksunkara a=CreepySkeleton



Co-authored-by: CreepySkeleton <creepy-skeleton@yandex.ru>
  • Loading branch information
bors[bot] and CreepySkeleton committed Aug 21, 2020
2 parents ed3bb99 + 28ad25f commit 317a816
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 37 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ color = ["atty", "termcolor"]
wrap_help = ["terminal_size", "textwrap/terminal_size"]
derive = ["clap_derive", "lazy_static"]
yaml = ["yaml-rust"]
cargo = [] # Disable if you're not using Cargo, enables Cargo-env-var-dependent macros
cargo = ["lazy_static"] # Disable if you're not using Cargo, enables Cargo-env-var-dependent macros
unstable = ["clap_derive/unstable"] # for building with unstable clap features (doesn't require nightly Rust) (currently none)
debug = ["clap_derive/debug"] # Enables debug messages
doc = ["yaml"] # All the features which add to documentation
Expand Down
4 changes: 2 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ pub use yaml_rust::YamlLoader;
#[cfg_attr(feature = "derive", doc(hidden))]
pub use clap_derive::{self, *};

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

#[macro_use]
Expand Down
38 changes: 4 additions & 34 deletions src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,42 +80,12 @@ macro_rules! crate_version {
#[macro_export]
macro_rules! crate_authors {
($sep:expr) => {{
use std::ops::Deref;
use std::boxed::Box;
use std::cell::Cell;

#[allow(missing_copy_implementations)]
#[allow(unused)]
struct CargoAuthors {
authors: Cell<Option<&'static str>>,
__private_field: (),
};

impl Deref for CargoAuthors {
type Target = str;

fn deref(&self) -> &'static str {
let authors = self.authors.take();
if authors.is_some() {
let unwrapped_authors = authors.unwrap();
self.authors.replace(Some(unwrapped_authors));
unwrapped_authors
} else {
// This caches the result for subsequent invocations of the same instance of the macro
// to avoid performing one memory allocation per call.
// If performance ever becomes a problem for this code, it should be moved to build.rs
let s: Box<String> = Box::new(env!("CARGO_PKG_AUTHORS").replace(':', $sep));
let static_string = Box::leak(s);
self.authors.replace(Some(&*static_string));
&*static_string // weird but compiler-suggested way to turn a String into &str
}
}
clap::lazy_static::lazy_static! {
static ref CACHED: String = env!("CARGO_PKG_AUTHORS").replace(':', $sep);
}

&*CargoAuthors {
authors: std::cell::Cell::new(Option::None),
__private_field: (),
}
let s: &'static str = &*CACHED;
s
}};
() => {
env!("CARGO_PKG_AUTHORS")
Expand Down

0 comments on commit 317a816

Please sign in to comment.