问题描述
我正在尝试使用warp
使异步处理程序为路由工作。我正在使用tokio
做一些文件操作。这是一个小例子:
use tokio::fs::File;
use tokio::prelude::*;
use warp::Filter;
#[tokio::main]
async fn main() {
let route = warp::path!("hello")
.and_then( move || async move {
let bin = File::open("test.txt").await?;
Result::<warp::reply::Json,ServiceError>::Ok(warp::reply::json(vec!("1","2")))
});
}
#[derive(Debug)]
enum ServiceError{
IoError(io::Error)
}
impl warp::reject::Reject for ServiceError {}
impl std::convert::From<std::io::Error> for ServiceError {
fn from(error: io::Error) -> Self{
ServiceError::IoError(error)
}
}
您需要:
[dependencies]
tokio = { version = "0.2",features = ["full","fs"] }
warp = "0.2"
错误发生在.and_then( move || async move {
:
the trait bound `ServiceError: warp::reject::sealed::CombineRejection<warp::Rejection>` is not satisfied
此特征是密封的,因此如果需要,我无法实现。我本以为实现warp::reject::Reject for ServiceError
就足够了。我不确定如何解决这个问题。
我发现this的答案提供了可能的解决方案,但我希望尽可能保留官方版本。
如果可能的话,我真的很想使用?
,而我的研究使我相信可以。使用map_error
显然是另一种选择,但麻烦得多,尤其是在多次调用异步文件功能的情况下。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)