'Converting Vec<RtcIceCandidate> into JsValue

I am trying to define a js_sys::Promise. The resolution of the promise should return a container buf with all gathered ice candidates in a webrtc initialization.

let promise = js_sys::Promise::new(&mut |resolve: js_sys::Function, reject: js_sys::Function| {
    let mut buf: Vec<RtcIceCandidate> = Vec::new();
    let onicecandidate_callback = Closure::wrap(
        Box::new(move |ev: RtcPeerConnectionIceEvent| match ev.candidate() {
            Some(candidate) => {
                buf.push(candidate);
            }
            None => {
                // resolve promise here
                resolve.call0(&buf.into())
            }
        }) as Box<dyn FnMut(RtcPeerConnectionIceEvent)>,
    );
});

I am not sure how to convert buf into a JsValue which is required to call the resolve function. I am getting the following error when trying to compile the code above:

error[E0277]: the trait bound `wasm_bindgen::JsValue: From<Vec<RtcIceCandidate>>` is not satisfied
   --> src/lib.rs:529:43
    |
529 |                     resolve.call0(&buffer.into());
    |                                           ^^^^ the trait `From<Vec<RtcIceCandidate>>` is not implemented for `wasm_bindgen::JsValue`
    |
    = help: the following implementations were found:
              <wasm_bindgen::JsValue as From<&'a T>>
              <wasm_bindgen::JsValue as From<&'a std::string::String>>
              <wasm_bindgen::JsValue as From<&'a str>>
              <wasm_bindgen::JsValue as From<ArrayBuffer>>
            and 112 others
    = note: required because of the requirements on the impl of `Into<wasm_bindgen::JsValue>` for `Vec<RtcIceCandidate>`

I have tried some alternative ways of converting buf:

resolve.call0(&JsValue::from(&buf));

This gives the error:

error[E0277]: the trait bound `Vec<RtcIceCandidate>: JsCast` is not satisfied
   --> src/lib.rs:529:36
    |
529 |                     resolve.call0(&JsValue::from(&buffer));
    |                                    ^^^^^^^^^^^^^ the trait `JsCast` is not implemented for `Vec<RtcIceCandidate>`
    |
    = note: required because of the requirements on the impl of `From<&Vec<RtcIceCandidate>>` for `wasm_bindgen::JsValue`

And JsValue::from_serde requires that RtcIceCandidate implements the Serialize trait, which is not the case here.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source