#integer #no-std

no-std js_int

JavaScript-interoperable integer types

13 releases

0.2.2 Mar 23, 2022
0.2.1 May 26, 2021
0.2.0 Dec 8, 2020
0.1.9 Jul 19, 2020
0.1.1 Jul 7, 2019

#8 in No standard library

Download history 908/week @ 2022-01-25 1217/week @ 2022-02-01 1432/week @ 2022-02-08 999/week @ 2022-02-15 749/week @ 2022-02-22 1080/week @ 2022-03-01 947/week @ 2022-03-08 1028/week @ 2022-03-15 1409/week @ 2022-03-22 1302/week @ 2022-03-29 1045/week @ 2022-04-05 988/week @ 2022-04-12 1267/week @ 2022-04-19 1219/week @ 2022-04-26 1864/week @ 2022-05-03 1144/week @ 2022-05-10

5,692 downloads per month
Used in 42 crates (10 directly)

MIT license

53KB
921 lines

js_int

Latest Version Docs

Crate js_int provides JavaScript-interoperable integer types.

JavaScript does not have native integers. Instead it represents all numeric values with a single Number type which is represented as an IEEE 754 floating-point value.* Rust's i64 and u64 types can contain values outside the range of what can be represented in a JavaScript Number.

This crate provides the types Int and UInt which wrap i64 and u64, respectively. These types add bounds checking to ensure the contained value is within the range representable by a JavaScript Number. They provide useful trait implementations to easily convert from Rust's primitive integer types.

* In the upcoming ECMAScript 2020, JavaScript will probably gain support for integers. There is a proposal for a BigInt type type that is not far from becoming part of the JavaScript specification. It won't make this crate obsolete in any way though, since there will still be lots of JS code using Number, and code in other languages that assumes its use.

This crate requires rustc >= 1.35.

This crate is no_std-compatible with default-features = false. This will disable the std feature, which at the time of writing will only omit the implementations of std::error::Error for ParseIntError and TryFromIntError.

(De-)Serialization via serde is supported via the serde feature, even without the std feature.

Deserialization can be routed through f64 instead of u64 with the float_deserialize feature. This will still not deserialize numbers with a non-zero fractional component. Enabling the lax_deserialize feature will discard the fractional part instead of declining to deserialize. Please be aware that serde_json doesn't losslessly parse large floats with a fractional part by default (even if the fractional part is .0). To fix that, enable its float_roundtrip feature.

Dependencies

~170KB