Why doesn't the relaxed orphan rule work with multiple generic levels?

I'm trying to do

impl<'a> RObject<'a> {...}

where RObject is defined as type RObject<'a> = Arc<RwLock<Object<'a>>>. According to Rust 1.41.0 release notes, impl blocks for foreign types parametrized by local types are allowed, but that snippet fails with the following error:

cannot define inherent `impl` for a type outside of the crate where the type is defined

impl for type defined outside of crate.

note: define and implement a trait or new type instead

I'm using the latest nightly compiler (x86_64, Windows, GNU, if that matters), but also tried to use the latest stable.

2 Likes

I don't see anything to this effect. All I see is the re-rebalancing coherence change, which increases cases where type parameters are allowed to appear in trait impls. (yours is not a trait impl, nor does it have a type parameter)

Note that in the eyes of the compiler T<'a> == T<'b> in terms of orphan rules and permitting implementations.

Excuse me for butting in here. But I have been programming in all kind of languages since 1980. From ALGOL to Javascript.

Sadly I have no idea what the above is talking about.

Is there something I should know here as a Rust newbie or should I go back to C/C++?

Sorry, I should have linked the re-rebalancing coherence RFC (that's just what it was called). I assumed that the reader of my message would be looking at the 1.41 release notes, where this can be easily found from the first thing linked.

No, it's not something you need to know about. All it did was just make the language easier to use by making more things work the way most users already expected them to.

(if, on the other hand, it's the phrase "cases where type parameters are allowed to appear in trait impls" that created confusion... yes, you might want to go read The Rust Programming Language, namely the section on Generics)

2 Likes

2 posts were split to a new topic: Parts of Rust that I still don't get after all this time

Do I need to refactor it or wrap it into a newtype then? That's sad, thanks for pointing out.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.