| 
 | 1 | +//! Handpicked stubs from [divan::entry](https://github.com/nvzqz/divan/blob/main/src/entry/mod.rs)  | 
 | 2 | +//! Minimally reimplemented in an API compatible way to run the benches using codspeed intrumentation  | 
 | 3 | +use codspeed::codspeed::CodSpeed;  | 
 | 4 | +use std::{cell::RefCell, rc::Rc};  | 
 | 5 | + | 
 | 6 | +/// Benchmarking options set directly by the user in `#[divan::bench]` and  | 
 | 7 | +/// `#[divan::bench_group]`.  | 
 | 8 | +///  | 
 | 9 | +/// Changes to fields must be reflected in the "Options" sections of the docs  | 
 | 10 | +/// for `#[divan::bench]` and `#[divan::bench_group]`.  | 
 | 11 | +#[derive(Default)]  | 
 | 12 | +pub struct BenchOptions<'a> {  | 
 | 13 | +    pub(crate) _marker: std::marker::PhantomData<&'a ()>,  | 
 | 14 | +}  | 
 | 15 | + | 
 | 16 | +pub struct Bencher<'a, 'b> {  | 
 | 17 | +    pub(crate) codspeed: Rc<RefCell<CodSpeed>>,  | 
 | 18 | +    pub(crate) uri: String,  | 
 | 19 | +    pub(crate) _marker: std::marker::PhantomData<&'a &'b ()>,  | 
 | 20 | +}  | 
 | 21 | + | 
 | 22 | +#[allow(clippy::needless_lifetimes)]  | 
 | 23 | +impl<'a, 'b> Bencher<'a, 'b> {  | 
 | 24 | +    pub(crate) fn new(uri: String) -> Self {  | 
 | 25 | +        Self {  | 
 | 26 | +            codspeed: Rc::new(RefCell::new(CodSpeed::new())),  | 
 | 27 | +            uri,  | 
 | 28 | +            _marker: std::marker::PhantomData,  | 
 | 29 | +        }  | 
 | 30 | +    }  | 
 | 31 | +}  | 
 | 32 | + | 
 | 33 | +#[allow(clippy::needless_lifetimes)]  | 
 | 34 | +impl<'a, 'b> Bencher<'a, 'b> {  | 
 | 35 | +    pub fn bench<O, B>(&self, benched: B)  | 
 | 36 | +    where  | 
 | 37 | +        B: Fn() -> O + Sync,  | 
 | 38 | +    {  | 
 | 39 | +        let mut codspeed = self.codspeed.borrow_mut();  | 
 | 40 | +        codspeed.start_benchmark(self.uri.as_str());  | 
 | 41 | +        divan::black_box(benched());  | 
 | 42 | +        codspeed.end_benchmark();  | 
 | 43 | +    }  | 
 | 44 | +}  | 
0 commit comments