-
Notifications
You must be signed in to change notification settings - Fork 234
Solution for asserting infallibility of a Result<T, Infallible> #329
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Breaking the silence on this topic after a couple years, I'll add some takes. Poking around a little bit, if you set any But I found the
And with that said! I stumbled upon TL;DR I get to the following table:
No1 = It's not obvious from So my opinion is that:
However I cannot propose what the tradeoff should be here. If we're going to push hard on Possibly even with feature flags and adding Currently I don't see any |
After removing `void`, we're still missing a way to explicitly unwrap infallible results such that it is clear that no panic can occur in this location. Because the standard library does not yet provide a solution to this, let's use the `unwrap-infallible` crate [1] in the meantime. This topic was also discussed in more detail in the embedded-hal project [2]. [1]: https://crates.io/crates/unwrap-infallible [2]: rust-embedded/embedded-hal#329
After removing `void`, we're still missing a way to explicitly unwrap infallible results such that it is clear that no panic can occur in this location. Because the standard library does not yet provide a solution to this, let's use the `unwrap-infallible` crate [1] in the meantime. This topic was also discussed in more detail in the embedded-hal project [2]. [1]: https://crates.io/crates/unwrap-infallible [2]: rust-embedded/embedded-hal#329
(From #328)
Back in the days, before
core::convert::Infallible
was added, we hadvoid::Void
to markResult
s which cannot ever produce the error variant. With it came thevoid::ResultExt
extension trait that adds a.void_unwrap()
method toResult<T, Void>
, to unwrap such an infallible result. There were nice advantages to that over just using normalunwrap()
on such results:Result
is getting unwrapped.void_unwrap()
would immediately stop to compile, forcing one to revisit the code. When just usingunwrap()
, this would silently become a panic site on dependency upgrade.Now, the
void
crate is largely unmaintained and ascore::convert::Infallible
is becoming the core-language equivalent, I'd like to see a similar feature available. There is an unstableResult<T, !>::into_ok()
method but that doesn't help much for stable users. To note, it is trivial to reimplement but I feel like this needs to be done somewhere "official" and embraced throughout the ecosystem.What I care about most here is how we can teach proper coding habits: By experience, people copy from crate and documentation examples so if these use
unwrap()
, downstream code will as well. In turn, this will make it hard for upstream crates to eventually introduce concrete error types because of the risk of silently breaking downstream code. If there is a commonly accepted version of "infallible_unwrap()
" (please bikeshed a better name) which is used throughout examples, downstream users will naturally move to using it as well.I see a few possible solutions to here:
embedded-hal
. While not technically a fitting place,embedded-hal
(and itsprelude
!) is pulled in by lots of projects already, so having it available there would reach the most people.into_ok()
method such that we can just promote use of that.The text was updated successfully, but these errors were encountered: