I want to transform types of "A" into "B" and collections of "A" to collections of "B" (and vice versa).
I have some misunderstanding of how the mechanism works.
I assumed implementing From on the base type would transfer to collections similarly without explicitly implementing.
For example:
struct A {
text: String
}
struct B {
text: String
}
impl From<A> for B {
fn from(a: A) -> Self {
B { text: a.text }
}
}
fn main() {
let a = A { text: "hello".to_string() };
let b = B::from(a); // works
let a2 = A { text: "hello".to_string() };
let b2 = a.into(); // works
let v1 = vec![A { text: "hello".to_string()}];
let v2 = Vec::<B>::from(v1); // doesn't work
let v2 : Vec<B> = v1.into(); // doesn't work
}
the error I get for converting the collections:
Compiling playground v0.0.1 (/playground)
error[E0277]: the trait bound `Vec<B>: From<Vec<A>>` is not satisfied
--> src/main.rs:20:14
|
20 | let v2 = Vec::<B>::from(v1); // doesn't work
| ^^^^^^^^^^^^^^ the trait `From<Vec<A>>` is not implemented for `Vec<B>`
|
= help: the following implementations were found:
<Vec<T, A> as From<Box<[T], A>>>
<Vec<T> as From<&[T]>>
<Vec<T> as From<&mut [T]>>
<Vec<T> as From<BinaryHeap<T>>>
and 6 others
= note: required by `from`
error[E0277]: the trait bound `Vec<B>: From<Vec<A>>` is not satisfied
--> src/main.rs:21:26
|
21 | let v2 : Vec<B> = v1.into(); // doesn't work
| ^^^^ the trait `From<Vec<A>>` is not implemented for `Vec<B>`
|
= help: the following implementations were found:
<Vec<T, A> as From<Box<[T], A>>>
<Vec<T> as From<&[T]>>
<Vec<T> as From<&mut [T]>>
<Vec<T> as From<BinaryHeap<T>>>
and 6 others
= note: required because of the requirements on the impl of `Into<Vec<B>>` for `Vec<A>`
Is there a "blanket" implementation for these nested conversions? If not what is the best way to achieve this flexibility?