#number #complex #float #integer #unsigned

number-general

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

25 releases (6 breaking)

0.7.0 Jan 19, 2022
0.5.3 Jan 5, 2022
0.5.2 Dec 22, 2021
0.4.6 Nov 29, 2021
0.3.0 Feb 1, 2021

#227 in Data structures

Download history 31/week @ 2021-10-05 63/week @ 2021-10-12 12/week @ 2021-10-19 8/week @ 2021-10-26 16/week @ 2021-11-02 28/week @ 2021-11-09 33/week @ 2021-11-16 40/week @ 2021-11-23 49/week @ 2021-11-30 62/week @ 2021-12-07 21/week @ 2021-12-14 73/week @ 2021-12-21 14/week @ 2021-12-28 89/week @ 2022-01-04 27/week @ 2022-01-11 65/week @ 2022-01-18

199 downloads per month
Used in 9 crates (3 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:

# use number_general::{Int, Number};
# use safecast::CastFrom;
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

~0.7–1.3MB
~31K SLoC