I'm currently trying to reduce code duplication and ran into a little problem:
I have a few (local) types containing some
&Bar. Now lets say I want to implement the foreign trait
core::ops::Index for all these types and the implementation only relies on the value of
The solution I thought of was creating two traits
trait GetFoo and
trait GetBar which contain a function to get the value
Now I thought I could implement
core::ops::Index for T where T: GetFoo + GetBar, but the compiler gives me the error message: "type parameter
T must be used as the type parameter for some local type".
So here my two questions:
I do understand why its not possible to implement foreign traits for arbitrary types, but shouldn't this case be fine, since the type parameter
T is bound by a local, non public Trait?
Is there any way to implement this without having to write all functions for every type (and possibly delegating them somewhere)?
I asked this question on the discord also (thanks everyone there) and someone had the idea that I could try to implement
Deref<Target=Bar> for my types and use this as a trait bound. This failed in my actual use case with the error message of conflicting implemetations of
Index for the type
Vec (the type
T was obviously not bound to be from my crate anymore).