error[E0119]: conflicting implementations of trait `From<bool>` for type `Repr<'_>`
--> src/main.rs:21:1
|
9 | / impl<'a, T> From<T> for Repr<'a>
10 | | where
11 | | T: Into<Cow<'a, str>>,
| |__________________________- first implementation here
...
21 | impl From<bool> for Repr<'_> {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ conflicting implementation for `Repr<'_>`
|
= note: upstream crates may add a new impl of trait `std::convert::From<bool>` for type `std::borrow::Cow<'_, str>` in future versions
I'm trying to write something along the lines of Pattern in std, but with a method that receives repr: impl Into<Repr<'a>> and some different From impls for it.
Playground: Rust Playground
Thank you!
I'm on the phone right now, so I'm sorry in advance for not searching the implementation for you, but this error means that bool implements Into<Cow<'a, str>>, therefore your implementations conflict with each other.
This cannot be fixed without specialization being stabilised.
note: upstream crates may add a new impl of trait
`std::convert::From<bool>` for type `std::borrow::Cow<'_, str>`
in future versions
If your implementation was allowed, it would become a semver breaking change for std to add that implementation (it would break your crate and any dependencies of it). The error is to prevent you from imposing such a restriction on upstream.
I.e. it's not just about existing implementations.
Humm, but I don't try to implement From<bool> for Cow, I implement it for a local type, which will call Cow::default()... I still fail to see why it breaks.
Then your impl<'a, T: Into<Cow<'_, str>> From<T> for Repr<'a> would apply for T = bool
And this would overlap with your impl From<bool> for Repr<'_>
More generally, the overlap check won't assume that "T doesn't implement Trait<..>" must hold unless it is both currently true, andimpl T for Trait<..> is also allowed under the orphan rules.[1]
You can't implement Into<Cow<'_, str>> for bool due to the orphan rules, so for the purposes of the overlap check, the compiler assumes upstream may one day implement it (in the same major SemVer).
(This may become more flexible in various ways eventually, but that's probably a long way off and may or may not apply to your specific situation.)
So, there isn't really any way to do this, right?
I like Into parameters so I can call f(1) or f(None) for a fn f(t: impl Into<Option<i32>>).
Here, I was trying f("bytes") and f(true) for a fn f(r: impl Into<Repr>).