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

Misleading error returning impl Future #1918

Open
NickeZ opened this issue Dec 18, 2019 · 3 comments
Open

Misleading error returning impl Future #1918

NickeZ opened this issue Dec 18, 2019 · 3 comments
Labels

Comments

@NickeZ
Copy link

NickeZ commented Dec 18, 2019

I'm trying to create async methods on my struct. I looked at #1858 but that issue is closed without an explanation why the code below doesn't work. Is there any workaround?

use std::future::Future;
use wasm_bindgen::prelude::*;

#[wasm_bindgen]
pub struct A {}

#[wasm_bindgen]
impl A {
    #[wasm_bindgen(constructor)]
    pub fn new() -> Self {
        A {}
    }

    pub fn test(&mut self) -> impl Future<Output = Result<JsValue, JsValue>> {
        Ok(JsValue::from(true))
    }
}
error[E0667]: `impl Trait` is not allowed in path parameters
 --> src/lib.rs:7:1
  |
7 | #[wasm_bindgen]
  | ^^^^^^^^^^^^^^^

error[E0562]: `impl Trait` not allowed outside of function and inherent method return types
 --> src/lib.rs:7:1
  |
7 | #[wasm_bindgen]
  | ^^^^^^^^^^^^^^^

error[E0277]: the trait bound `impl std::future::Future: wasm_bindgen::convert::traits::IntoWasmAbi` is not satisfied
 --> src/lib.rs:7:1
  |
7 | #[wasm_bindgen]
  | ^^^^^^^^^^^^^^^ the trait `wasm_bindgen::convert::traits::IntoWasmAbi` is not implemented for `impl std::future::Future`
  |
  = note: required because of the requirements on the impl of `wasm_bindgen::convert::traits::ReturnWasmAbi` for `impl std::future::Future`

error: aborting due to 3 previous errors
@Pauan
Copy link
Contributor

Pauan commented Dec 18, 2019

It'd be nice if we could support impl Future, but it seems rather tricky.

@alexcrichton does this seem doable, or would it be too tricky/hacky to support impl Future (even if it's special-cased)?

@NickeZ
Copy link
Author

NickeZ commented Dec 18, 2019

Otherwise maybe a nicer error message that points to js_sys::Promise?

@alexcrichton alexcrichton changed the title error[E0667]: impl Trait is not allowed in path parameters Misleading error returning impl Future Dec 18, 2019
@alexcrichton
Copy link
Contributor

Definitely agreed that we should try to improve the error here! It may be somewhat difficult to do so, but @Pauan is also correct in that you'll need to return a Promise instead of an impl Trait

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

No branches or pull requests

3 participants