impl<A> Trait1 for A{
fn myfunc(&mut self){
.................
<A as Trait2>::fn(self) //if and only if A:Trait2
.................
}
}
i think i can probably find a workaround to do that but i was wondering if anyone knows of a clean maintainable solution for something like this ideally without runtime costs
yeah but that's not the problem, what i want is to reuse the rest of the function for instances where A is not Trait2 and also avoid conflicts from overlapping impls
I think OP wants something like conditional bounds, or specialization, guessing from the comment "if and only if ...".
if I understand your question correctly, here, by "runtime costs", you probably mean something like trait object downcasting, or TypeId checking?
the "cleanest" solution would be specialization, but it's an incomplete feature and doesn't work well. a working subset is called min_specialization, but it's also nightly only.
if Trait2 is dyn-compatible, I think the IDET pattern might be a "not so clean" workaround, see:
The only workaround I know is to create empty default for Trait2 (for things which are not really Trait2) and declare the real implementation for things which are real Trait2.
Like this, the bound for Trait2 is always true and the function can always call the Trait2 methods.