metrale_bench/
dynamic.rs

1// SPDX-License-Identifier: MIT OR Apache-2.0
2
3//! 2026-09-26: [`DynBenchmark`], the object-safe form of [`Benchmark`].
4//!
5//! [`Benchmark`] and [`Plugin`] return `impl Future` / `impl Stream`, which
6//! makes them not dyn-compatible, so descriptors construct
7//! `Box<dyn DynBenchmark>` instead. The blanket impl below gives every
8//! `Benchmark + Send` this trait with boxed futures. `Benchmark::run` has no
9//! counterpart here: callers drive a `DynBenchmark` with `executor::drive`.
10//!
11//! Owner: bench.
12//! Invariants: none beyond the types.
13
14use anyhow::Result;
15use futures::future::BoxFuture;
16
17use crate::benchmark::{Benchmark, BenchmarkDescriptor};
18use crate::metadata::PluginMetadata;
19use crate::params::{ParamSpec, ParamValues};
20use crate::plugin::{Plugin, PluginHandle};
21use crate::result::BenchmarkResult;
22
23/// 2026-09-26: Object-safe form of [`Benchmark`] + [`Plugin`]; each method
24/// forwards to the trait method of the same name.
25pub trait DynBenchmark: Send {
26    fn descriptor(&self) -> &'static BenchmarkDescriptor;
27    fn metadata(&self) -> &'static PluginMetadata;
28    fn parameters(&self) -> Vec<ParamSpec>;
29    fn configure(&mut self, values: &ParamValues) -> Result<()>;
30    fn load<'a>(&'a mut self, handle: PluginHandle) -> BoxFuture<'a, Result<()>>;
31    fn next<'a>(&'a mut self) -> BoxFuture<'a, Result<BenchmarkResult>>;
32    fn cleanup<'a>(&'a mut self) -> BoxFuture<'a, Result<()>>;
33}
34
35impl<T> DynBenchmark for T
36where
37    T: Benchmark + Send,
38{
39    fn descriptor(&self) -> &'static BenchmarkDescriptor {
40        Benchmark::descriptor(self)
41    }
42    fn metadata(&self) -> &'static PluginMetadata {
43        Plugin::metadata(self)
44    }
45    fn parameters(&self) -> Vec<ParamSpec> {
46        Benchmark::parameters(self)
47    }
48    fn configure(&mut self, values: &ParamValues) -> Result<()> {
49        Benchmark::configure(self, values)
50    }
51    fn load<'a>(&'a mut self, handle: PluginHandle) -> BoxFuture<'a, Result<()>> {
52        Box::pin(Plugin::load(self, handle))
53    }
54    fn next<'a>(&'a mut self) -> BoxFuture<'a, Result<BenchmarkResult>> {
55        Box::pin(Benchmark::next(self))
56    }
57    fn cleanup<'a>(&'a mut self) -> BoxFuture<'a, Result<()>> {
58        Box::pin(Benchmark::cleanup(self))
59    }
60}