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

Feature request: Typed array types #315

Open
eye-wave opened this issue May 9, 2024 · 3 comments
Open

Feature request: Typed array types #315

eye-wave opened this issue May 9, 2024 · 3 comments
Labels
enhancement New feature or request

Comments

@eye-wave
Copy link

eye-wave commented May 9, 2024

Is your feature request related to a problem? Please describe.
It would be to have Integer of Float Vectors be converted to typed arrays.

example

Vec<f32> -> Float32Array
Vec<u8> -> Uint8Array
Vec<i32> -> Int32Array 

Describe alternatives you've considered
Tsify, but it only works with wasm-bindgen which i don't need for a tauri app

adding #[ts(type = "MyArray")] for each problematic property

@eye-wave eye-wave added the enhancement New feature or request label May 9, 2024
@escritorio-gustavo
Copy link
Contributor

I'm not sure we can implement this at all.

At the moment, Vec<T> simply exports Array<T>, which is generally what the user expects, even in Tauri, as most of the time the serialization is done through serde.

We do this through a generic impl block on Vec that goes something like

impl<T: TS> TS for Vec<T> {
    // ... export T, but wrapped in Array<>
}

So we can't just write

impl TS for Vec<u8> {
    // Export as Uint8Array
}

because that would generate a compiler error complaining about conflicting trait impls.

Also, most people don't expect an Uint8Array, so even if it were allowed, we still couldn't do that easily, as it can't be the default behavior.

I do recommend that you create a type that exports as Uint8Array and use #[ts(as = "...")] instead of #[ts(type = "...")] though, as it helps avoid typos

@NyxCode
Copy link
Collaborator

NyxCode commented Jun 21, 2024

@eye-wave I'd be interested in how you do (de)serialization such that you'd profit from ts-rs generating typed arrays. What format are you using for (de)serialization, and which libraries do you use for them on the Rust/TS side?

I'd generally be open to support more data models besides the serde data model, but the serde model is the only commonly used one i am aware of.

@eye-wave
Copy link
Author

I'm 90% sure that it works like that with binaries generated with wasm-bindgen

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants