Coder Social home page Coder Social logo

Add Clone support about anyhow HOT 4 CLOSED

dtolnay avatar dtolnay commented on June 21, 2024 5
Add Clone support

from anyhow.

Comments (4)

dtolnay avatar dtolnay commented on June 21, 2024 2

In order to provide a Clone impl we would need to do either of:

  • require that you can only construct anyhow::Error from errors that are Clone, which would make it incompatible with many error types in the wild like io::Error and serde_json::Error; or
  • reference-count the internal error.

The first one seems bad to me and the second one makes it impossible to downcast by value which also seems bad (fwiw failure came to the same conclusion in rust-lang-deprecated/failure#148).

Is there a way for you to work around this by using Rc<Error> or Arc<Error> in the tests?

from anyhow.

krojew avatar krojew commented on June 21, 2024

My first though was about using Arc, but that introduced a ton of changes which were not really needed from the api standpoint. Unfortunately, the work required outweighed the benefit of using anyhow. And there's also the possibility of passing error across 3rd party crates which don't wrap results in anything.

from anyhow.

dtolnay avatar dtolnay commented on June 21, 2024

That's too bad. I am sorry to hear that it didn't work out for your use case!

from anyhow.

oconnor663 avatar oconnor663 commented on June 21, 2024

I was writing a question about this, but I think I answered it for myself. Here it is, in case anyone else has it.

Q: How can I use anyhow::Result inside of a global static once_cell::sync::Lazy<anyhow::Result<T>>, given that the error is not Clone? How can a caller report the error if it can't be cloned out of the Result?

A: Use a once_cell::sync::OnceCell<T> instead and initialize it with get_or_try_init. Probably wrap all of that inside a function returning anyhow::Result<&T>, with the OnceCell as a local static inside the function body. That gives the error value to the caller who tried to initialize it. Subsequent callers can try again, and they'll get their own errors. (This assumes that either it's ok for subsequent callers to try again, or that you'll quit the first time you encounter an error.)

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.