diff --git a/crates/criterion_compat/benches/test_benches.rs b/crates/criterion_compat/benches/test_benches.rs index 8d84e24e..4fcd0b39 100644 --- a/crates/criterion_compat/benches/test_benches.rs +++ b/crates/criterion_compat/benches/test_benches.rs @@ -1,4 +1,4 @@ -use codspeed_criterion_compat::{criterion_group, criterion_main, BenchmarkId, Criterion}; +use codspeed_criterion_compat::{criterion_group, criterion_main, Bencher, BenchmarkId, Criterion}; fn bench(c: &mut Criterion) { // Setup (construct data, allocate memory, etc) @@ -14,6 +14,23 @@ fn bench(c: &mut Criterion) { }); } +fn bench_with_explicit_lifetime(c: &mut Criterion) { + let input = 5u64; + c.bench_with_input( + BenchmarkId::new("with_input", input), + &input, + |b: &mut Bencher<'_>, i| { + b.iter(|| { + let mut x = 0; + for _ in 0..*i { + x += 2; + } + x + }) + }, + ); +} + mod nested { use super::*; pub fn bench(c: &mut Criterion) { @@ -31,5 +48,5 @@ mod nested { } } -criterion_group!(benches, bench, nested::bench); +criterion_group!(benches, bench, bench_with_explicit_lifetime, nested::bench); criterion_main!(benches); diff --git a/crates/criterion_compat/src/compat/bencher.rs b/crates/criterion_compat/src/compat/bencher.rs index 53568d94..501e6d26 100644 --- a/crates/criterion_compat/src/compat/bencher.rs +++ b/crates/criterion_compat/src/compat/bencher.rs @@ -9,14 +9,19 @@ use criterion::async_executor::AsyncExecutor; #[cfg(feature = "async")] use std::future::Future; -pub struct Bencher { +pub struct Bencher<'a> { codspeed: Rc>, uri: String, + _marker: std::marker::PhantomData<&'a ()>, } -impl Bencher { +impl<'a> Bencher<'a> { pub fn new(codspeed: Rc>, uri: String) -> Self { - Bencher { codspeed, uri } + Bencher { + codspeed, + uri, + _marker: std::marker::PhantomData, + } } #[inline(never)] @@ -120,19 +125,19 @@ impl Bencher { } #[cfg(feature = "async")] - pub fn to_async(&mut self, runner: A) -> AsyncBencher { + pub fn to_async<'b, A: AsyncExecutor>(&'b mut self, runner: A) -> AsyncBencher<'a, 'b, A> { AsyncBencher { b: self, runner } } } #[cfg(feature = "async")] -pub struct AsyncBencher<'b, A: AsyncExecutor> { - b: &'b mut Bencher, +pub struct AsyncBencher<'a, 'b, A: AsyncExecutor> { + b: &'b mut Bencher<'a>, runner: A, } #[cfg(feature = "async")] -impl<'b, A: AsyncExecutor> AsyncBencher<'b, A> { +impl<'a, 'b, A: AsyncExecutor> AsyncBencher<'a, 'b, A> { #[allow(clippy::await_holding_refcell_ref)] #[inline(never)] pub fn iter(&mut self, mut routine: R)