Impl From array for array: `impl From<[T; N]> for [U; N]`

I can't any thread or RFC about such an implementation. It seems to be very simple, so I can't understand why it doesn't exist.

impl<const N: usize, T, U: From<T>> From<[T; N]> for [U; N]
{
    fn from(value: [T; N]) -> [U; N] {
        // unsure if something with `unsafe` + `MaybeUninit` would be faster
        value.map(From::from)
    }
}

Here is a playground with impl<const N: usize, T, U: MyFrom<T>> MyFrom<[T; N]> for [U; N]: Rust Playground

This would be an overlapping impl in the case T = U.

1 Like

Right, it would overlap with impl From<T> for T.

Found Type inequality constraints in `where` clauses · Issue #1834 · rust-lang/rfcs · GitHub that talks about where T: !U as a next step in case someone also finds this thread.

1 Like

This is the same problem as how there's not the obvious impl From<Option<T>> for Option<U>. Everyone wants it, but we don't know how to make it work yet.

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.