impl Try for bool {
type Ok = ();
type Error = ();
fn into_result(self) -> Result<(), ()> {
if self { Ok(()) } else { Err(()) }
}
fn from_error((): ()) -> bool {
false
}
fn from_ok((): ()) -> bool {
true
}
}
I want this quite often when I want to do a bunch of checks and get false if any of the checks return false. Having bool implement Try will mean I can do that without using chains of combinators.
As far as I’m concerned bool is basically just shorthand for Option<()> which is basically just shorthand for Result<(), ()>, and we already have this for Option<T> and Result<T, E>.
Speaking of which, it’d be nice to have functions to downcast from Result<(), ()> to Option<()> and Option<()> to bool. Result::ok and Option::is_some can be used but I’d like functions that explicitly don’t throw away any data.
Unfortunately, I don’t think this will have the right precedences: !cond?. That alone makes me a bit skeptical that this is a good idea. This won’t compile but having to write (!cond)? is a bit surprising.
Zero / false meaning success is perhaps not as common as zero / false meaning failure, but it is commonly encountered -- for instance both Unix and Windows system calls use zero for success and some nonzero code for failure (if they don't have some other token to return). So I do not think this mapping is universal enough to be put in the stdlib, and unfortunately that torpedoes the entire concept.
You end up having the same polarity problem with Option and Result though. Occasionally I want to write code that returns early on the first Ok (or Some) but otherwise runs to the end collecting errors.