Skip to content

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

Open
@johnsonw

Description

@johnsonw

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?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions