nan-default 0.0.1

Derive macro for defaulting structs with floats to NAN
Documentation
  • Coverage
  • 50%
    1 out of 2 items documented1 out of 2 items with examples
  • Size
  • Source code size: 9.11 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 277.57 kB This is the summed size of all files generated by rustdoc for all configured targets
  • Ø build duration
  • this release: 4s Average build duration of successful builds.
  • all releases: 4s Average build duration of successful builds in releases after 2024-10-23.
  • Links
  • mfairman/nan-default
    0 0 0
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • tegimeki

nan-default License: MIT nan-default on crates.io nan-default on docs.rs Source Code Repository

NaN Default Derive Macro

A macro which lets you create structs whose floating-point members (f32, f64) are initialized as NAN rather than 0.0 by default. Struct members which are not floats get Default::default() assignments, and partial defaulting via ..Default::default() is also supported.

This can be convenient when using the special NAN state of a floating point number to designate it as invalid or uninitialized, rather than wrapping it in an Option which takes some extra space — especially if you have many of them. Of course, any operations on such variables need to check for this state just as they would need to check for the Some variant of an Option (albeit without idiomatic if-let statements, mapping, and so on). Depending on the application, is_nan() or !is_finite() may be appropriate functions to perform the check.

Example

use nan_default::NanDefault;

#[derive(NanDefault)]
struct Data {
    a: f32,
    b: [f64; 10],
    c: i32,
}

fn basic() {
    let data = Data::default();
    assert!(data.a.is_nan());
    assert!(data.b[9].is_nan());
    assert_eq!(data.c, 0);
}