I have a function that takes a &mut io::Write
and I'd like to use it to send a streaming response from the actix-web server without having to buffer the whole response. The function is "pushing" the data, and I can't change the function (that's the whole premise of this question) to use async streams or other kind of polling.
Currently I'm forced to use &mut Vec
(which implements io::Write
) to buffer the whole result and then send the Vec
as the response body. However, the response may be large, so I'd rather stream it without buffering.
Is there some kind of adapter that would implement io::Write
, with writes blocking as necessary in response to backpressure, and be compatible with types that actix-web can use for responses (e.g. futures::Stream
)?
fn generate(output: &mut io::Write) {
// ...
}
fn request_handler() -> Result<HttpResponse> {
thread::spawn(|| generate(/*???*/));
Ok(HttpResponse::Ok().body(/*???*/))
}
std::sync::mpsc
and futures::mpsc
have either both ends async, or both ends blocking, so it's not obvious how to use them as an adapter between sync and async ends.