# Idea: Relaxation of the Orphan Rule

**URL:** <https://internals.rust-lang.org/t/idea-relaxation-of-the-orphan-rule/23127>\
**Category:** language design\
**Created:** [June 23, 2025, 4:27pm UTC](https://internals.rust-lang.org/t/idea-relaxation-of-the-orphan-rule/23127 "2025-06-23T16:27:41Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![SciMind2460](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/scimind2460/32/13125_2.png) [@SciMind2460](https://internals.rust-lang.org/u/SciMind2460)\
**Post date:** [June 23, 2025, 4:27pm UTC](https://internals.rust-lang.org/t/idea-relaxation-of-the-orphan-rule/23127/1 "2025-06-23T16:27:41Z")

</div>

Right now, the orphan rule prevents any external type from being implemented by an external trait. But what if the orphan rules were relaxed to make sure that external types's implementations for traits cannot be publicly visible?

# The Explanation

Say we have `ExternType` and `ExternTrait`, and we want a certain, useful function that `ExternTrait` provides. We can implement it for certain types like this:

```rust
impl ExternTrait for ExternType {
   fn extern_fn(val: ExternType) -> SomeType {
       // snip
   }
}

impl ExternTrait for ExternType {
   pub(super) fn extern_fn(val: ExternType) -> SomeType {
       // snip
   }
}

impl ExternTrait for ExternType {
   pub(crate) fn extern_fn(val: ExternType) -> SomeType {
       // snip
   }
}

// but not
// impl ExternTrait for ExternType {
// pub fn extern_fn(val: ExternType) -> SomeType {
// // snip
// }
// }

```

This seems like a reasonable, crate-internal relaxation.

---

<div class="post-metadata">

**Author:** ![kpreid](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/kpreid/32/8484_2.png) [@kpreid](https://internals.rust-lang.org/u/kpreid)\
**Post date:** [June 23, 2025, 5:12pm UTC](https://internals.rust-lang.org/t/idea-relaxation-of-the-orphan-rule/23127/2 "2025-06-23T17:12:50Z")

</div>

Visibility of trait implementations would itself be a very complex thing to add. Particularly, there is not necessarily a definite "call site" at which the implementation is used — the requirement for the local implementation to exist might only arise in a use of generic code from a downstream crate. There would need to be a new set of rules for defining which crate a use of a trait should be considered to be in.

It would be very useful if achieved, but it would need its own design and implementation.

---

<div class="post-metadata">

**Author:** ![scottmcm](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/scottmcm/32/2355_2.png) [@scottmcm](https://internals.rust-lang.org/u/scottmcm)\
**Post date:** [June 23, 2025, 6:12pm UTC](https://internals.rust-lang.org/t/idea-relaxation-of-the-orphan-rule/23127/3 "2025-06-23T18:12:57Z")

</div>

> [@SciMind2460](#):
>
> This seems like a reasonable, crate-internal relaxation.

As always, the hard part is the classic _HashTable problem_. If I have a `HashTable<Foo>` and pass it to you, we need to be using the same `Hash` impl, or the table will not work. Your proposal doesn't look like it solves that, because in my function I can be using my private `Hash for Foo` impl, and in your crate you can be using a _different_ `Hash for Foo` impl.

Thus _something_ needs to be in the signature for this to work -- and thus one often ends up at something not _that_ different from a newtype, since the newtype in question is the thing in the signature that says which impl is in use, thanks to the orphan rule.

---

<div class="post-metadata">

**Author:** ![jrose](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/jrose/32/9591_2.png) [@jrose](https://internals.rust-lang.org/u/jrose)\
**Post date:** [June 23, 2025, 6:19pm UTC](https://internals.rust-lang.org/t/idea-relaxation-of-the-orphan-rule/23127/4 "2025-06-23T18:19:06Z")

</div>

I don’t think that necessarily follows: the visibility rules would say you can’t put `HashTable<Foo>` in your public API because it relies on a trait impl that’s not public. (I do think the visibility would belong on the impl though, not specific requirements.)

---

<div class="post-metadata">

**Author:** ![JarredAllen](https://avatars.discourse-cdn.com/v4/letter/j/a183cd/32.png) [@JarredAllen](https://internals.rust-lang.org/u/JarredAllen)\
**Post date:** [June 23, 2025, 8:45pm UTC](https://internals.rust-lang.org/t/idea-relaxation-of-the-orphan-rule/23127/5 "2025-06-23T20:45:01Z")

</div>

There was an RFC a little while ago that proposed adding scoped implementations of traits to get around the orphan rule (also including the possibility of one crate exporting its implementation for other crates to opt into). It was marked as postponed for not having the resources to implement, but it might interest people here to take a look: [https://github.com/rust-lang/rfcs/pull/3634](https://github.com/rust-lang/rfcs/pull/3634)

The gist was that you could write

```rust
use impl ExternTrait for ExternType {
   fn extern_fn(val: ExternType) -> SomeType {
       // snip
   }
}

```

to indicate that you want to make a privately-scoped implementation, and put `pub` before the `use` to allow it to be exported for other crates to choose to opt in to your implementation.

The advantage to allowing for public trait impls is that I could write a crate for `bevy_reflect_impls` to add implementations of bevy's reflection traits to a bunch of third-party types in one location, and then all my other crates can import the same definitions from that crate and interoperate with each other.

---

<div class="post-metadata">

**Author:** ![SkiFire13](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/skifire13/32/7579_2.png) [@SkiFire13](https://internals.rust-lang.org/u/SkiFire13)\
**Post date:** [June 23, 2025, 8:48pm UTC](https://internals.rust-lang.org/t/idea-relaxation-of-the-orphan-rule/23127/6 "2025-06-23T20:48:07Z")

</div>

> [@jrose](#):
>
> the visibility rules would say you can’t put `HashTable<Foo>` in your public API because it relies on a trait impl that’s not public.

How would that work? `HashMap<Foo>` has no requirement for `Foo` to implement `Hash` or `Eq`.

---

<div class="post-metadata">

**Author:** ![jrose](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/jrose/32/9591_2.png) [@jrose](https://internals.rust-lang.org/u/jrose)\
**Post date:** [June 23, 2025, 8:53pm UTC](https://internals.rust-lang.org/t/idea-relaxation-of-the-orphan-rule/23127/7 "2025-06-23T20:53:46Z")

</div>

Oops, right. ☹ Withdrawn!

---

<div class="post-metadata">

**Author:** ![SciMind2460](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/scimind2460/32/13125_2.png) [@SciMind2460](https://internals.rust-lang.org/u/SciMind2460)\
**Post date:** [June 24, 2025, 4:15am UTC](https://internals.rust-lang.org/t/idea-relaxation-of-the-orphan-rule/23127/9 "2025-06-24T04:15:58Z")

</div>

@kpreid my current solution says that the `impl` is only crate-specific, and a downstream crate cannot directly use the `impl`.@scottmcm Maybe for that we could write a `proc_macro_attribute` that you can add to each trait to mark them as safe to relax the orphan rules? Eg... `From`, `TryFrom`?
