Dynamic Error Wrapper Like anyhow::Error in std #30
Description
Has there been any discussion of getting something like anyhow::Error
into std
?
The adoption of anyhow shows how popular that approach is, and especially with Backtrace
capturing I consider it the best strategy for non-library code with the current language feature set.
The benefit of having this in std
instead of a third party library would be two-fold.
Coherence
One drawback is that types like anyhow::Error
can not implement std::error::Error
themselves without specialization due to coherence rules and the required From<T: Error> for WrapperError
. In std
this should be solvable with specialization.
This is quite important for usability in certain circumstances, since these types don't compose well. Thankfully most libraries have their own custom Error types, which lessens the impact, but there are still many circumstances where libraries wrap a user-provided fallible operation.
Examples would be a database transaction function like Db::transaction<E, T, F>(&self, f) where F: Fn(&tx) -> Result<T, E>
or a fallible stream like it is required for hyper::Body
.
failure
used to mitigate this with a Compat
wrapper that could implement std::error::Error
, but it's still a rather awkward solution.
Standardization
The second benefit would be standardizing the ecosystem around a common solution. cargo add anyhow
is the first thing I do in pretty much every (non-library) crate I start. Having it built in would also be very beneficial for new users, so they don't need to go hunting for solutions and read blog posts on how to handle errors.
Drawbacks
The obvious drawback is that Rust is still evolving. If new patterns emerge, std
might be stuck with a sub-optimal implementation and cause some ecosystem churn.
But considering the current state of Rust, I personally feel like the risk is acceptable and is outweighed by the benefits.
For bikeshedding purposes: I would probably name this type AnyError
, with DynError
as a second idea.