-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How should an AsyncRead
be sent as a body?
#2008
Comments
As for direct support of |
Just to clarify: This is about |
@chrysn did you eventually manage to do it? |
Yes, but rather brutally. (By having a task spawned that reads from the AsyncRead file and feeds into a channel body. I'm not happy with it because of the task (especially as it's spawned using the tokio I think it'd be really convenient to have a way to get a Body right from an AsyncRead – but there was the "not sure it belongs in hyper" comment, and I'd like to understand whether that was only about tokio's AsyncRead or whether a future::AsyncRead would belong (in which case I'd open another issue, and might contribute code as I fix all my unhappiness about the current workaround). |
I'm not sure what the best solution is, but the main use case I have for this is streaming a File from disk to the Body without having to keep the entire file in memory at once (files can be very large), which seems like a pretty common use case. |
You don't need to use hyper's Body type at all - you can pretty easily make a http_body::Body implementation wrapping an AsyncRead that just buffers into a BytesMut and then splits of Bytes from that. |
Using a different Body type is definitely possible (working on a task-free cleaner implementation right now), but it's a bit unwieldy (all I do now have an implementation at https://gitlab.com/chrysn/annex-to-web/blob/master/src/asyncread_hyperbody.rs -- I'm still quite uncertain around where I do structural pinning, and have no clue whether the error handling is any good (but the HttpBody documentation is not explicit enough for me to grasp its details). |
Is there currently a good way to send an
AsyncRead
as the body of a response (or a request for that matter)?The best I could come up with is this:
But there are a couple problems with this:
Result<BytesMut, _>
toResult<Bytes, _>
is unergonomic, especially since this pattern needs to be done each time anAsyncRead
is sent as aBody
(unless a customDecoder
is written to decode toChunk
s or something).For 1, I think that either the feature flag should be removed, or there should be better documentation on why it exists.
For 2, the easiest solutions would be to either implement
From<BytesMut>
forChunk
, or do #1931. Another option is to add direct support forAsyncRead
toBody
(See #1328).The text was updated successfully, but these errors were encountered: