【发布时间】:2020-07-29 23:59:42
【问题描述】:
所需代码: 注释掉的块编译和工作,但是我想从嵌套匹配样式转移到更清晰的函数链
async fn ws_req_resp(msg: String, conn: PgConn) -> Result<String, Box<dyn std::error::Error>>{
let req: WSReq = serde_json::from_str(&msg)?;
match req.request_type{
"upsert_competitions" => {
// let dr = serde_json::from_value(req.data);
// match dr{
// Ok(d) => match upsert_competitions(conn, d).await{
// Ok(x) => serde_json::to_string(&x).map_err(|e| e.into()),
// Err(e) => Err(Box::new(e))
// }
// Err(e) => Err(Box::new(e))
// }
serde_json::from_value(req.data).and_then(|d| async move {
upsert_competitions(conn, d).await}).and_then(|r| serde_json::to_string(&r))
.map_err(|e| e.into())
},
uwotm8 => {
Err(Box::new(InvalidRequestError{req_type: uwotm8.to_string()}))
}
}
}
upsert_competitions签名pub async fn upsert_competitions(conn: PgConn, new: Vec<ApiNewCompetition>) -> Result<Vec<DbCompetition>, diesel::result::Error>
错误:
expected enum `std::result::Result<_, serde_json::error::Error>`
found opaque type `impl core::future::future::Future`
已尝试将 await 放在链中的多个位置,但没有编译。 相信等待未来应该等待它完成,然后返回结果。
(对我来说,从这个函数返回一个未来可能会更好;然后在外面打开。但是我不明白为什么链中的await 失败了,所以很明显我缺乏理解......也在尝试为了返回一个未来,我遇到了编译器不知道返回大小的问题)
【问题讨论】:
标签: rust async-await