我正在尝试让异步处理程序使用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
就足够了。我不知道如何解决这个问题。
我发现这个答案提供了一个可能的解决方案,但如果可能的话,我想留在官方版本上。
如果可能的话,我真的很想使用?
,我的研究让我相信我可以。Usingmap_error
显然是一种替代方法,但要麻烦得多,尤其是在多次调用异步文件函数的情况下。