Coder Social home page Coder Social logo

Comments (5)

dtolnay avatar dtolnay commented on July 19, 2024 7

anyhow! handles the conversion.

use anyhow::anyhow;

fn main() {
    let e: Box<dyn std::error::Error + Send + Sync> = unimplemented!();
    let e = anyhow!(e);
}

from anyhow.

vorner avatar vorner commented on July 19, 2024 5

It could be shorter with map_err instead of explicit match, eg something like:

something_returning_boxed_error().map_err(|e| anyhow!(e))?

This would be even slightly more ergonomic if it existed as a function/constructor/method, eg:

something_returning_boxed_error().map_err(Anyhow::from_boxed)?

But as long as Rust doesn't allow implementing impl From<Box<std::error::Error + Send + Sync>> for Anyhow, this is probably the best that can be done :-(.

from anyhow.

vorner avatar vorner commented on July 19, 2024

Reading the docs, doesn't this stringify the error? Won't it lose all the source chain, type information and such?

I'd hope it could somehow convert the existing dyn Error from 2-words trait object to the internal 1-word trait object. Or is it not possible?

from anyhow.

vorner avatar vorner commented on July 19, 2024

Oh, I'm reading through the source code and it seems to be preserving it somehow. Would you mind if I send a PR making such usage clearer in the docs? Also, link from the Error type to the macro would make it little bit easier to find.

from anyhow.

rbtcollins avatar rbtcollins commented on July 19, 2024

Perhaps I'm missing something but anyhow! isn't very easy to use with ?.

fn main() -> anyhow::Result<()>{
   something_returning_boxed_error()?;
}

is a bit awkward - I mean:

fn main() -> anyhow::Result<()>{
  match something_returning_boxed_error(){
     Ok(v) => Ok(v),
     Err(e) => Err(anyhow!(e))
  }?
}

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.