I've got a few different elements I'm trying to put into a struct, each of which has a few options for their implementation, but all their implementations should function similarly. This has lead me to two paths to consider... First, the obvious:
pub enum Coefficient<T: Float> {
fixed(T),
interpolated(Spline<T, T>),
conditional(HashMap<&'static char, T>),
}
Something like that using a single get value function. There's obviously a bit more involved as the get_value(&self)
implementation doesn't provide the argument for interpolating or selection from the HashMap
. But I can put a reference in the interpolated and conditional coefficients back to where it can obtain the needed info at will.
But since it then has a single access point with a fixed signature, I could do something like this instead and implement it for each struct
and avoid the enum
.
trait CoeffTrait<T: Float> {
fn get_value(&self) -> T;
}
impl<T: Float> CoeffTrait<T> for FixedCoefficient<T> {}
// and the others...
That then gives the two options for where these are stored:
pub struct OptionA<T: Float> {
coefficients: Vec<Coefficient<T>>,
}
pub struct OptionB<T: Float, C: CoeffTrait<T>> {
coefficients: Vec<C>,
}
However, if I go the implementation method to avoid enum
s for too many things, I end up at a top level struct with tons generics (on top of a couple lifetimes):
pub struct Env<'env, 'run, T: Float, C: CoeffTrait, Solver: SolveMethods, Run: Runtime> {
objects: Vec<Object<'env, Solver>>,
}
I was initially wary of an enum with most commonly a fixed f64 value sharing other variants that were not sized. But also I really only want to have to specify T when allocating an Env
. Also, I'm starting to think that the cascade of the traits back to the top-level means that I'll need to lock in a coefficient type at environment creation, and also that different objects couldn't have different solve methods or coefficient types (unless I'm missing something). I may have just set myself back to enum
s in this process. But, I'd certainly appreciate any thoughts on the various tradeoffs of how to accomplish something like this.