In one sentence: Rust's orphan rule is not really "either the trait or the type in
impl Trait for Typemust be yours." Precisely, it is "scanning the Self type and then the trait's type arguments, no bare type parameter may appear before the first local type." If a library leaves a spare type parameter such asCtxon its trait, users can hang someone else's types off someone else's trait by using their own marker type as the "stem."
Encode<'a, X = ()> / Decode<X = ()>) so that users implement them for uuid or chrono types through their own marker type AppCodec.fn_delegation, RFC 3530). Some commenters also criticized the post as reading like AI-generated text.The classic case: a serializer crate defines ToJson, the uuid crate defines Uuid, and your app wants to glue them together.
impl ToJson for uuid::Uuid { // both foreign
fn to_json(&self) -> String { format!("\"{}\"", self) }
}
// error[E0117]: only traits defined in the current crate can be
// implemented for types defined outside of the crate
Why is this forbidden? If crate A could emit hyphenated UUIDs and crate B 32 raw hex digits with the same impl, a program depending on both could not decide which uuid.to_json() should call. Rust guarantees coherence (exactly one implementation per trait/type pair), and the orphan rule is how it enforces it. The Reference also names the goals of preventing builds from breaking just because a dependency was added or updated, and letting library authors add impls without breaking downstream code.
The two common workarounds both hurt.
with-uuid, with-chrono, with-jiff, … feature-flag approach. It tries to enumerate an open ecosystem inside one crate, so support always lags, you are locked to the other crate's major versions, and users cannot change the behavior (for example ISO-8601 strings vs. integer milliseconds for timestamps).struct MyUuid(pub Uuid). Fine for a couple of types, but wrapping a whole domain model in MyUuid, MyUrl, MyDateTime, … litters the code with .0 and hand-written method delegation. And you still cannot plug your impl into places where the foreign library itself handles a Uuid.The Reference defines it like this. Given impl<P1..=Pn> Trait<T1..=Tn> for T0, the impl is valid only if at least one of the following is true:
Trait is a local trait, orT0..=Tn is a local type. Let Ti be the first such type.P1..=Pn appear in T0..Ti (excluding Ti).The key point is that the scan covers not only the Self type T0 but also the trait's type arguments T1..Tn. So
pub struct MyApp; // a type from your crate
impl ToJson<MyApp> for uuid::Uuid { … } // OK: trait and Uuid are foreign, but MyApp is local
compiles. No other crate knows MyApp, so nobody else can write a conflicting impl.