#complex-numbers #numbers #deserialize #complex #float #integer

number-general

A generic number type for Rust which supports basic math and (de)serialization

33 releases

0.12.0 Aug 13, 2024
0.11.1 Dec 20, 2023
0.11.0 Nov 6, 2023
0.10.0 Jul 3, 2023
0.3.0 Feb 1, 2021

#158 in Math

Download history 39/week @ 2024-08-17 28/week @ 2024-08-24 47/week @ 2024-08-31 12/week @ 2024-09-07 41/week @ 2024-09-14 155/week @ 2024-09-21 39/week @ 2024-09-28 143/week @ 2024-10-05 328/week @ 2024-10-12 446/week @ 2024-10-19 499/week @ 2024-10-26 562/week @ 2024-11-02 214/week @ 2024-11-09 175/week @ 2024-11-16 154/week @ 2024-11-23 106/week @ 2024-11-30

715 downloads per month
Used in 16 crates (4 directly)

Apache-2.0

125KB
4K SLoC

number-general

A generic Rust number type with support for basic math operations, (de)serialization, and casting with safecast.

Example usage:

use number_general::{Int, Number};
use safecast::CastFrom;

let sequence: Vec<Number> = serde_json::from_str("[true, 2, 3.5, -4, [1.0, -0.5]]").unwrap();
let actual = sequence.into_iter().product();

assert_eq!(actual, Number::from(num::Complex::<f64>::new(-28., 14.)));
assert_eq!(Int::cast_from(actual), Int::from(-28));

lib.rs:

Provides a generic Number enum with Boolean, Complex, Float, [Int], and UInt variants, as well as a NumberCollator, ComplexCollator, and FloatCollator since these types do not implement [Ord].

Number supports casting with safecast.

For (de)serialization with serde, enable the "serde" feature.

For (de)coding with destream, enable the "stream" feature.

Example usage:

let sequence: Vec<Number> = vec![true.into(), 2.into(), 3.5.into(), [1.0, -0.5].into()];
let actual = sequence.into_iter().product();
let expected = Number::from(num::Complex::<f64>::new(7., -3.5));

assert_eq!(expected, actual);
assert_eq!(Int::cast_from(actual), Int::from(7));

Dependencies

~1.1–2.1MB
~42K SLoC