Skip to content

Commit

Permalink
use a feature flag rather than conditional compilation
Browse files Browse the repository at this point in the history
  • Loading branch information
ironpeak committed Nov 27, 2024
1 parent d62d16e commit 6a72d2a
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 50 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ license = "MIT"

[features]
default = []
slow_function_warning = []

[lib]
proc-macro = true
Expand Down
40 changes: 5 additions & 35 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,47 +30,17 @@ fn example_function() {
}
```

## Debug Only Example
The warning is not on by default and is only recommended for debugging purposes. To enable it use the `slow_function_warning` feature.

```rust
#[cfg_attr(debug_assertions, slow_function_warning(1000ms))]
fn example_function() {
// Function implementation
}
```

Or using the convenience proc macro:

```rust
#[debug_slow_function_warning(1000ms)]
fn example_function() {
// Function implementation
}
```

## Release Only Example

```rust
#[cfg_attr(not(debug_assertions), slow_function_warning(1000ms))]
fn example_function() {
// Function implementation
}
```

Or using the convenience proc macro:

```rust
#[release_slow_function_warning(1000ms)]
fn example_function() {
// Function implementation
}
```
~~~bash
cargo run --features slow_function_warning
~~~

## Custom Message Example

```rust
// Warn if the function takes longer than a second with a custom message
#[debug_slow_function_warning(1s, println!("Function {function} took too long!"))]
#[slow_function_warning(1s, println!("Function {function} took too long!"))]
fn example_function() {
// Function implementation
}
Expand Down
17 changes: 2 additions & 15 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,23 +34,10 @@ fn parse_time(expr: &Expr) -> Result<Duration> {
}

#[proc_macro_attribute]
pub fn debug_slow_function_warning(args: TokenStream, input: TokenStream) -> TokenStream {
if !cfg!(debug_assertions) {
return input;
}
slow_function_warning(args, input)
}

#[proc_macro_attribute]
pub fn release_slow_function_warning(args: TokenStream, input: TokenStream) -> TokenStream {
if cfg!(debug_assertions) {
pub fn slow_function_warning(args: TokenStream, input: TokenStream) -> TokenStream {
if !cfg!(feature = "slow_function_warning") {
return input;
}
slow_function_warning(args, input)
}

#[proc_macro_attribute]
pub fn slow_function_warning(args: TokenStream, input: TokenStream) -> TokenStream {
let args: Punctuated<Expr, Token![,]> = if !args.is_empty() {
parse_macro_input!(args with Punctuated::<Expr, Token![,]>::parse_separated_nonempty)
.into_iter()
Expand Down

0 comments on commit 6a72d2a

Please sign in to comment.