How is there a conflicting implementation of `From` when using a generic type?
up vote
5
down vote
favorite
I'm trying to implement an error enum which can contain an error associated with one of our traits like this:
trait Storage {
type Error;
}
enum MyError<S: Storage> {
StorageProblem(S::Error),
}
I have also tried to implement the From
trait to allow construction of MyError
from an instance of a Storage::Error
:
impl<S: Storage> From<S::Error> for MyError<S> {
fn from(error: S::Error) -> MyError<S> {
MyError::StorageProblem(error)
}
}
(playground)
However this fails to compile:
error[E0119]: conflicting implementations of trait `std::convert::From<MyError<_>>` for type `MyError<_>`:
--> src/main.rs:9:1
|
9 | / impl<S: Storage> From<S::Error> for MyError<S> {
10 | | fn from(error: S::Error) -> MyError<S> {
11 | | MyError::StorageProblem(error)
12 | | }
13 | | }
| |_^
|
= note: conflicting implementation in crate `core`
I don't understand why the compiler reckons this has already been implemented. The error message is telling me that there's already an implementation of From<MyError<_>>
(which there is), but I'm not trying to implement that here - I'm trying to implement From<S::Error>
and MyError
is not the same type as S::Error
from what I can see.
Am I missing something fundamental to generics here?
rust
add a comment |
up vote
5
down vote
favorite
I'm trying to implement an error enum which can contain an error associated with one of our traits like this:
trait Storage {
type Error;
}
enum MyError<S: Storage> {
StorageProblem(S::Error),
}
I have also tried to implement the From
trait to allow construction of MyError
from an instance of a Storage::Error
:
impl<S: Storage> From<S::Error> for MyError<S> {
fn from(error: S::Error) -> MyError<S> {
MyError::StorageProblem(error)
}
}
(playground)
However this fails to compile:
error[E0119]: conflicting implementations of trait `std::convert::From<MyError<_>>` for type `MyError<_>`:
--> src/main.rs:9:1
|
9 | / impl<S: Storage> From<S::Error> for MyError<S> {
10 | | fn from(error: S::Error) -> MyError<S> {
11 | | MyError::StorageProblem(error)
12 | | }
13 | | }
| |_^
|
= note: conflicting implementation in crate `core`
I don't understand why the compiler reckons this has already been implemented. The error message is telling me that there's already an implementation of From<MyError<_>>
(which there is), but I'm not trying to implement that here - I'm trying to implement From<S::Error>
and MyError
is not the same type as S::Error
from what I can see.
Am I missing something fundamental to generics here?
rust
add a comment |
up vote
5
down vote
favorite
up vote
5
down vote
favorite
I'm trying to implement an error enum which can contain an error associated with one of our traits like this:
trait Storage {
type Error;
}
enum MyError<S: Storage> {
StorageProblem(S::Error),
}
I have also tried to implement the From
trait to allow construction of MyError
from an instance of a Storage::Error
:
impl<S: Storage> From<S::Error> for MyError<S> {
fn from(error: S::Error) -> MyError<S> {
MyError::StorageProblem(error)
}
}
(playground)
However this fails to compile:
error[E0119]: conflicting implementations of trait `std::convert::From<MyError<_>>` for type `MyError<_>`:
--> src/main.rs:9:1
|
9 | / impl<S: Storage> From<S::Error> for MyError<S> {
10 | | fn from(error: S::Error) -> MyError<S> {
11 | | MyError::StorageProblem(error)
12 | | }
13 | | }
| |_^
|
= note: conflicting implementation in crate `core`
I don't understand why the compiler reckons this has already been implemented. The error message is telling me that there's already an implementation of From<MyError<_>>
(which there is), but I'm not trying to implement that here - I'm trying to implement From<S::Error>
and MyError
is not the same type as S::Error
from what I can see.
Am I missing something fundamental to generics here?
rust
I'm trying to implement an error enum which can contain an error associated with one of our traits like this:
trait Storage {
type Error;
}
enum MyError<S: Storage> {
StorageProblem(S::Error),
}
I have also tried to implement the From
trait to allow construction of MyError
from an instance of a Storage::Error
:
impl<S: Storage> From<S::Error> for MyError<S> {
fn from(error: S::Error) -> MyError<S> {
MyError::StorageProblem(error)
}
}
(playground)
However this fails to compile:
error[E0119]: conflicting implementations of trait `std::convert::From<MyError<_>>` for type `MyError<_>`:
--> src/main.rs:9:1
|
9 | / impl<S: Storage> From<S::Error> for MyError<S> {
10 | | fn from(error: S::Error) -> MyError<S> {
11 | | MyError::StorageProblem(error)
12 | | }
13 | | }
| |_^
|
= note: conflicting implementation in crate `core`
I don't understand why the compiler reckons this has already been implemented. The error message is telling me that there's already an implementation of From<MyError<_>>
(which there is), but I'm not trying to implement that here - I'm trying to implement From<S::Error>
and MyError
is not the same type as S::Error
from what I can see.
Am I missing something fundamental to generics here?
rust
rust
edited Sep 24 '17 at 0:53
Shepmaster
143k11268399
143k11268399
asked May 20 '16 at 12:54
Fraser
50.3k7159175
50.3k7159175
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
up vote
8
down vote
accepted
The problem here is someone may implement Storage
so that the From
impl you have written overlaps with the impl in the standard library of impl<T> From<T> for T
(that is, anything can be converted to itself).
Specifically,
struct Tricky;
impl Storage for Tricky {
type Error = MyError<Tricky>;
}
(The set-up here means this doesn't actually compile—MyError<Tricky>
is infinitely large—but that error is unrelated to the reasoning about impl
s/coherence/overlap, and indeed small changes to MyError
can make it compile without changing the fundamental problem, e.g. adding a Box
like StorageProblem(Box<S::Error>),
.)
If we substitute Tricky
in place of S
in your impl, we get:
impl From<MyError<Tricky>> for MyError<Tricky> {
...
}
This impl
exactly matches the self-conversion one with T
== MyError<Tricky>
, and hence the compiler wouldn't know which one to choose. Instead of making an arbitrary/random choice, the Rust compiler avoids situations like this, and thus the original code must be rejected due to this risk.
This coherence restriction can definitely be annoying, and is one of reasons that specialisation is a much-anticipated feature: essentially allows manually instructing the compiler how to handle overlap... at least, one of the extensions to the current restricted form allows that.
Has the extension been implemented in nightly now?
– WiSaGaN
May 20 '16 at 13:08
No, and in fact the restricted form is still available only in nightly. I believe the intention is to let that bake for a bit/iron out the bugs before diving into the more flexible/powerful versions.
– huon
May 20 '16 at 13:10
A clear answer: thanks. I guess the error message could be a bit clearer if it said something like there could be conflicting implementations rather than implying there already are.
– Fraser
May 20 '16 at 13:11
@Fraser, yeah, definitely, I guess it may be covered by github.com/rust-lang/rust/issues/23980 (I'll write a comment summarising this particular piece of confusion there).
– huon
May 20 '16 at 13:14
Brilliant - thanks again.
– Fraser
May 20 '16 at 13:15
add a comment |
up vote
1
down vote
A workaround for the coherence issue is to use Result::map_err
to perform the conversion yourself. You can then use the end Result
with try!
or ?
:
fn example<S: Storage>(s: S) -> Result<i32, MyError<S>> {
s.do_a_thing().map_err(MyError::StorageProblem)?;
Ok(42)
}
This solution is also valuable when there are error variants that have the same underlying Error
, such as if you want to separate "file opening" and "file reading" errors, both of which are io::Error
.
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
8
down vote
accepted
The problem here is someone may implement Storage
so that the From
impl you have written overlaps with the impl in the standard library of impl<T> From<T> for T
(that is, anything can be converted to itself).
Specifically,
struct Tricky;
impl Storage for Tricky {
type Error = MyError<Tricky>;
}
(The set-up here means this doesn't actually compile—MyError<Tricky>
is infinitely large—but that error is unrelated to the reasoning about impl
s/coherence/overlap, and indeed small changes to MyError
can make it compile without changing the fundamental problem, e.g. adding a Box
like StorageProblem(Box<S::Error>),
.)
If we substitute Tricky
in place of S
in your impl, we get:
impl From<MyError<Tricky>> for MyError<Tricky> {
...
}
This impl
exactly matches the self-conversion one with T
== MyError<Tricky>
, and hence the compiler wouldn't know which one to choose. Instead of making an arbitrary/random choice, the Rust compiler avoids situations like this, and thus the original code must be rejected due to this risk.
This coherence restriction can definitely be annoying, and is one of reasons that specialisation is a much-anticipated feature: essentially allows manually instructing the compiler how to handle overlap... at least, one of the extensions to the current restricted form allows that.
Has the extension been implemented in nightly now?
– WiSaGaN
May 20 '16 at 13:08
No, and in fact the restricted form is still available only in nightly. I believe the intention is to let that bake for a bit/iron out the bugs before diving into the more flexible/powerful versions.
– huon
May 20 '16 at 13:10
A clear answer: thanks. I guess the error message could be a bit clearer if it said something like there could be conflicting implementations rather than implying there already are.
– Fraser
May 20 '16 at 13:11
@Fraser, yeah, definitely, I guess it may be covered by github.com/rust-lang/rust/issues/23980 (I'll write a comment summarising this particular piece of confusion there).
– huon
May 20 '16 at 13:14
Brilliant - thanks again.
– Fraser
May 20 '16 at 13:15
add a comment |
up vote
8
down vote
accepted
The problem here is someone may implement Storage
so that the From
impl you have written overlaps with the impl in the standard library of impl<T> From<T> for T
(that is, anything can be converted to itself).
Specifically,
struct Tricky;
impl Storage for Tricky {
type Error = MyError<Tricky>;
}
(The set-up here means this doesn't actually compile—MyError<Tricky>
is infinitely large—but that error is unrelated to the reasoning about impl
s/coherence/overlap, and indeed small changes to MyError
can make it compile without changing the fundamental problem, e.g. adding a Box
like StorageProblem(Box<S::Error>),
.)
If we substitute Tricky
in place of S
in your impl, we get:
impl From<MyError<Tricky>> for MyError<Tricky> {
...
}
This impl
exactly matches the self-conversion one with T
== MyError<Tricky>
, and hence the compiler wouldn't know which one to choose. Instead of making an arbitrary/random choice, the Rust compiler avoids situations like this, and thus the original code must be rejected due to this risk.
This coherence restriction can definitely be annoying, and is one of reasons that specialisation is a much-anticipated feature: essentially allows manually instructing the compiler how to handle overlap... at least, one of the extensions to the current restricted form allows that.
Has the extension been implemented in nightly now?
– WiSaGaN
May 20 '16 at 13:08
No, and in fact the restricted form is still available only in nightly. I believe the intention is to let that bake for a bit/iron out the bugs before diving into the more flexible/powerful versions.
– huon
May 20 '16 at 13:10
A clear answer: thanks. I guess the error message could be a bit clearer if it said something like there could be conflicting implementations rather than implying there already are.
– Fraser
May 20 '16 at 13:11
@Fraser, yeah, definitely, I guess it may be covered by github.com/rust-lang/rust/issues/23980 (I'll write a comment summarising this particular piece of confusion there).
– huon
May 20 '16 at 13:14
Brilliant - thanks again.
– Fraser
May 20 '16 at 13:15
add a comment |
up vote
8
down vote
accepted
up vote
8
down vote
accepted
The problem here is someone may implement Storage
so that the From
impl you have written overlaps with the impl in the standard library of impl<T> From<T> for T
(that is, anything can be converted to itself).
Specifically,
struct Tricky;
impl Storage for Tricky {
type Error = MyError<Tricky>;
}
(The set-up here means this doesn't actually compile—MyError<Tricky>
is infinitely large—but that error is unrelated to the reasoning about impl
s/coherence/overlap, and indeed small changes to MyError
can make it compile without changing the fundamental problem, e.g. adding a Box
like StorageProblem(Box<S::Error>),
.)
If we substitute Tricky
in place of S
in your impl, we get:
impl From<MyError<Tricky>> for MyError<Tricky> {
...
}
This impl
exactly matches the self-conversion one with T
== MyError<Tricky>
, and hence the compiler wouldn't know which one to choose. Instead of making an arbitrary/random choice, the Rust compiler avoids situations like this, and thus the original code must be rejected due to this risk.
This coherence restriction can definitely be annoying, and is one of reasons that specialisation is a much-anticipated feature: essentially allows manually instructing the compiler how to handle overlap... at least, one of the extensions to the current restricted form allows that.
The problem here is someone may implement Storage
so that the From
impl you have written overlaps with the impl in the standard library of impl<T> From<T> for T
(that is, anything can be converted to itself).
Specifically,
struct Tricky;
impl Storage for Tricky {
type Error = MyError<Tricky>;
}
(The set-up here means this doesn't actually compile—MyError<Tricky>
is infinitely large—but that error is unrelated to the reasoning about impl
s/coherence/overlap, and indeed small changes to MyError
can make it compile without changing the fundamental problem, e.g. adding a Box
like StorageProblem(Box<S::Error>),
.)
If we substitute Tricky
in place of S
in your impl, we get:
impl From<MyError<Tricky>> for MyError<Tricky> {
...
}
This impl
exactly matches the self-conversion one with T
== MyError<Tricky>
, and hence the compiler wouldn't know which one to choose. Instead of making an arbitrary/random choice, the Rust compiler avoids situations like this, and thus the original code must be rejected due to this risk.
This coherence restriction can definitely be annoying, and is one of reasons that specialisation is a much-anticipated feature: essentially allows manually instructing the compiler how to handle overlap... at least, one of the extensions to the current restricted form allows that.
edited May 20 '16 at 13:24
answered May 20 '16 at 13:04
huon
53.4k6142171
53.4k6142171
Has the extension been implemented in nightly now?
– WiSaGaN
May 20 '16 at 13:08
No, and in fact the restricted form is still available only in nightly. I believe the intention is to let that bake for a bit/iron out the bugs before diving into the more flexible/powerful versions.
– huon
May 20 '16 at 13:10
A clear answer: thanks. I guess the error message could be a bit clearer if it said something like there could be conflicting implementations rather than implying there already are.
– Fraser
May 20 '16 at 13:11
@Fraser, yeah, definitely, I guess it may be covered by github.com/rust-lang/rust/issues/23980 (I'll write a comment summarising this particular piece of confusion there).
– huon
May 20 '16 at 13:14
Brilliant - thanks again.
– Fraser
May 20 '16 at 13:15
add a comment |
Has the extension been implemented in nightly now?
– WiSaGaN
May 20 '16 at 13:08
No, and in fact the restricted form is still available only in nightly. I believe the intention is to let that bake for a bit/iron out the bugs before diving into the more flexible/powerful versions.
– huon
May 20 '16 at 13:10
A clear answer: thanks. I guess the error message could be a bit clearer if it said something like there could be conflicting implementations rather than implying there already are.
– Fraser
May 20 '16 at 13:11
@Fraser, yeah, definitely, I guess it may be covered by github.com/rust-lang/rust/issues/23980 (I'll write a comment summarising this particular piece of confusion there).
– huon
May 20 '16 at 13:14
Brilliant - thanks again.
– Fraser
May 20 '16 at 13:15
Has the extension been implemented in nightly now?
– WiSaGaN
May 20 '16 at 13:08
Has the extension been implemented in nightly now?
– WiSaGaN
May 20 '16 at 13:08
No, and in fact the restricted form is still available only in nightly. I believe the intention is to let that bake for a bit/iron out the bugs before diving into the more flexible/powerful versions.
– huon
May 20 '16 at 13:10
No, and in fact the restricted form is still available only in nightly. I believe the intention is to let that bake for a bit/iron out the bugs before diving into the more flexible/powerful versions.
– huon
May 20 '16 at 13:10
A clear answer: thanks. I guess the error message could be a bit clearer if it said something like there could be conflicting implementations rather than implying there already are.
– Fraser
May 20 '16 at 13:11
A clear answer: thanks. I guess the error message could be a bit clearer if it said something like there could be conflicting implementations rather than implying there already are.
– Fraser
May 20 '16 at 13:11
@Fraser, yeah, definitely, I guess it may be covered by github.com/rust-lang/rust/issues/23980 (I'll write a comment summarising this particular piece of confusion there).
– huon
May 20 '16 at 13:14
@Fraser, yeah, definitely, I guess it may be covered by github.com/rust-lang/rust/issues/23980 (I'll write a comment summarising this particular piece of confusion there).
– huon
May 20 '16 at 13:14
Brilliant - thanks again.
– Fraser
May 20 '16 at 13:15
Brilliant - thanks again.
– Fraser
May 20 '16 at 13:15
add a comment |
up vote
1
down vote
A workaround for the coherence issue is to use Result::map_err
to perform the conversion yourself. You can then use the end Result
with try!
or ?
:
fn example<S: Storage>(s: S) -> Result<i32, MyError<S>> {
s.do_a_thing().map_err(MyError::StorageProblem)?;
Ok(42)
}
This solution is also valuable when there are error variants that have the same underlying Error
, such as if you want to separate "file opening" and "file reading" errors, both of which are io::Error
.
add a comment |
up vote
1
down vote
A workaround for the coherence issue is to use Result::map_err
to perform the conversion yourself. You can then use the end Result
with try!
or ?
:
fn example<S: Storage>(s: S) -> Result<i32, MyError<S>> {
s.do_a_thing().map_err(MyError::StorageProblem)?;
Ok(42)
}
This solution is also valuable when there are error variants that have the same underlying Error
, such as if you want to separate "file opening" and "file reading" errors, both of which are io::Error
.
add a comment |
up vote
1
down vote
up vote
1
down vote
A workaround for the coherence issue is to use Result::map_err
to perform the conversion yourself. You can then use the end Result
with try!
or ?
:
fn example<S: Storage>(s: S) -> Result<i32, MyError<S>> {
s.do_a_thing().map_err(MyError::StorageProblem)?;
Ok(42)
}
This solution is also valuable when there are error variants that have the same underlying Error
, such as if you want to separate "file opening" and "file reading" errors, both of which are io::Error
.
A workaround for the coherence issue is to use Result::map_err
to perform the conversion yourself. You can then use the end Result
with try!
or ?
:
fn example<S: Storage>(s: S) -> Result<i32, MyError<S>> {
s.do_a_thing().map_err(MyError::StorageProblem)?;
Ok(42)
}
This solution is also valuable when there are error variants that have the same underlying Error
, such as if you want to separate "file opening" and "file reading" errors, both of which are io::Error
.
edited Nov 4 '17 at 15:06
answered Oct 9 '17 at 15:27
Shepmaster
143k11268399
143k11268399
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f37347311%2fhow-is-there-a-conflicting-implementation-of-from-when-using-a-generic-type%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown