napi-rs / napi-rs

A framework for building compiled Node.js add-ons in Rust via Node-API
https://napi.rs
Other
5.55k stars 236 forks source link

Support JavaScript Sets #2099

Open shohamazon opened 1 week ago

shohamazon commented 1 week ago

Hi there! 🙂

I'm currently working on a project where I utilize N-API to facilitate the conversion of values from a Rust core to a TypeScript wrapper. However, I've encountered an issue: I'm unable to convert a Set object to a JavaScript Set due to the lack of support for sets in N-API.

Before proceeding further, I wanted to confirm whether my understanding is correct. Does N-API indeed lack support for sets, or is there a method I might have overlooked?

Additionally, I'm curious to know if there are any plans to include support for sets in N-API in future releases. If not, I'd appreciate any suggestions or workarounds that could help me achieve the desired conversion from Rust to TypeScript.

Thank you for your assistance!

southorange0929 commented 1 week ago

At present,N-API doesn't provide support to create a Set data structure. The creation from rust's hashmap is done by creating an object.

Could you consider using an array instead? Is there a specific situation where you absolutely need to use a set? Alternatively, after creating the array, you could create a set in the JS environment using this array.

shohamazon commented 1 week ago

Thank you for your response! This is exactly what I am doing (using an array and converting to a set in the Node wrapper).

What I wanted to know if I there was a way I could overcome this so many conversions, but I guess there isn't 😕.

southorange0929 commented 1 week ago

Yeah, there really isn't an ideal solution for this.