#number #complex #float #integer #unsigned

number-general

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

29 releases

new 0.9.1 Jan 24, 2023
0.8.3 Sep 3, 2022
0.8.0 Apr 21, 2022
0.7.1 Mar 11, 2022
0.3.0 Feb 1, 2021

#345 in Data structures

Download history 40/week @ 2022-10-08 19/week @ 2022-10-15 33/week @ 2022-10-22 57/week @ 2022-10-29 87/week @ 2022-11-05 36/week @ 2022-11-12 59/week @ 2022-11-19 30/week @ 2022-11-26 47/week @ 2022-12-03 91/week @ 2022-12-10 42/week @ 2022-12-17 31/week @ 2022-12-24 15/week @ 2022-12-31 42/week @ 2023-01-07 30/week @ 2023-01-14 91/week @ 2023-01-21

181 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.6–1.5MB
~33K SLoC