I have a fairly complex trait set up and I'm having trouble lining the pieces up. Right now it looks roughly like this:
/// Trait for models which can be gradient-optimized.
pub trait Optimizable {
type Data;
type Target;
// The contract //
}
/// Trait for optimization algorithms.
pub trait OptimAlgorithm<M : Optimizable> {
// The contract //
}
Now I want to be able to allow a struct implementing OptimAlgorithm
to be a field in a struct implementing Optimizable
. This would look something like this:
/// Model struct
pub struct Model<A: OptimAlgorithm<Self>> {
alg: A,
}
impl Optimizable for Model<A> {
...
}
This doesn't work as the Self
reference on the struct is nonsense. I tried using associated types for OptimAlgorithm
but I need the algorithms to be generic over the models so this doesn't work. Is there a magic syntax I'm missing or does this need an overhaul?
Edit --
Here's a minimal example which shows error E0275 as described in Steven's answer. It's a little closer to my source code but less messy.