Comments (9)
This is intentional, since that impl would be incoherent with the existing two impls impl From<T> for T
and impl<E> From<E> for anyhow::Error where E: std::error::Error + Send + Sync + 'static
.
The impl you should write is:
impl<T> FromStr for StrList<T>
where
T: FromStr,
anyhow::Error: From<T::Err>,
from anyhow.
Cool! that's true, I was able to implement it like that. But I have another situation, using diesel
's Error
type, that if I want to create one, it only accepts a boxed std::error::Error
. How can I achieve that from an anyhow::Error
?
from anyhow.
We provide a conversion to boxed std::error::Error, so .into()
would do the trick.
https://docs.rs/anyhow/1.0.26/anyhow/struct.Error.html#impl-From<Error>
from anyhow.
Thanks! That worked perfectly :) Great job with the lib!!
I'll close this now.
from anyhow.
I personally find it really unfortunate that there's no way to work with anyhow::Error
s in a generic context without the extra fuss/overhead of boxing, though I understand trait coherence makes it difficult. Just thought I'd register this as something I was confused enough about to go look in the issue tracker -- I think it could at least use more explicit documenting if nothing else.
from anyhow.
I think a nice way to solve this that would also make the implementation of the conversion into std::error::Error
a lot less scary is to just provide a newtype wrapper over anyhow::Error
that doesn't have the From
conveniences, but does implement Error
. Then you could freely convert back and forth according to your needs, and adapt to APIs that expect Error
more easily.
edit: it's also pretty hard to work around, because Box<dyn Error>
doesn't implement Error
either :(
from anyhow.
Trivial proof of concept of what it'd be nice to see in anyhow
proper:
struct AsStdError(anyhow::Error);
impl From<anyhow::Error> for AsStdError {
fn from(err: anyhow::Error) -> Self {
Self(err)
}
}
impl fmt::Debug for AsStdError {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
self.0.fmt(fmt)
}
}
impl fmt::Display for AsStdError {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
self.0.fmt(fmt)
}
}
impl Error for AsStdError {
fn source(&self) -> Option<&(dyn Error + 'static)> {
self.0.source()
}
// requires nightly to implement, which is prohibitive for downstream code
fn backtrace(&self) -> Option<Backtrace> {
self.0.backtrace()
}
}
I think the main thing that would need bikeshedding here is the name.
from anyhow.
I don't plan to put a wrapper in anyhow because users should use thiserror if they need one.
use thiserror::Error;
#[derive(Error, Debug)]
#[error(transparent)]
pub struct AsStdError(#[from] anyhow::Error);
This expands to what you wrote. Also it has the advantage of being a local type for the purpose of orphan rules and adding inherent methods.
from anyhow.
Oh, that's fair; I'm a happy user of thiserror but didn't realize it would suffice for this case. Thanks!
from anyhow.
Related Issues (20)
- [Question] In `{:#?}` why is the outermost context called "caused by" and how is `Context` expected to be used? HOT 1
- `ensure!` macro implementation details HOT 8
- Nightly feature wrongly enabled on stable toolchain HOT 3
- Should `anyhow::Error::chain` return `dyn Error + Send + Sync` HOT 2
- Customize backtrace logic?
- `anyhow!(e)` doesn't preserve `source` for `&Error` HOT 1
- A way to disable anyhow stacktraces (without disabling stacktraces from other crates) HOT 2
- Default Ok-type to `()` in `anyhow::Result` typedef HOT 2
- Updating from version 1.0.76 breaks backtraces HOT 2
- Make backtrace support optional HOT 10
- Possible performance regression on Windows HOT 5
- as_ref() type must be known at this point
- Depending on `CARGO_ENCODED_RUSTFLAGS` may produce stale builds HOT 1
- rust-analyzer nightly throws needless_return warning on bail! HOT 2
- Implement Context for Error
- Question regarding stacktrace
- anyhow::Error to Box<dyn Error> isn't compatible with other libraries HOT 2
- Are you open to de-duplicating the build.rs build probe code? HOT 7
- `anyhow::ensure!` doesn't work with custom error type HOT 1
- Short backtrace HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from anyhow.