• lolcatnip@reddthat.com
    link
    fedilink
    English
    arrow-up
    1
    ·
    edit-2
    3 months ago

    I think a better solution would be to add a method called something like ulock that does a combined lock and unwrap.

    My concern with lock+unwrap is only partly because of convenience; I also didn’t like it because I think it’s a bad idea to get people used to casually calling unwrap, because it tends to hide inadequate error handing.

    Now that I think about it, I don’t like how unwrap can signal either “I know this can’t fail”, “the possible error states are too rare to care about” or “I can’t be bothered with real error handing right now”. In one or two of those cases you want to leave it in my production code, and in the last you want to audit all instances and replace them with proper error handing. Using the same function for all three cases makes that difficult.

    • BB_C
      link
      fedilink
      arrow-up
      3
      ·
      edit-2
      3 months ago

      a better solution would be to add a method called something like ulock that does a combined lock and unwrap.

      That’s exactly what’s done above using an extension trait! You can mutex_val.ulock() with it!

      Now that I think about it, I don’t like how unwrap can signal either “I know this can’t fail”, “the possible error states are too rare to care about” or “I can’t be bothered with real error handing right now”.

      That’s why you’re told (clippy does that i think) to use expect instead, so you can signal “whatever string” you want to signal precisely.

      • sugar_in_your_tea@sh.itjust.works
        link
        fedilink
        arrow-up
        1
        ·
        3 months ago

        Exactly! My code has a handful of “expect()” calls in it, and each one self-documents why it’s okay. It’s like a comment, but it appears in logs if it ever triggers.

    • 5C5C5C
      link
      fedilink
      arrow-up
      2
      ·
      3 months ago

      Best practice when using .unwrap() in production code is to put a line of documentation immediately above the use of .unwrap() that describes the safety invariants which allow the unwrap to be safe.

      Since code churn could eventually cause those safety invariants to be violated, I think it’s not a bad thing for a blunt audit of .unwrap() to bring your attention to those cases and prompt to reevaluate if the invariants are still satisfied.