use crate::Sample;
use std::{fmt::Debug, ops::Div};
pub trait ClonableFnMut<S, T>: FnMut(&[&S]) -> Option<T> + Send + Sync {
fn clone_box(&self) -> Box<dyn ClonableFnMut<S, T>>;
}
impl<S, T, F> ClonableFnMut<S, T> for F
where
F: FnMut(&[&S]) -> Option<T> + Send + Sync + Clone + 'static,
{
fn clone_box(&self) -> Box<dyn ClonableFnMut<S, T>> {
Box::new(self.clone())
}
}
#[derive(Default)]
pub enum ResamplingFunction<
T: Div<Output = T> + std::iter::Sum + Default + Debug,
S: Sample<Value = T>,
> {
#[default]
Average,
Sum,
Max,
Min,
First,
Last,
Coalesce,
Count,
Custom(Box<dyn ClonableFnMut<S, T>>),
}
impl<T, S> Clone for ResamplingFunction<T, S>
where
T: Div<Output = T> + std::iter::Sum + Default + Debug,
S: Sample<Value = T>,
{
fn clone(&self) -> Self {
match self {
Self::Average => Self::Average,
Self::Sum => Self::Sum,
Self::Max => Self::Max,
Self::Min => Self::Min,
Self::First => Self::First,
Self::Last => Self::Last,
Self::Coalesce => Self::Coalesce,
Self::Count => Self::Count,
Self::Custom(f) => Self::Custom(f.clone_box()),
}
}
}