Skip to content

Commit e755a03

Browse files
committed
Use literal instead of tt in timeit macro_rules
1 parent eab6a8b commit e755a03

File tree

3 files changed

+8
-7
lines changed

3 files changed

+8
-7
lines changed

timeit/Cargo.lock

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

timeit/README.md

+5-5
Original file line numberDiff line numberDiff line change
@@ -112,13 +112,13 @@ With this info, we can see how to add an optional log prefix in the `timeit!` ma
112112
```rust
113113
macro_rules! timeit {
114114
($e:expr) => { /* block from above */ };
115-
// New match rule that recives a `tt` match type (Token), which can be a `str`
116-
($e:expr, $n:tt) => {{
115+
// New match rule that recives a `literal` match type, like a `&str`
116+
($e:expr, $desc:literal) => {{
117117
// This is the same as our previous rule
118118
let _start = std::time::Instant::now();
119119
let _res = $e();
120-
// Except that we now use the `$n` str in our log output
121-
eprintln!("{} took {:.3} ms", $n, _start.elapsed().as_millis());
120+
// Except that we now use the `$desc` str in our log output
121+
eprintln!("{} took {:.3} ms", $desc, _start.elapsed().as_millis());
122122
_res
123123
}};
124124
}
@@ -177,7 +177,7 @@ macro_rules! timeit {
177177
_res
178178
}};
179179
($e:expr) => { /* block from above */ };
180-
($e:expr, $n:tt) => { /* block from above */ };
180+
($e:expr, $desc:literal) => { /* block from above */ };
181181
}
182182
```
183183

timeit/src/lib.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,10 @@ macro_rules! timeit {
5858
// timeit!(my_func, "My Func");
5959
// ```
6060
// > My Func took 2000 ms
61-
($e:expr, $n:tt) => {{
61+
($e:expr, $desc:literal) => {{
6262
let _start = std::time::Instant::now();
6363
let _res = $e();
64-
eprintln!("{} took {:.3} ms", $n, _start.elapsed().as_millis());
64+
eprintln!("{} took {:.3} ms", $desc, _start.elapsed().as_millis());
6565
_res
6666
}};
6767
}

0 commit comments

Comments
 (0)