use std::ptr::NonNull;
use crate::{bench::BenchArgsRunner, Bencher};
mod generic;
mod list;
mod meta;
mod tree;
pub use self::{
generic::{EntryConst, EntryType, GenericBenchEntry},
list::EntryList,
meta::{EntryLocation, EntryMeta},
};
pub(crate) use tree::EntryTree;
pub static BENCH_ENTRIES: EntryList<BenchEntry> = EntryList::root();
pub static GROUP_ENTRIES: EntryList<GroupEntry> = EntryList::root();
#[derive(Clone, Copy)]
pub enum BenchEntryRunner {
Plain(fn(Bencher)),
Args(fn() -> BenchArgsRunner),
}
pub struct BenchEntry {
pub meta: EntryMeta,
pub bench: BenchEntryRunner,
}
pub struct GroupEntry {
pub meta: EntryMeta,
pub generic_benches: Option<&'static [&'static [GenericBenchEntry]]>,
}
impl GroupEntry {
pub(crate) fn generic_benches_iter(&self) -> impl Iterator<Item = &'static GenericBenchEntry> {
self.generic_benches.unwrap_or_default().iter().flat_map(|benches| benches.iter())
}
}
#[derive(Clone, Copy)]
pub(crate) enum AnyBenchEntry<'a> {
Bench(&'a BenchEntry),
GenericBench(&'a GenericBenchEntry),
}
impl<'a> AnyBenchEntry<'a> {
#[inline]
pub fn entry_addr(self) -> NonNull<()> {
match self {
Self::Bench(entry) => NonNull::from(entry).cast(),
Self::GenericBench(entry) => NonNull::from(entry).cast(),
}
}
#[inline]
pub fn bench_runner(self) -> &'a BenchEntryRunner {
match self {
Self::Bench(BenchEntry { bench, .. })
| Self::GenericBench(GenericBenchEntry { bench, .. }) => bench,
}
}
#[inline]
pub fn arg_names(self) -> Option<&'static [&'static str]> {
match self.bench_runner() {
BenchEntryRunner::Args(bench_runner) => {
let bench_runner = bench_runner();
Some(bench_runner.arg_names())
}
_ => None,
}
}
#[inline]
pub fn meta(self) -> &'a EntryMeta {
match self {
Self::Bench(entry) => &entry.meta,
Self::GenericBench(entry) => &entry.group.meta,
}
}
#[inline]
pub fn raw_name(self) -> &'a str {
match self {
Self::Bench(entry) => entry.meta.raw_name,
Self::GenericBench(entry) => entry.raw_name(),
}
}
#[inline]
pub fn display_name(self) -> &'a str {
match self {
Self::Bench(entry) => entry.meta.display_name,
Self::GenericBench(entry) => entry.display_name(),
}
}
}