Description
The current orphan rules (described as "covered first" in my blog post) are too restrictive in some cases. The purpose of this RFC issue is to catalog cases where the rules are tighter than we would like and to also point at possible solutions.
Scenario. Type parameters restrict our flexibility
In the discussion around the try trait in #1718, we realized that the orphan rules would prohibit one from adding an impl like this:
impl<T,E> Try<Poll<T, E>> for Result<T, E>
This is sort of annoying because if there were no type parameters involved, the impl would be permitted:
impl Try<Poll<(),()>> for Result<(),()> // OK
This roughly corresponds to the impl<T> BorrowFrom<Rc<T>> for T
example from the Little Orphan Impls blog post. As that blog post describes, this is an artifact of introducing ordering into the algorithm; the post describes various alternative rules, notably the "covered rule", that would permit both of these impls, but at the cost of other kinds of rules.
Scenario. Wanting to implement a trait where the output type is not in current crate.
@Manishearth describes wanting to add an impl into libstd like so:
impl Add<str> for str {
type Output = String;
...
}
By rights, this impl should live in libcore, since all the input types are in libcore, but the output type is defined in libstd, so of course it cannot.
Scenario. Optionally derive traits if desired by others, but without adding a required dependency.
A common example is that my crate foo
defines a type Foo
which can be serialized (e.g., using the serde crate). However, I do not want to add serde
as a required dependency of foo
. Currently, best practice is to define a Cargo.toml
feature (e.g., with_serde
) that adds a dependency on serde
and includes the relevant impls. But this is a lot of manual work. Another example is how the rayon
crate exports the ParallelIterator
trait; other crates, like ndarray
, might want to implement those, but without forcing a dependency on rayon
on others
Proposals targeting this scenario:
This scenario was also described in #1553.
Other scenarios?
Please leave comments and we can migrate those descriptions into this header.