use crate::Context;
use opentelemetry::KeyValue;
pub trait AttributesFactory<State>: Send + Sync + 'static {
fn attributes(&self, size_hint: usize, ctx: &Context<State>) -> Vec<KeyValue>;
}
impl<State> AttributesFactory<State> for () {
fn attributes(&self, size_hint: usize, _ctx: &Context<State>) -> Vec<KeyValue> {
Vec::with_capacity(size_hint)
}
}
impl<State> AttributesFactory<State> for Vec<KeyValue> {
fn attributes(&self, size_hint: usize, _ctx: &Context<State>) -> Vec<KeyValue> {
let mut attributes = self.clone();
attributes.reserve(size_hint);
attributes
}
}
impl<State, F> AttributesFactory<State> for F
where
F: Fn(usize, &Context<State>) -> Vec<KeyValue> + Send + Sync + 'static,
{
fn attributes(&self, size_hint: usize, ctx: &Context<State>) -> Vec<KeyValue> {
(self)(size_hint, ctx)
}
}