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

implement OptionIntoWasmAbi for Option<&Closure> #2173

Closed
ctaggart opened this issue May 31, 2020 · 6 comments · May be fixed by #2191
Closed

implement OptionIntoWasmAbi for Option<&Closure> #2173

ctaggart opened this issue May 31, 2020 · 6 comments · May be fixed by #2191

Comments

@ctaggart
Copy link
Contributor

ctaggart commented May 31, 2020

Motivation

In Monico Editor, you configure the environment with callbacks. This is the TypeScript:

    export interface Environment {
        getWorkerUrl?(workerId: string, label: string): string;
    }

It is not readonly, so it is settable. It has a ?, so it may be set with undefined.

This works if i map it using an optional js_sys::Function:

#[wasm_bindgen()]
extern "C" {
    pub type Environment;
    # [ wasm_bindgen ( method , setter , js_name = getWorkerUrl ) ]
    pub fn set_get_worker_url(this: &Environment, value: Option<&Function>>);
}

In order to have it be more type safe, I want to use Closure:

#[wasm_bindgen()]
extern "C" {
    pub type Environment;
    # [ wasm_bindgen ( method , setter , js_name = getWorkerUrl ) ]
    pub fn set_get_worker_url(this: &Environment, value: Option<&Closure<dyn FnMut(String, String) -> String>>);
}

But then I get this error:

   Compiling monaco-editor-hello-world v0.1.0 (/Users/cameron/rs/ts2rs/monaco-editor-hello-world)
error[E0277]: the trait bound `&wasm_bindgen::closure::Closure<dyn std::ops::FnMut(std::string::String, std::string::String) -> std::string::String>: wasm_bindgen::convert::traits::OptionIntoWasmAbi` is not satisfied
  --> monaco-editor-hello-world/src/monaco.rs:10:51
   |
10 |     pub fn set_get_worker_url(this: &Environment, value: Option<&Closure<dyn FnMut(String, String) -> String>>);
   |                                                   ^^^^^ the trait `wasm_bindgen::convert::traits::OptionIntoWasmAbi` is not implemented for `&wasm_bindgen::closure::Closure<dyn std::ops::FnMut(std::string::String, std::string::String) -> std::string::String>`
   |
   = note: required because of the requirements on the impl of `wasm_bindgen::convert::traits::IntoWasmAbi` for `std::option::Option<&wasm_bindgen::closure::Closure<dyn std::ops::FnMut(std::string::String, std::string::String) -> std::string::String>>`

error[E0277]: the trait bound `&wasm_bindgen::closure::Closure<(dyn std::ops::FnMut(std::string::String, std::string::String) -> std::string::String + 'static)>: wasm_bindgen::convert::traits::OptionIntoWasmAbi` is not satisfied
 --> monaco-editor-hello-world/src/monaco.rs:4:1
  |
4 | #[wasm_bindgen()]
  | ^^^^^^^^^^^^^^^^^ the trait `wasm_bindgen::convert::traits::OptionIntoWasmAbi` is not implemented for `&wasm_bindgen::closure::Closure<(dyn std::ops::FnMut(std::string::String, std::string::String) -> std::string::String + 'static)>`
  |
  = note: required because of the requirements on the impl of `wasm_bindgen::convert::traits::IntoWasmAbi` for `std::option::Option<&wasm_bindgen::closure::Closure<(dyn std::ops::FnMut(std::string::String, std::string::String) -> std::string::String + 'static)>>`
  = note: this error originates in an attribute macro (in Nightly builds, run with -Z macro-backtrace for more info)

error: aborting due to 2 previous errors

For more information about this error, try `rustc --explain E0277`.
error: could not compile `monaco-editor-hello-world`.

To learn more, run the command again with --verbose.

Proposed Solution

Implement OptionIntoWasmAbi for Option<&Closure>.

@ctaggart ctaggart changed the title Implement OptionIntoWasmAbi for Option<&Closure> implement OptionIntoWasmAbi for Option<&Closure> May 31, 2020
ctaggart added a commit to taggartsoftware/ts2rs that referenced this issue May 31, 2020
@ctaggart
Copy link
Contributor Author

Steps to reproduce:

git clone git@github.com:taggartsoftware/ts2rs.git
cd ts2rs
git checkout e5cd3367effd2fbb49bdbe258d6519eefe082a00
cd monaco-editor-hello-world
cargo build

@alexcrichton
Copy link
Contributor

Seems reasonable to me to add! I think this should be a relatively simple PR to add.

@teymour-aldridge
Copy link
Contributor

@alexcrichton can I try my hand at this?

@Pauan
Copy link
Contributor

Pauan commented Jun 4, 2020

@teymour-aldridge Of course, we gladly accept PRs.

@teymour-aldridge
Copy link
Contributor

Cool! I'll try and do this tommorow.

@ctaggart
Copy link
Contributor Author

ctaggart commented May 3, 2022

Same as #2767 & fixed by #2768. 🥳 Thanks @sinking-point 👏🏻 .

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

Successfully merging a pull request may close this issue.

4 participants