Skip to content

Don't panic :) #971

Open
Open

Description

@felixrabe

I think there is too much unwrap in winit and it could benefit from a healty dose of Results. Thus I'm opening this issue to spark discussion on:

  • Is such a change desirable? (I vote "yes".)
  • To people knowing more about winit internals: Is it feasible? (If we can only get rid of panics in half the places, it might be more confusing to users than just a "catch the panic if you care" policy.)
  • If yes, start a PR converting a handful of currently non-Result-returning functions to return Result. (/me volunteers.)
  • Decide on when to release this. Seems to make sense to not include this change in 0.20, but maybe 0.21?
  • Pretty please use the codename "Don't panic :)" for that release. :)

Original discussion.

Activity

  1. felixrabe commented on Jun 23, 2019

    @felixrabe
    ContributorAuthor

    Results can always be converted to panics, but not the other way round.

  2. elinorbgr commented on Jun 23, 2019

    @elinorbgr
    Contributor

    I'll vote against blindly trying to remove all panics in winit, for the following reason.

    There are imo two kinds of panics:

    • "something bad occurred but it might be feasible to recover", these are absolutely worth converting to errors (examples would be "the current platform does not support the requested actions")
    • "something was fundamentally broken in the internal state of winit", these panics reveal a bug in winit, and the downstream crate cannot really do anything about it. I believe it makes sense to keep these panics (for example the wayland backend contains a large number of unwraps that fall in this category).

    So I'd prefer a guiding rule like: "Don't panic if this error occurring is not a bug and can be handled by the downstream crate".

  3. Osspial commented on Jun 23, 2019

    @Osspial
    Contributor

    I largely agree with @vberger here. There is one caveat I'd like to make here, though: In the "fundamentally broken" case, we should be using expect instead of unwrap, so that people seeing the panic can know what's causing it and where to report it.

    Still, I like what this issue is trying to accomplish. In terms of concrete steps to take, I'd suggest banning unwraps in favor of expect in future PRs, and then going over Winit's codebase to determine what panics are the result of bugs and which should be removed.

  4. felixrabe commented on Jun 23, 2019

    @felixrabe
    ContributorAuthor

    I think we actually agree. I propose sensible conversions of unwrap to Result where someone originally prototyped the API and it was just not changed since, because it would break the API. I don't mean to change unrecoverable error conditions that are best left to panic. (That's what I meant by "healthy dose", and I'm currently also working my way through "Programming Rust" that contains a very nice section on the semantics of panic vs. Result.)

    winit was started before Rust was even 1.0, and the ? operator that helps making Results much easier to use has only been added recently, so it makes sense that in the spirit of "let's ship it", many places where Result would (now) be preferable were just left to panic instead.

  5. added
    C - needs discussion
    S - api
    S - meta
    on Jun 26, 2019
  6. maxjoehnk commented on Nov 20, 2023

    @maxjoehnk

    I just wanna chime in here and add an argument regarding

    "something was fundamentally broken in the internal state of winit", these panics reveal a bug in winit, and the downstream crate cannot really do anything about it.

    If an application doesn't require windowing to function it can do something about it, for example disable the functionality requiring a window to work.
    I'm working on an application which can optionally render video to windows but I would like to use the same binary for (potentially) headless setups and headful setups.

    This is especially frustrating as winit already panics in the creation of the EventLoop which could easily just return a Result.

  7. daxpedda commented on Nov 21, 2023

    @daxpedda
    Member

    I'm generally against panicking in a library unless absolutely necessary.
    AFAIK there is no consensus among maintainers on this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    C - needs discussionS - apiS - meta

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions