我正在尝试定义一个js_sys::Promise
. 承诺的解析应该返回一个容器,其中包含buf
在 webrtc 初始化中收集的所有候选冰。
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)>,
);
});
我不确定如何转换buf
为JsValue
调用该resolve
函数所需的。尝试编译上面的代码时出现以下错误:
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>`
我尝试了一些转换 buf 的替代方法:
resolve.call0(&JsValue::from(&buf));
这给出了错误:
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`
并JsValue::from_serde
要求RtcIceCandidate
实现Serialize
特征,这里不是这种情况。