Skip to content

help mapping chrome.tabs.query callback #1910

Closed
@ctaggart

Description

@ctaggart

How would I call [chrome.tabs.query] to get back an array of tabs. I'm struggling to write the interop for this.

https://developer.chrome.com/extensions/tabs#method-query
http://definitelytyped.org/docs/chrome--chrome/interfaces/chrome.tabs.tab.html

I created a module named chrome_tabs.rs and it currently has:

use wasm_bindgen::prelude::*;

#[wasm_bindgen]
pub struct QueryInfo {
    pub active: Option<bool>
}

#[wasm_bindgen]
pub struct Tab {
    pub active: bool,
    pub id: u32,
}

#[wasm_bindgen(module = "chrome.tabs")]
extern "C" {
    pub fn query(query_info: QueryInfo, callback: &Fn([&Tab]));
}

Errors with:

error[E0277]: the trait bound `&dyn for<'r> std::ops::Fn([&'r chrome_tabs::Tab]): wasm_bindgen::convert::traits::IntoWasmAbi` is not satisfied
  --> src/chrome_tabs.rs:19:1
   |
19 | #[wasm_bindgen(module = "chrome.tabs")]
   | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `wasm_bindgen::convert::traits::IntoWasmAbi` is not implemented for `&dyn for<'r> std::ops::Fn([&'r chrome_tabs::Tab])`
   |
   = help: the following implementations were found:
             <&'a (dyn for<'r> std::ops::Fn(&'r A) -> R + 'b) as wasm_bindgen::convert::traits::IntoWasmAbi>
             <&'a (dyn std::ops::Fn() -> R + 'b) as wasm_bindgen::convert::traits::IntoWasmAbi>
             <&'a (dyn std::ops::Fn(A) -> R + 'b) as wasm_bindgen::convert::traits::IntoWasmAbi>
             <&'a (dyn std::ops::Fn(A, B) -> R + 'b) as wasm_bindgen::convert::traits::IntoWasmAbi>
           and 6 others

I'm using nightly because I was looking at

#[wasm_bindgen]
pub struct RefFirstArgument {
contents: u32,
}
#[wasm_bindgen_test]
fn reference_as_first_argument_builds_at_all() {
#[wasm_bindgen]
extern "C" {
fn ref_first_arg1(a: &Fn(&JsValue));
fn ref_first_arg2(a: &mut FnMut(&JsValue));
fn ref_first_arg3(a: &Closure<Fn(&JsValue)>);
fn ref_first_arg4(a: &Closure<FnMut(&JsValue)>);
fn ref_first_custom1(a: &Fn(&RefFirstArgument));
fn ref_first_custom2(a: &mut FnMut(&RefFirstArgument));
fn ref_first_custom3(a: &Closure<Fn(&RefFirstArgument)>);
fn ref_first_custom4(a: &Closure<FnMut(&RefFirstArgument)>);
trying to find the answer.

wasm-bindgen = { version = "0.2.55", features = ["serde-serialize","nightly"]  }

If I do not make Tab a reference, then I get this error:

error[E0277]: the trait bound `[chrome_tabs::Tab]: wasm_bindgen::convert::traits::FromWasmAbi` is not satisfied
  --> src/chrome_tabs.rs:29:41
   |
29 |     pub fn query(query_info: QueryInfo, callback: &Fn([Tab]));
   |                                         ^^^^^^^^ the trait `wasm_bindgen::convert::traits::FromWasmAbi` is not implemented for `[chrome_tabs::Tab]`
   |
   = note: required because of the requirements on the impl of `wasm_bindgen::convert::traits::IntoWasmAbi` for `&dyn std::ops::Fn([chrome_tabs::Tab])`

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions