numy 0.1.0

Trait boundaries for primitive Rust types
Documentation
  • Coverage
  • 97.29%
    215 out of 221 items documented0 out of 0 items with examples
  • Size
  • Source code size: 64.57 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 10.53 MB This is the summed size of all files generated by rustdoc for all configured targets
  • Links
  • jsode64/numy
    0 0 0
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • jsode64

Numy

Trait boundaries for primitive Rust types.

Features

Numy contains traits for selection of Rust primitive types, mainly useful for generic guards.

For example: having a function take a type T that is only a numeric value, or one that takes only floats, or one that takes only signed integers, etc.

To see the provided traits, see the API's documentation.

Examples

use numy::{Float, Signed, UnsignedInt};

fn main() {
    foo(1.0_f64);
    bar(-1_i32);
    roy(7_u32);
}

/// Takes only floating point types (`f32` or `f64`).
fn foo<T: Float>(n: T) -> T {
    n.sqrt() * T::PI
}

/// Takes only signed types such as `i32` or `f64`.
fn bar<T: Signed>(n: T) -> T {
    n.abs() + T::ONE
}

/// Takes only unsigned integers such as `u32`.
fn roy<T: UnsignedInt>(n: T) -> T {
    // Note that `T::S` is the signed integer type with the same size as `T`.
    n.wrapping_add_signed(T::S::NEG_ONE)
}

"util" feature:

// Requires the `"util"` feature:
use numy::NumUtil;

fn main() {
    assert_eq!(1.abs_diff(3_i32), 2);
    assert_eq!(1.abs_diff(3_u32), 2);
    assert_eq!(1.0.abs_diff(3.0_f64), 2.0);
}

/// Returns the absolute difference of the two values.
fn abs_diff<T: NumUtil>(lhs: T, rhs: T) -> T {
    // `.diff` will never underflow for unsigned integers and `.abs` makes sure
    // signed types yield non-negative results.
    lhs.diff(rhs).abs()
}