Code:
use std::collections::VecDeque;
struct MyVec<T>(Vec<T>);
impl<T> From<Option<Vec<T>>> for Option<MyVec<T>> {
fn from(value: Option<Vec<T>>) -> Self {
Some(MyVec(value?))
}
}
impl From<Option<Vec<i32>>> for Option<MyVec<i32>> {
fn from(value: Option<Vec<i32>>) -> Self {
Some(MyVec(value?))
}
}
struct Node<T> {
val: T,
next: Option<Box<Node<T>>>,
}
impl<T> From<VecDeque<Box<Node<T>>>> for Option<Box<Node<T>>> {
fn from(mut value: VecDeque<Box<Node<T>>>) -> Self {
let mut head = value.pop_front()?;
let mut current = &mut head;
for node in value {
current.next = Some(node);
current = current.next.as_mut().unwrap()
}
Some(head)
}
}
fn main() {}
And the result:
Checking explore v0.1.0 (/home/PC-Killer/projects/explore)
error[E0119]: conflicting implementations of trait `From<Option<Vec<i32>>>` for type `Option<MyVec<i32>>`
--> src/main.rs:11:1
|
5 | impl<T> From<Option<Vec<T>>> for Option<MyVec<T>> {
| ------------------------------------------------- first implementation here
...
11 | impl From<Option<Vec<i32>>> for Option<MyVec<i32>> {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ conflicting implementation for `Option<MyVec<i32>>`
error[E0117]: only traits defined in the current crate can be implemented for types defined outside of the crate
--> src/main.rs:5:1
|
5 | impl<T> From<Option<Vec<T>>> for Option<MyVec<T>> {
| ^^^^^^^^--------------------^^^^^----------------
| | |
| | `Option` is not defined in the current crate
| `Option` is not defined in the current crate
|
= note: impl doesn't have any local type before any uncovered type parameters
= note: for more information see https://doc.rust-lang.org/reference/items/implementations.html#orphan-rules
= note: define and implement a trait or new type instead
error[E0117]: only traits defined in the current crate can be implemented for types defined outside of the crate
--> src/main.rs:11:1
|
11 | impl From<Option<Vec<i32>>> for Option<MyVec<i32>> {
| ^^^^^----------------------^^^^^------------------
| | |
| | `Option` is not defined in the current crate
| `Option` is not defined in the current crate
|
= note: impl doesn't have any local type before any uncovered type parameters
= note: for more information see https://doc.rust-lang.org/reference/items/implementations.html#orphan-rules
= note: define and implement a trait or new type instead
error[E0117]: only traits defined in the current crate can be implemented for types defined outside of the crate
--> src/main.rs:22:1
|
22 | impl<T> From<VecDeque<Box<Node<T>>>> for Option<Box<Node<T>>> {
| ^^^^^^^^----------------------------^^^^^--------------------
| | |
| | `Option` is not defined in the current crate
| `VecDeque` is not defined in the current crate
|
= note: impl doesn't have any local type before any uncovered type parameters
= note: for more information see https://doc.rust-lang.org/reference/items/implementations.html#orphan-rules
= note: define and implement a trait or new type instead
Some errors have detailed explanations: E0117, E0119.
For more information about an error, try `rustc --explain E0117`.
error: could not compile `explore` (bin "explore") due to 4 previous errors
Divide this into three parts:
-
Here we implementstruct MyVec<T>(Vec<T>); impl<T> From<Option<Vec<T>>> for Option<MyVec<T>> { fn from(value: Option<Vec<T>>) -> Self { Some(MyVec(value?)) } }From<Option<Vec<T>>forOption<MyVec<T>>, and the compiler reports that we voilate the orphan rule. However,MyVecis defined in the scope, so there's no chance that a more specialized overload will be defined in any other modules. -
Since we all use concrete types likeimpl From<Option<Vec<i32>>> for Option<MyVec<i32>> { fn from(value: Option<Vec<i32>>) -> Self { Some(MyVec(value?)) } }Option<MyVec<i32>>, which means it's impossible that there is a more specialized implementation than this. However the compiler just reports thatOption<MyVec<i32>>is foreign, which is a little stupid. -
This proves that similar case does make sense, while still being rejected by the compiler.struct Node<T> { val: T, next: Option<Box<Node<T>>>, } impl<T> From<VecDeque<Box<Node<T>>>> for Option<Box<Node<T>>> { fn from(mut value: VecDeque<Box<Node<T>>>) -> Self { let mut head = value.pop_front()?; let mut current = &mut head; for node in value { current.next = Some(node); current = current.next.as_mut().unwrap() } Some(head) } }
Is it possible to correct the behavior?