类型不匹配解决 future_to_promise
type mismatch resolving future_to_promise
我正在尝试执行以下操作:
- 通过参数接收函数
- 执行returns Promise
的函数
- 等待 Promise 被解决
- 执行某事
- return承诺
fn get_replaced(parser: Parser, string_generators: Vec<Function>, initial_capacity: usize) -> Result<JsString, JsValue> {
// do something
}
#[wasm_bindgen]
pub fn markdown_img_url_editor(markdown_text: &str, converter: &Function, before_collect_callback: JsValue) -> Promise {
// do something
if before_collect_callback.is_null() || before_collect_callback.is_undefined() {
// do something
} else {
match before_collect_callback.dyn_into::<Function>() {
Ok(callback) => {
match callback.call0(&JsValue::NULL) {
Ok(maybe_promise) => {
if let Ok(p) = maybe_promise.dyn_into::<Promise>() {
// return p.then(&Closure::wrap(Box::new(move |_| get_replaced_wrap(parser, string_generators, markdown_text.len() + 128))));
let future = JsFuture::from(p).compat().then(|_| future::ready(get_replaced(parser, string_generators, markdown_text.len() + 128)));
let ff = future.compat();
return future_to_promise(ff);
起初,我想打电话给js_sys::Promise#then
。但是,没有办法从闭包中 return 传递给 js_sys::Promise#then
的东西。
所以,我正在尝试将 js_sys::Promise
转换为 wasm_bindgen_futures::JsFuture
,调用 futures::future::TryFutureExt#and_then
并使用 future_to_promise
将 wasm_bindgen_futures::JsFuture
转换为 js_sys::Promise
.
现在,我得到如下所示的编译错误:
$cargo build
Compiling markdown_img_url_editor_rust v0.1.0 (C:\msys64\home\yumetodo\markdown_img_url_editor\markdown_img_url_editor_rust)
warning: unused import: `futures::future::Future`
--> src\lib.rs:6:5
|
6 | use futures::future::Future;
| ^^^^^^^^^^^^^^^^^^^^^^^
|
= note: #[warn(unused_imports)] on by default
error[E0271]: type mismatch resolving `<futures_util::future::then::Then<futures_util::compat::compat01as03::Compat01As03<wasm_bindgen_futures::legacy_shared::JsFuture>, futures_util::future::ready::Ready<std::result::Result<js_sys::JsString, wasm_bindgen::JsValue>>, [closure@src\lib.rs:116:74: 116:159 parser:_, string_generators:_, markdown_text:_]> as core::future::future::Future>::Output == std::result::Result<wasm_bindgen::JsValue, _>`
--> src\lib.rs:118:36
|
118 | return future_to_promise(ff);
| ^^^^^^^^^^^^^^^^^ expected struct `js_sys::JsString`, found struct `wasm_bindgen::JsValue`
|
= note: expected type `std::result::Result<js_sys::JsString, wasm_bindgen::JsValue>`
found type `std::result::Result<wasm_bindgen::JsValue, _>`
= note: required because of the requirements on the impl of `futures_core::future::TryFuture` for `futures_util::future::then::Then<futures_util::compat::compat01as03::Compat01As03<wasm_bindgen_futures::legacy_shared::JsFuture>, futures_util::future::ready::Ready<std::result::Result<js_sys::JsString, wasm_bindgen::JsValue>>, [closure@src\lib.rs:116:74: 116:159 parser:_, string_generators:_, markdown_text:_]>`
= note: required by `wasm_bindgen_futures::legacy::future_to_promise`
error: aborting due to previous error
For more information about this error, try `rustc --explain E0271`.
error: Could not compile `markdown_img_url_editor_rust`.
found type std::result::Result<wasm_bindgen::JsValue, _>
我无法理解这些错误发生的原因。 get_replaced
returns Result<JsString, JsValue>
。为什么错误消息说找到类型 std::result::Result<wasm_bindgen::JsValue, _>
?????
请告诉我如何解决此错误或其他解决方案。
Cargo.toml
[package]
name = "markdown_img_url_editor_rust"
version = "0.1.0"
authors = ["yumetodo <yume-wikijp@live.jp>"]
edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
pulldown-cmark = "0.5.3"
pulldown-cmark-to-cmark = "1.2.2"
wasm-bindgen = "0.2.50"
# wasm-bindgen-futures = "0.3.27"
wasm-bindgen-futures = { version="0.3.27", features=["futures_0_3"] }
js-sys = "0.3.27"
futures-preview = { version="0.3.0-alpha", features=["compat"] }
# futures-preview="0.3.0-alpha"
[lib]
crate-type = ["cdylib"]
我的构建环境如下:
$rustup --version
rustup 1.18.3 (435397f48 2019-05-22)
$cargo --version
cargo 1.37.0 (9edd08916 2019-08-02)
$rustc --version
rustc 1.37.0 (eae3437df 2019-08-13)
OS: Windows 10 1809, Ubuntu 18.04
最后放弃抗争,决定重构API。 future_to_promise
请求静态生命周期 &str
和 pulldown_cmark
只接受 &'a str
是一个严重的问题。
ref: https://qiitadon.com/web/statuses/102710559203790261(日文)
为了重构API,我做了一个大胆的假设,即解析器将对相同的输入产生相同的结果。所以,我可以决定不持有解析器。
结果我做了这样的API
#[wasm_bindgen]
pub struct MarkdownImgUrlEditor {
markdown_text: String,
string_generators: Vec<Function>,
initial_capacity: usize,
}
#[wasm_bindgen]
impl MarkdownImgUrlEditor {
#[wasm_bindgen(constructor)]
pub fn new(text: String, converter: &Function) -> Result<MarkdownImgUrlEditor, JsValue> {
}
pub fn replace(&mut self) -> Result<String, JsValue> {
}
}
before_collect_callback
所做的处理将在调用new
和replace
之间执行。
https://github.com/yumetodo/markdown_img_url_editor/pull/13
顺便说一句,我注意到 pulldown_cmark
坏了。我无法获取图像替代值。
所以,我创建了一个问题。
我正在尝试执行以下操作:
- 通过参数接收函数
- 执行returns Promise 的函数
- 等待 Promise 被解决
- 执行某事
- return承诺
fn get_replaced(parser: Parser, string_generators: Vec<Function>, initial_capacity: usize) -> Result<JsString, JsValue> {
// do something
}
#[wasm_bindgen]
pub fn markdown_img_url_editor(markdown_text: &str, converter: &Function, before_collect_callback: JsValue) -> Promise {
// do something
if before_collect_callback.is_null() || before_collect_callback.is_undefined() {
// do something
} else {
match before_collect_callback.dyn_into::<Function>() {
Ok(callback) => {
match callback.call0(&JsValue::NULL) {
Ok(maybe_promise) => {
if let Ok(p) = maybe_promise.dyn_into::<Promise>() {
// return p.then(&Closure::wrap(Box::new(move |_| get_replaced_wrap(parser, string_generators, markdown_text.len() + 128))));
let future = JsFuture::from(p).compat().then(|_| future::ready(get_replaced(parser, string_generators, markdown_text.len() + 128)));
let ff = future.compat();
return future_to_promise(ff);
起初,我想打电话给js_sys::Promise#then
。但是,没有办法从闭包中 return 传递给 js_sys::Promise#then
的东西。
所以,我正在尝试将 js_sys::Promise
转换为 wasm_bindgen_futures::JsFuture
,调用 futures::future::TryFutureExt#and_then
并使用 future_to_promise
将 wasm_bindgen_futures::JsFuture
转换为 js_sys::Promise
.
现在,我得到如下所示的编译错误:
$cargo build
Compiling markdown_img_url_editor_rust v0.1.0 (C:\msys64\home\yumetodo\markdown_img_url_editor\markdown_img_url_editor_rust)
warning: unused import: `futures::future::Future`
--> src\lib.rs:6:5
|
6 | use futures::future::Future;
| ^^^^^^^^^^^^^^^^^^^^^^^
|
= note: #[warn(unused_imports)] on by default
error[E0271]: type mismatch resolving `<futures_util::future::then::Then<futures_util::compat::compat01as03::Compat01As03<wasm_bindgen_futures::legacy_shared::JsFuture>, futures_util::future::ready::Ready<std::result::Result<js_sys::JsString, wasm_bindgen::JsValue>>, [closure@src\lib.rs:116:74: 116:159 parser:_, string_generators:_, markdown_text:_]> as core::future::future::Future>::Output == std::result::Result<wasm_bindgen::JsValue, _>`
--> src\lib.rs:118:36
|
118 | return future_to_promise(ff);
| ^^^^^^^^^^^^^^^^^ expected struct `js_sys::JsString`, found struct `wasm_bindgen::JsValue`
|
= note: expected type `std::result::Result<js_sys::JsString, wasm_bindgen::JsValue>`
found type `std::result::Result<wasm_bindgen::JsValue, _>`
= note: required because of the requirements on the impl of `futures_core::future::TryFuture` for `futures_util::future::then::Then<futures_util::compat::compat01as03::Compat01As03<wasm_bindgen_futures::legacy_shared::JsFuture>, futures_util::future::ready::Ready<std::result::Result<js_sys::JsString, wasm_bindgen::JsValue>>, [closure@src\lib.rs:116:74: 116:159 parser:_, string_generators:_, markdown_text:_]>`
= note: required by `wasm_bindgen_futures::legacy::future_to_promise`
error: aborting due to previous error
For more information about this error, try `rustc --explain E0271`.
error: Could not compile `markdown_img_url_editor_rust`.
found type
std::result::Result<wasm_bindgen::JsValue, _>
我无法理解这些错误发生的原因。 get_replaced
returns Result<JsString, JsValue>
。为什么错误消息说找到类型 std::result::Result<wasm_bindgen::JsValue, _>
?????
请告诉我如何解决此错误或其他解决方案。
Cargo.toml
[package]
name = "markdown_img_url_editor_rust"
version = "0.1.0"
authors = ["yumetodo <yume-wikijp@live.jp>"]
edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
pulldown-cmark = "0.5.3"
pulldown-cmark-to-cmark = "1.2.2"
wasm-bindgen = "0.2.50"
# wasm-bindgen-futures = "0.3.27"
wasm-bindgen-futures = { version="0.3.27", features=["futures_0_3"] }
js-sys = "0.3.27"
futures-preview = { version="0.3.0-alpha", features=["compat"] }
# futures-preview="0.3.0-alpha"
[lib]
crate-type = ["cdylib"]
我的构建环境如下:
$rustup --version
rustup 1.18.3 (435397f48 2019-05-22)
$cargo --version
cargo 1.37.0 (9edd08916 2019-08-02)
$rustc --version
rustc 1.37.0 (eae3437df 2019-08-13)
OS: Windows 10 1809, Ubuntu 18.04
最后放弃抗争,决定重构API。 future_to_promise
请求静态生命周期 &str
和 pulldown_cmark
只接受 &'a str
是一个严重的问题。
ref: https://qiitadon.com/web/statuses/102710559203790261(日文)
为了重构API,我做了一个大胆的假设,即解析器将对相同的输入产生相同的结果。所以,我可以决定不持有解析器。
结果我做了这样的API
#[wasm_bindgen]
pub struct MarkdownImgUrlEditor {
markdown_text: String,
string_generators: Vec<Function>,
initial_capacity: usize,
}
#[wasm_bindgen]
impl MarkdownImgUrlEditor {
#[wasm_bindgen(constructor)]
pub fn new(text: String, converter: &Function) -> Result<MarkdownImgUrlEditor, JsValue> {
}
pub fn replace(&mut self) -> Result<String, JsValue> {
}
}
before_collect_callback
所做的处理将在调用new
和replace
之间执行。
https://github.com/yumetodo/markdown_img_url_editor/pull/13
顺便说一句,我注意到 pulldown_cmark
坏了。我无法获取图像替代值。
所以,我创建了一个问题。