Would it ever be possible to add a blanket implementations like this to stdlib?

impl<A, B> [Try]From<A> for Option<B> where B: [Try]From<A>

It certainly ia not an infrequent occurrence to perform an operation like that, but while in normal code it is possible to just map the container type, it is not always possible, for example in some derive annotations like sqlx's FromRow derive's #[try_from(Type)] annotation.

The immediate problem is that this overlaps with impl<T> From<T> for T, because A and B may be the same type. I don't know if specialization or the like can ever solve that.

2 Likes

I think specialization could solve that (we'd need a specialized [Try]From Option<B> for Option<B> to cover the overlap), and specialization would also be required to avoid this being a big breaking change. Right now, one could write (not only can you write this, I'd guess this is what OP should write to get their code working):

struct Foo { .. }
struct Bar { .. }

impl TryFrom<Foo> for Bar { .. }
impl TryFrom<Foo> for Option<Bar> { .. }

And with OP's blanket impl but without specialization, this is a conflict that would cause the code to compile error.

The construction I'm wishing it was possible to avoid is as follows:

use foo_crate::Foo;

struct FooProxy(..);
struct FooProxyOptional(..);

impl TryFrom<FooProxy> for Foo { .. }
impl TryFrom<FooProxyOptional> for Option<Foo> { .. }