This error indicates a violation of one of Rust’s orphan rules for trait implementations. The rule concerns the use of type parameters in an implementation of a foreign trait (a trait defined in another crate), and states that type parameters must be “covered” by a local type.
When implementing a foreign trait for a foreign type, the trait must have one or more type parameters. A type local to your crate must appear before any use of any type parameters.
To understand what this means, it is perhaps easier to consider a few examples.
If ForeignTrait
is a trait defined in some external crate foo
, then the
following trait impl
is an error:
extern crate foo;
use foo::ForeignTrait;
impl<T> ForeignTrait for T { } // error
RunTo work around this, it can be covered with a local type, MyType
:
struct MyType<T>(T);
impl<T> ForeignTrait for MyType<T> { } // Ok
RunPlease note that a type alias is not sufficient.
For another example of an error, suppose there’s another trait defined in foo
named ForeignTrait2
that takes two type parameters. Then this impl
results
in the same rule violation:
struct MyType2;
impl<T> ForeignTrait2<T, MyType<T>> for MyType2 { } // error
RunThe reason for this is that there are two appearances of type parameter T
in
the impl
header, both as parameters for ForeignTrait2
. The first appearance
is uncovered, and so runs afoul of the orphan rule.
Consider one more example:
impl<T> ForeignTrait2<MyType<T>, T> for MyType2 { } // Ok
RunThis only differs from the previous impl
in that the parameters T
and
MyType<T>
for ForeignTrait2
have been swapped. This example does not
violate the orphan rule; it is permitted.
To see why that last example was allowed, you need to understand the general
rule. Unfortunately this rule is a bit tricky to state. Consider an impl
:
impl<P1, ..., Pm> ForeignTrait<T1, ..., Tn> for T0 { ... }
Runwhere P1, ..., Pm
are the type parameters of the impl
and T0, ..., Tn
are types. One of the types T0, ..., Tn
must be a local type (this is another
orphan rule, see the explanation for E0117).
Both of the following must be true:
T0..=Tn
must be a local type.
Let Ti
be the first such type.P1..=Pm
may appear in T0..Ti
(excluding Ti
).For information on the design of the orphan rules, see RFC 2451 and RFC 1023.
For information on the design of the orphan rules, see RFC 1023.