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

feat: impl From<T> for JsValue where T: IntoWasmAbi + Tsify #22

Merged
merged 1 commit into from
Apr 11, 2024

Conversation

Pantamis
Copy link

@Pantamis Pantamis commented Apr 11, 2024

Fix: #21

The main motivation of this PR is to allows Result<T, E> to implement hidden trait IntoJsResult which is required for a returned value to be allowed in async function exported by wasm-bindgen.

@Pantamis
Copy link
Author

The force push does implement From (not Into) and the diff is cleaner

@Pantamis Pantamis changed the title feat: implement Into<JsValue> for IntoWasmAbi tsify types feat: impl From<T> for JsValue where T: IntoWasmAbi + Tsify Apr 11, 2024
@siefkenj
Copy link
Owner

The new version looks much more concise :-).

Could you give a code snippet where you might use this feature?

@siefkenj siefkenj merged commit 90fb74d into siefkenj:main Apr 11, 2024
3 checks passed
@Pantamis
Copy link
Author

Thank you for the quick merge !

This comes very handy when you work with futures in Wasm for whatever reason so something like this is now possible:

#[derive(Debug, Serialize, Tsify)]
#[tsify(into_wasm_abi)]
struct PreciousNumber(usize);

#[wasm_bindgen]
async fn my_precious() -> PreciousNumber {

    let expansive_number: usize = query_precious_in_storage().await;

    PreciousNumber(expansive_number)
}

My use case was actually not querying a number in storage but querying the result of a very expensive computation that run in a rayon pool thanks to wasm-bindgen-rayon. To be able to display progress of the computation as made in Parallel Raytracing in wasm bindgen guide, I must not block the main thread with the expensive computation so I use an async channel to yield immediatly control back to the browser and get the result later through the Future channel receiver ! This allow to get all the TS types right in such cases now :)

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

Successfully merging this pull request may close these issues.

impl From<T> for JsValue where T: IntoWasmAbi using tsify
2 participants