impl<E: Error> From<E> for Box<dyn Error + Send>?

I noticed that the following impls exists:

impl<'a, E: Error + 'a> From<E> for Box<dyn Error + 'a> { /* ... */ }
impl<'a, E: Error + Send + Sync + 'a> From<E>
    for Box<dyn Error + Send + Sync + 'a> { /* ... */ }

Why the Error + Send and the (not so much useful) Error + Sync do not exist? Is it because of actual limits on impl overlapping/missing specialization?

The actual reason is I was playing a bit with rayon and error handling. I just needed the error type to be dyn Error + Send, but at the end I needed to use dyn Error + Send + Sync in order to use the try operator without spamming .map_err(|e| Box::new(e) as Box<dyn Error + Send>) everywhere.

I thought so, but then the target of the From trait is different each time , so there is no actual overlap:


In the meantime, you can factor out that closure within its own function to get a slightly less verbose output: .map_err(boxerr)?

  • Playground

  • Click here for the detailed reasoning behind this choice

    image
    :stuck_out_tongue:

2 Likes

I was not ready.

1 Like

QotW, please! :joy:

1 Like

Ok, back to the original discussion: could be this something overlooked? I think the "only dyn Send" can be useful when rayon or channels are used. I am still not sure about the "only dyn Sync" objects -- maybe in case of Pinned errors (but why)?

In case it looks like a reasonable feature, I will try to ask in the internals forum.

1 Like

Yes, it looks like a legitimate request for me: you can even go as far as submitting a PR and get a discussion there, provided nobody has already done that yet.

2 Likes

The OP was talking about there being this impl in the stdlib, since it is indeed impossible for a downstream crate to impl it.

1 Like

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