Closed as not planned
Description
Proposal
Problem statement
We could make working with floats a bit nicer by leveraging ?
Motivating examples or use cases
rust-lang/rust#84277 (comment)
fn do_thing_checked(rhs: f32, lhs: f32) -> Option<f32> {
if (rhs.is_nan() || lhs.is_nan()) { return None }
let res = do_thing(rhs, lhs);
if res.is_nan() { return None }
Some(res)
}
Solution sketch
fn not_nan(self) -> Option<Self> {
(!num.is_nan()).then_some(num)
}
The example would look like the following:
fn do_thing_checked(rhs: f32, lhs: f32) -> Option<f32> {
rhs.not_nan()?;
lhs.not_nan()?;
do_thing(rhs, lhs).not_nan()
// or continue using it with
let res = do_thing(rhs, lhs).not_nan()?;
...
}
Alternatives
This could also be an extension trait implemented in a third party crate.