Coder Social home page Coder Social logo

Comments (9)

dtolnay avatar dtolnay commented on July 19, 2024

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.

Razican avatar Razican commented on July 19, 2024

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.

dtolnay avatar dtolnay commented on July 19, 2024

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.

Razican avatar Razican commented on July 19, 2024

Thanks! That worked perfectly :) Great job with the lib!!

I'll close this now.

from anyhow.

emilazy avatar emilazy commented on July 19, 2024

I personally find it really unfortunate that there's no way to work with anyhow::Errors 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.

emilazy avatar emilazy commented on July 19, 2024

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.

emilazy avatar emilazy commented on July 19, 2024

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.

dtolnay avatar dtolnay commented on July 19, 2024

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.

emilazy avatar emilazy commented on July 19, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.