-
Notifications
You must be signed in to change notification settings - Fork 239
Description
(From #328)
Back in the days, before core::convert::Infallible
was added, we had void::Void
to mark Result
s which cannot ever produce the error variant. With it came the void::ResultExt
extension trait that adds a .void_unwrap()
method to Result<T, Void>
, to unwrap such an infallible result. There were nice advantages to that over just using normal unwrap()
on such results:
- For one, a reader could immediately understand that there is no potential panic location here, even though a
Result
is getting unwrapped. - Secondly, if the called function which returns said result ever changes to a different, concrete error type, any downstream code using
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 as core::convert::Infallible
is becoming the core-language equivalent, I'd like to see a similar feature available. There is an unstable Result<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:
- Create a very small crate with such an implementation and adopt it throughout the ecosystem to encourage its use.
- Actually add such an extension trait to
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. - Push for stabilization of the standard library
into_ok()
method such that we can just promote use of that.