struct Newtype(usize);
impl From<usize> for Newtype {
fn from(item: usize) -> Self {
Newtype(item)
}
}
fn main() {
let us: usize = 0;
let newtype: Newtype = us.into(); // works
let vec: Vec<usize> = vec![0, 1, 2];
let vec_newtype: Vec<Newtype> = vec.into(); // not works
}
Maybe it would be better to have an impl such like
impl<T, U> From<Vec<T>> for Vec<U>
where
U: From<T>,
{
fn from(item: Vec<T>) -> Self {
item.into_iter().map(U::from).collect()
}
}
In the standard library?
Could it be considered if I'll make a PR like this?
Also I have a question, wouldn't it be useful for all types? To automatically have Into, if all generics do have impl Into? Maybe if Into is unwanted, it could be possible to opt out them with negative impl?
As far as I know, at the moment, specialization is used in the standard library only for performance optimizations, not to write publicly visible trait implementations that are impossible without specialization.
Also specialization is of limited help here, since not one implementation is more general than the other, but we have two overlapping ones, but both impls contain cases the other one doesn’t.