我正在写一个在线的latex编辑器,用户可以在浏览器中编辑latex文档。当用户单击“编译”按钮时,我想将服务器端的latex日志输出推送到客户端。我选择使用SSE来实现此功能。现在,我尝试像这样定义SSE服务器端API:
use actix_web::{middleware::Logger, web, App, HttpRequest, HttpResponse, HttpServer};
use futures::StreamExt;
async fn sse_handler(req: HttpRequest, stream: web::Payload) -> HttpResponse {
let mut res = HttpResponse::Ok()
.content_type("text/event-stream")
.streaming(Box::pin(stream.map(|chunk| {
Ok(actix_web::web::Bytes::from(format!(
"data: {}\n\n",
String::from_utf8(chunk.unwrap().to_vec()).unwrap()
)))
})));
if req.headers().get("cache-control").is_none() {
res.headers_mut().insert(
actix_web::http::header::CACHE_CONTROL,
actix_web::http::header::HeaderValue::from_static("no-cache"),
);
}
res
}
#[actix_web::main]
async fn main() -> std::io::Result<()> {
HttpServer::new(|| {
App::new()
.wrap(Logger::default())
.route("/events", web::get().to(sse_handler))
})
.bind("127.0.0.1:8080")?
.run()
.await
}
编译器显示错误:
error[E0283]: type annotations needed
--> src/main.rs:8:13
|
7 | .streaming(Box::pin(stream.map(|chunk| {
| --------- type must be known at this point
8 | Ok(actix_web::web::Bytes::from(format!(
| ^^ cannot infer type of the type parameter `E` declared on the enum `Result`
|
= note: cannot satisfy `_: Into<Box<(dyn std::error::Error + 'static)>>`
note: required by a bound in `HttpResponseBuilder::streaming`
--> /Users/xiaoqiangjiang/.cargo/registry/src/mirrors.tuna.tsinghua.edu.cn-df7c3c540f42cdbd/actix-web-4.3.1/src/response/builder.rs:324:12
|
324 | E: Into<BoxError> + 'static,
| ^^^^^^^^^^^^^^ required by this bound in `HttpResponseBuilder::streaming`
help: consider specifying the generic arguments
|
8 | Ok::<actix_web::web::Bytes, E>(actix_web::web::Bytes::from(format!(
| ++++++++++++++++++++++++++++
我读了这个错误,没有弄清楚哪里出错了。我应该怎么做才能解决这个问题并使其正常工作?这是我的货物。toml:
[package]
name = "rust-learn"
version = "0.1.0"
edition = "2018"
[dependencies]
tokio = { version = "1.17.0", features = ["full"] }
serde = { version = "1.0.64", features = ["derive"] }
serde_json = "1.0.64"
actix-web = "4"
futures = "0.3"
1条答案
按热度按时间vsnjm48y1#
问题就出在这一部分,这里你从闭包返回
Ok
:如果您查看
streaming()
方法的签名,您可以看到它对于流的错误类型是通用的:任何满足
Into<BoxError> + 'static
的错误都是有效的。问题是编译器没有足够的信息来知道你想使用什么错误类型。您可以通过多种方式提供此信息。例如,如果您想使用std::io::Error
:如果您想使用
Box<dyn Error>
:或