Search code examples
rusttraitsimplementationworkspace

Could rule "only traits defined in the current crate can be implemented for arbitrary types" be extended to workspace?


I tested rule "only traits defined in the current crate can be implemented for arbitrary types" in the case of crates defined in a same workspace:

MyWorkSpace-|
            |-MyCrateA--> pub MyTrait {}
            |
            |-MyCrateB--> pub MyStruct;
            |
            |-MyCrateC--> impl MyTrait for MyStruct {}

And as expected, it did not work...

I understood that this rule avoids collisions between impls regarding the dependencies.

However, in the case of a workspace, the owner has control over the crates within it. So why is it not possible to have a rule "only traits defined in the current workspace can be implemented for arbitrary types"?


Solution

  • Workspaces are a Cargo concept. rustc knows nothing about them.

    Also, the demand for this feature is very low. If the crates are only separate for performance reasons, you can always unify them if required. If they're separate because of API concerns, then the orphan rules should probably still hold.

    I don't think this whether this feature was ever suggested (you can try, although I doubt it'll be accepted), but it wasn't accepted/implemented.

    Crates should be an API boundary. If you need to break the orphan rules inside a workspace, you're probably misusing workspaces.