Code Monkey home page Code Monkey logo

Comments (6)

shepmaster avatar shepmaster commented on July 17, 2024 7

I wonder if we could do something like

#[cfg(backtrace)]
struct Backtrace(...);

#[cfg(not(backtrace))]
struct Backtrace;

This would lift the need for each library to add the conditional compilation. It might also allow them to avoid adding a feature flag, as the application author could be responsible for adding snafu = { features: [backtrace ] } to their Cargo.toml. Then the library author just has to ensure they add a Backtrace field where appropriate.

from snafu.

shepmaster avatar shepmaster commented on July 17, 2024 1

General plan:

  • The backtraces feature is disabled by default
  • SNAFU unconditionally has a Backtrace type, encouraging error type authors to include it in their types.
  • When the application author enables the backtraces feature, the SNAFU Backtrace type starts to carry a backtrace::Backtrace.
  • The backtrace-crate feature implies backtraces and adds the AsRef implementation as shown in this PR. It wouldn't need to return an Option because of the requirement on backtraces.
  • In the future, we could add a backtrace-std feature that also implies backtraces. It would be incompatible with the backtrace-crate feature, but only an application should enable it so this shouldn't be a problem. At that point in time, we could also implement AsRef for the standard library Backtrace type.

For a library author, they just scatter Backtrace in all of their leaf errors (and backtrace(delegate) in aggregating errors, for now).

An application author adds features = [backtraces] and optionally one of backtrace-crate, backtrace-std (sometime), if they need to access the underlying type.

from snafu.

shepmaster avatar shepmaster commented on July 17, 2024

as the top-level consumer is no more in charge of those features

But the consumer is not actually in charge of this. The library creating the error already has to add a Backtrace type to each corresponding error variant:

#[derive(Snafu)]
enum Error {
    Something { backtrace: Backtrace }
}

With your proposed end result, every crate will also need to

  1. Add a feature flag for backtraces

  2. Add conditional compilation to each backtrace field:

    #[derive(Snafu)]
    enum Error {
        Something { #[cfg(my-backtraces)] backtrace: Backtrace }
    }

These multiple steps make me worried that library authors simply won't use backtraces at all, negating any benefits.

There's an inherent tension around default features. Without a feature by default, people get confused because "the docs show it's there, but it doesn't work for me". With a feature by default, time and space can be wasted on unused functionality.

Additionally, there's tension between library usage and application usage. Libraries should strive to be minimal in what they bring in to better work in more contexts, but applications tend to want to be maximal.

Anecdotally, library crates do a poor job of using default-features = false.

and can't directly opt-out.

Can you provide some concrete cases where such a thing would be beneficial? Presumably you are encountering one "in the wild" to prompt the request.

from snafu.

shepmaster avatar shepmaster commented on July 17, 2024

Another point is that eventually, backtraces will be added to the standard library. This will essentially make it "free" to use a backtrace.

from snafu.

lucab avatar lucab commented on July 17, 2024

Thanks for the very quick feedback!

I don't have a direct report from the field, as I'm still in the planning phase of migrating my existing crates (e.g. caps) to snafu, but I've encountered the same friction with both failures and error-chain.

I maintain a mix of both libraries and applications. For the former, it is painful to keep tracking and disabling default features, in order to let application developers being in control. For the latter, opt-ing out default features through chained dependencies becomes a game of whack-a-mole.

You suggestion of an inert Backtrace type makes sense to me. Additionally in that case, direct consumers should be able to enable your feature directly via --features snafu/backtraces.

from snafu.

lucab avatar lucab commented on July 17, 2024

I didn't see opposition to this and the plan above seemed well-rounded, so I proceeded and opened a PR at #157.

from snafu.

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.