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

Returning closures from the start function fails when using #[wasm_bindgen(start)] #654

Open
johnsonw opened this issue Feb 6, 2022 · 0 comments

Comments

@johnsonw
Copy link
Contributor

johnsonw commented Feb 6, 2022

The update_from_js example shows how to create closures in rust and make them available in javascript:

#[wasm_bindgen]
pub fn start() -> Box<[JsValue]> {
    let app = App::start("app", init, update, view);
    create_closures_for_js(&app)
}

This works fine in the example, but our start function will generally use #[wasm_bindgen(start)] to make it immediately available to the application. The wasm-bindgen docs state that when using start, we can only return unit or a result: https://rustwasm.github.io/wasm-bindgen/reference/attributes/on-rust-exports/start.html.

The start function must take no arguments and must either return () or Result<(), JsValue>

But the seed docs state (https://seed-rs.org/0.8.0/start):

The official docs are a bit wrong - the function tagged with #[wasm_bindgen(start)] can also return all types that wasm_bindgen can transform to a consumable form for JS. In practice, this means you can return everything that implements FromWasmAbi. See e.g. Box<[JsValue]> in update_from_js example

When using the following in my seed application:

#[wasm_bindgen(start)]
pub fn start() -> Box<[JsValue]> {
    let app = App::start("app", init, update, view);
    create_closures_for_js(&app)
}

I get the following error:

error[E0277]: the trait bound `Box<[seed::prelude::JsValue]>: seed::prelude::wasm_bindgen::__rt::Start` is not satisfied
   --> src/lib.rs:700:1
    |
700 | #[wasm_bindgen(start)]
    | ^^^^^^^^^^^^^^^^^^^^^^ the trait `seed::prelude::wasm_bindgen::__rt::Start` is not implemented for `Box<[seed::prelude::JsValue]>`
    |
    = note: this error originates in the attribute macro `wasm_bindgen`

I'm hoping that I may be missing something but I believe wrapping wasm_bindgen in start is required as when I compile without it the package files contain errors. Being able to return closures appears to be the key piece when tying web_sys callbacks into the messaging system. Am I missing anything here?

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

No branches or pull requests

1 participant