rust - Rust:如何在函数链中使用 await
问题描述
所需代码:注释掉的块编译和工作,但是我想从嵌套匹配样式转移到更干净的函数链
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
链中的失败,所以很明显我缺乏理解......也试图返回我运行的未来编译器不知道返回大小的问题)
解决方案
有人告诉我不能在这样的函数链中使用 await。
当我切换到返回Box<dyn std::error::Error + Sync + Send + 'static>
错误类型时。然后我能够?
再次成功地将运算符用于中央代码(以前认为它由于错误返回类型不明确而失败)。
因此,即使没有函数更改,也可以使用这种语法来使代码变得美观和可读。
更新代码:
pub async fn upsert_competitions(req: WSReq, conn: PgConn, ws_conns: &mut WSConnections_, user_ws_id: Uuid) -> Result<String, BoxError>{
let deserialized: Vec<NewCompetition> = serde_json::from_value(req.data)?;
let competitions_out= db::upsert_competitions(&conn, deserialized.into_iter().map(transform_from).collect_vec())?;
if let Some(ws_user) = ws_conns.lock().await.get_mut(&user_ws_id){
sub_to_competitions(ws_user, competitions_out.iter().map(|c| &c.competition_id)).await;
}
publish_competitions(ws_conns, &competitions_out).await;
let resp_msg = WSMsgOut::resp(req.message_id, req.method, competitions_out);
serde_json::to_string(&resp_msg).map_err(|e| e.into())
}
推荐阅读
- c# - 如何将 Content-Type 标头添加到 .Net GET Web 请求?
- amazon-web-services - 尝试按日期过滤时出现 AWS Athena 查询错误
- python - 相互引用 Enum 成员
- r - 将一行中的值组合成r中的一列
- for-loop - 请改成嵌套for循环
- sql - 如何在多个连接行需要包含内容的情况下执行 SQL JOIN
- java - 何时在列表中使用 set 和 add 方法
- html - 对threejs的工作方式感到困惑
- mysql - SQL 按子字符串分组
- java - 如何优化多个相关 maven java 项目的 docker 构建?(缓存)