Hi,
I'm trying to convert a ToString
or Vec<ToString>
argument to an Enum
, but I'm stuck on implementing the From
trait, as there is a conflicting implementation, which seems logical to me as T
covers both X
and Vec<Y>
:
fn main() {
test("a");
test("b".to_string());
test(vec!["c"]);
test(vec!["d".to_string()]);
}
fn test<T>(value: T) where T: Into<TestEnum> {
let value = value.into();
}
enum TestEnum {
String(String),
Vec(Vec<String>),
}
impl<T> From<T> for TestEnum where T: ToString {
fn from(value: T) -> Self {
TestEnum::String(value.to_string())
}
}
impl<T> From<Vec<T>> for TestEnum where T: ToString {
fn from(value: Vec<T>) -> Self {
TestEnum::Vec(value.iter().map(|value| value.to_string()).collect())
}
}
The error:
error[E0119]: conflicting implementations of trait `std::convert::From<std::vec::Vec<_>>` for type `TestEnum`:
--> src/main.rs:23:1
|
17 | impl<T> From<T> for TestEnum where T: ToString {
| ---------------------------------------------- first implementation here
...
23 | impl<T> From<Vec<T>> for TestEnum where T: ToString {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ conflicting implementation for `TestEnum`
|
= note: upstream crates may add a new impl of trait `std::fmt::Display` for type `std::vec::Vec<_>` in future versions
How can I implement this?
Kind regards,
Raymond