Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Conversion BigInt/u64 doesn't work #310

Open
adimit opened this issue Feb 7, 2023 · 0 comments
Open

Conversion BigInt/u64 doesn't work #310

adimit opened this issue Feb 7, 2023 · 0 comments
Labels
bug Something isn't working

Comments

@adimit
Copy link

adimit commented Feb 7, 2023

Describe the Bug

With serde-wasm-bindgen conversion between bigint and u64 is unproblematic. When I have a struct with a u64 in Rust, I get a bigint (or BigInt) in JS-land. With gloo-utils u64 is translated to number, losing precision.

Steps to Reproduce

Rust:

#[derive(serde::Serialize, serde::Deserialize)]
struct Foo {
    field: u64,
}

#[wasm_bindgen]
pub fn make_a_mistake() -> JsValue {
    <JsValue as gloo_utils::format::JsValueSerdeExt>::from_serde(&Foo {
        field: 12815844532926915242u64,
    })
    .unwrap()
}

Then in JS:

console.log(make_a_mistake());

Expected Behavior

Object { field: 12815844532926915242 }

Actual Behavior

Result:

Object { field: 12815844532926915000 }

And 12815844532926915000 ≠ 12815844532926915242.

Additional Context

Without knowing how the library really works, I'm guessing the u64 gets stringified Rust-side into a neat { field: 12815844532926915242 } and then JSON.parse() mangles that value. I'm not sure this scenario can be handled with this approach at all. In that case, using serde-wasm-bindgen works well, and should be used instead.
The only way to make this work, I guess, is to treat the number as a string and then transparently post-process it in JS. I'm not sure this within the scope of your project. If it isn't, feel free to close this issue!

@adimit adimit added the bug Something isn't working label Feb 7, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant