I develop a generic constraint library and have some difficulties to achieve a truly generic library. Basically, I have a potentially infinite number of constraints that must implement several traits: Propagator, DeepClone,… In my solver, I store these constraints in a vector which has type:
Vec<Box<Propagator+DeepClone+...>>, since this syntax is not possible, I use a trait erasure, let’s call it
ErasurePropagator. If I decomposed operations into several traits, it’s because they are independents. The problem is that
DeepClone has a clone method returning a boxed type, but what should be that type?
Box<Self>doesn’t work because it makes
Box<DeepClone>doesn’t work because we can not cast it to
Box<PropagatorErasure>works but it adds unnecessary coupling between all these traits and
DeepClonewould not be re-usable in others contexts.
I don’t understand why the first solution prevents
DeepClone to be object-safe (
Self is boxed), is there any conflicting requirements? Is it just not implemented yet? Do you have any suggestions to make it work?
A toy example to test things is available here, it implements the first solution.