1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184
/*!
A simple counter for debugging.
Basic usage of the crate is calling [`pass`], with each
call incrementing a static counter. The counter starts at 0, and
has a max of [`usize::MAX`].
## Examples
Run without arguments to get basic debug information:
```rust
pass!(); // Stderr: [<count>][<line_number>]
```
You can pass a literal to have it printed:
```rust
// Stderr: [<count>][<line_number>] Special message
pass!("Special message");
// Stderr: [<count>][<line_number>] a
pass!('a');
// Stderr: [<count>][<line_number>] 5999999
pass(5999999);
```
You can pass a variable with the [`Display`] trait too:
```rust
let hw = String::from("Hello world");
let num = 5;
// Stderr: [<count>][<line_number>] Hello world
pass!(hw);
// Stderr: [<count>][<line_number>] 5
pass!(num);
```
[`Display`]: std::fmt::Display
*/
static mut COUNTER: usize = 0;
/**
Prints count information to stderr.
The macro takes an optional message argument, either
as a literal or a variable that has the [`Display`] trait.
## Examples
Run without arguments to get basic debug information:
```rust
pass!(); // Stderr: [<count>][<line_number>]
```
You can pass a literal to have it printed:
```rust
// Stderr: [<count>][<line_number>] Special message
pass!("Special message");
// Stderr: [<count>][<line_number>] a
pass!('a');
// Stderr: [<count>][<line_number>] 5999999
pass(5999999);
```
You can pass a variable with the [`Display`] trait too:
```rust
let hw = String::from("Hello world");
let num = 5;
// Stderr: [<count>][<line_number] Hello world
pass!(hw);
// Stderr: [<count>][<line_number] 5
pass!(num);
```
[`Display`]: std::fmt::Display
*/
#[macro_export]
macro_rules! pass {
() => {
let n = crate::increment();
eprintln!("[Pass {}][Line {}]", n, line!());
};
($mess:literal) => {
let n = crate::increment();
eprintln!("[Pass {}][Line {}] {}", n, line!(), $mess);
};
($mess:ident) => {
let n = crate::increment();
eprintln!("[Pass {}][Line {}] {}", n, line!(), $mess);
}
}
/**
Increment counter, return the result.
## Example
```rust
// Assuming the counter starts at 0
increment() // 1
increment() // 2
increment() // 3
```
*/
pub fn increment() -> usize {
unsafe {
COUNTER += 1;
COUNTER
}
}
/**
Get the current counter number without incrementing.
The lowest number this function can return is 0.
## Example
```rust
let count1 = current_count();
let count2 = current_count();
assert_eq!(count1, count2);
```
*/
pub fn current_count() -> usize {
unsafe { COUNTER }
}
/// Resets the counter back to 0.
pub fn reset_counter() {
unsafe { COUNTER = 0 }
}
#[cfg(test)]
mod tests {
use rand::{thread_rng, Rng};
use super::*;
#[test]
fn pass_default() {
pass!();
pass!();
pass!();
pass!();
}
#[test]
fn pass_message() {
pass!("One");
pass!("Two");
pass!("Three");
pass!("Four");
pass!('4');
}
#[test]
fn pass_ident() {
let one = "one";
let two = "two".to_string();
let three = 3;
pass!(one);
pass!(two);
pass!(three);
}
#[test]
fn test_helpers() {
reset_counter();
let r: usize = thread_rng().gen_range(0..1000);
for _ in 0..r {
increment();
}
assert_eq!(r, current_count());
}
}