Skip to content

Commit dd3763f

Browse files
committed
feat(criterion): allow explicit lifetime usage with compat
1 parent 40f11b2 commit dd3763f

File tree

2 files changed

+31
-9
lines changed

2 files changed

+31
-9
lines changed

crates/criterion_compat/benches/test_benches.rs

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use codspeed_criterion_compat::{criterion_group, criterion_main, BenchmarkId, Criterion};
1+
use codspeed_criterion_compat::{criterion_group, criterion_main, Bencher, BenchmarkId, Criterion};
22

33
fn bench(c: &mut Criterion) {
44
// Setup (construct data, allocate memory, etc)
@@ -14,6 +14,23 @@ fn bench(c: &mut Criterion) {
1414
});
1515
}
1616

17+
fn bench_with_explicit_lifetime(c: &mut Criterion) {
18+
let input = 5u64;
19+
c.bench_with_input(
20+
BenchmarkId::new("with_input", input),
21+
&input,
22+
|b: &mut Bencher<'_>, i| {
23+
b.iter(|| {
24+
let mut x = 0;
25+
for _ in 0..*i {
26+
x += 2;
27+
}
28+
x
29+
})
30+
},
31+
);
32+
}
33+
1734
mod nested {
1835
use super::*;
1936
pub fn bench(c: &mut Criterion) {
@@ -31,5 +48,5 @@ mod nested {
3148
}
3249
}
3350

34-
criterion_group!(benches, bench, nested::bench);
51+
criterion_group!(benches, bench, bench_with_explicit_lifetime, nested::bench);
3552
criterion_main!(benches);

crates/criterion_compat/src/compat/bencher.rs

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,19 @@ use criterion::async_executor::AsyncExecutor;
99
#[cfg(feature = "async")]
1010
use std::future::Future;
1111

12-
pub struct Bencher {
12+
pub struct Bencher<'a> {
1313
codspeed: Rc<RefCell<CodSpeed>>,
1414
uri: String,
15+
_marker: std::marker::PhantomData<&'a ()>,
1516
}
1617

17-
impl Bencher {
18+
impl<'a> Bencher<'a> {
1819
pub fn new(codspeed: Rc<RefCell<CodSpeed>>, uri: String) -> Self {
19-
Bencher { codspeed, uri }
20+
Bencher {
21+
codspeed,
22+
uri,
23+
_marker: std::marker::PhantomData,
24+
}
2025
}
2126

2227
#[inline(never)]
@@ -120,19 +125,19 @@ impl Bencher {
120125
}
121126

122127
#[cfg(feature = "async")]
123-
pub fn to_async<A: AsyncExecutor>(&mut self, runner: A) -> AsyncBencher<A> {
128+
pub fn to_async<'b, A: AsyncExecutor>(&'b mut self, runner: A) -> AsyncBencher<'a, 'b, A> {
124129
AsyncBencher { b: self, runner }
125130
}
126131
}
127132

128133
#[cfg(feature = "async")]
129-
pub struct AsyncBencher<'b, A: AsyncExecutor> {
130-
b: &'b mut Bencher,
134+
pub struct AsyncBencher<'a, 'b, A: AsyncExecutor> {
135+
b: &'b mut Bencher<'a>,
131136
runner: A,
132137
}
133138

134139
#[cfg(feature = "async")]
135-
impl<'b, A: AsyncExecutor> AsyncBencher<'b, A> {
140+
impl<'a, 'b, A: AsyncExecutor> AsyncBencher<'a, 'b, A> {
136141
#[allow(clippy::await_holding_refcell_ref)]
137142
#[inline(never)]
138143
pub fn iter<O, R, F>(&mut self, mut routine: R)

0 commit comments

Comments
 (0)