Git Product home page Git Product logo

js_int's Introduction

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.

License

MIT

js_int's People

Contributors

auscompgeek avatar fsmaxb avatar jimmycuadra avatar jplatte avatar noble-mushtak avatar v02460 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

js_int's Issues

Allow negative zero for Int?

f64 has separate binary representations for 0 and -0. They compare equal, but the minus sign is often visible when printed the value. However, in JS converting -0 to a string results in "0". If we do add a separate binary representation, we need to ensure it compares equal and produces the same hash as 0 though.

Benchmarks

It would be good to have benchmarks that show that using Int or UInt is no slower than using i64 or u64.

wasm_bindgen support

Allow using Int / UInt in wasm_bindgen interfaces, if not possible yet (probably isn't)

Support deserializing from floating point without fractional component.

Currently if you have a JSON value like 42.0 it fails to deserialize to Int or UInt.

Given that a JSON encoder in a language that only supports IEEE754 doubles will not necessarily omit the fractional part like .0 it would be great to support deserializing from those as well.

This feature would be different from lax_deserialize in that it only accepts doubles that only contain an integer value, values like 0.5 would be rejected.

If there is interest in this feature I can provide a pull request.

This can either be added under the serde feature, or a new feature flag, similar to lax_deserialize.

As an example of how this could look like see:
https://github.com/communityvi/communityvi/blob/01a0beea2d95fd8256625774bfe3b6fca948e823/communityvi-server/src/utils/portable_unsigned_integer.rs#L50-L63

How to implement signed over- / underflow for Int?

Rust specifies that, when debug assertions are disabled, signed integers over- and underflow. It have not found any documentation about whether bitmasks on negative integers are defined. If they are defined, this is (probably) easy to implement. Otherwise, any implementation would (probably) not be much faster than just having range-check assertions in both debug and release mode.

Store only 7 bytes instead of 8

We theoretically have 54 bits of information in Int and 53 bits in UInt. Because it is much simpler to just wrap an existing numeric type, we currently use 8 bytes (64 bits) to store this data, even though 7 bytes (56 bits) would be sufficient. Implementing 7-byte storage would certainly complicate the code quite a bit and potentially affect performance negatively. I could imagine developing it out under a feature flag though. If we have benchmarks until then (#4), that would be very useful.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.