Skip to content

float::not_nan #507

Closed as not planned
Closed as not planned
@oriongonza

Description

@oriongonza

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    T-libs-apiapi-change-proposalA proposal to add or alter unstable APIs in the standard libraries

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions