metered-rs icon indicating copy to clipboard operation
metered-rs copied to clipboard

Add support cfg_attr

Open helgoboss opened this issue 4 years ago • 1 comments

First: Extremely practical crate! Thanks!

I wanted to use Cargo features and cfg_attr in order to be able to activate/deactivate the compilation of metering or change which set of functions is going to be measured. However, I ran into the problem that cfg_attr complained about measure not being an attribute.

Example:

#[cfg_attr(feature = "measure-audio", measure)]
pub fn my_audio_function() {
    todo!()
}

Is there any way to fix it? E.g. to make measure an actual attribute?

helgoboss avatar May 15 '20 11:05 helgoboss

Current workaround which helps at least for being able to completly disable compilation with metered: Create a fake attribute macro called measure in a proc-macro crate, in this example named "my-macros":

/// No-op macro as work-around for https://github.com/magnet/metered-rs/issues/23.
#[doc(hidden)]
#[proc_macro_attribute]
pub fn measure(_: TokenStream, input: TokenStream) -> TokenStream {
    input
}

And use it when measuring is disabled:

#[cfg(feature = "measure-audio")]
use metered::{metered, ResponseTime};
#[cfg(not(feature = "measure-audio"))]
use my_macros::measure;

// ...

helgoboss avatar May 15 '20 12:05 helgoboss